NUnit TestFixture with TestCases - unit-testing

Does NUnit provide functionality to have TestCase attributes associated with a TestFixture? I'd like to run the same set of tests on different implementations of the same interface. The concrete implementation would then be chosen in the SetUp method, and each test should be run with every implementation.

I don't see any possibility for this. But nice idea anyway.
If you are not keen about the TestCase atribute you could use the TestCaseSource attribute with an abstract base class.
public abstract class A
{
public class TestCases : List<TestCaseData>
{
public TestCases()
{
Add(new TestCaseData(1));
Add(new TestCaseData(2));
}
}
public abstract void Test(int i);
}
[TestFixture]
public class B : A
{
[Test]
[TestCaseSource(typeof(TestCases))]
public override void Test(int i)
{
Assert.That(i, Is.GreaterThan(0));
}
}
[TestFixture]
public class C : A
{
[Test]
[TestCaseSource(typeof(TestCases))]
public override void Test(int i)
{
Assert.That(i, Is.LessThan(10));
}
}

Related

Bring object through interface in order to mock it

I am at a somewhat of a crossroads with the following issue. I have, let's say, the following class
public class Foo
{
internal Bar bar { get; }
internal Foo(Bar bar)
{
this.bar = bar;
}
}
and I am trying to a Unit test of it using XUnit and Moq. The initial test I created was
[Theory]
[AutoMockData]
internal void CtorShouldCreateInstance(Bar bar)
{
var sut = new Foo(bar);
Assert.NotNull(sut);
}
which was a valid unit test, but by passing Bar as a parameter it bring all it's dependencies making it as a concrete type. The suggestion from my colleague was to bring the Bar object through an Interface and Mock the interface but I am not sure how to do it.
I've thought of making the interface, a method to CreateBar with its required parameters, inherit the interface to the Foo class, implement the method and then add it to the unit test, but I've wanted to get a clarification or approval as I am not sure this is the correct way.
Thanks in advance for all the help!
You have to derive Bar from IBar, so then you can Mock it easily with Mocking framework like FakeItEasy, here is an example :
public interface IBar
{
string SayHello();
}
public class Bar : IBar
{
public string SayHello()
{
// A complex work to retrieve data from external resource
return "Hello";
}
}
public class Foo
{
private readonly IBar _bar;
public Foo(IBar bar)
{
_bar = bar;
}
public string Greeting()
{
return _bar.SayHello();
}
}
Now you can mock IBar to return your desired result :
[TestFixture]
public class FooTests
{
[Test]
public void Bar_ShouldReturn_HiInsteadOfHello()
{
IBar fakeBar = A.Fake<IBar>();
A.CallTo(() => fakeBar.SayHello()).Returns("Hi");
Foo sut = new Foo(fakeBar);
Assert.AreEqual(sut.Greeting(), "Hi");
}
}
* Side note: You can also Mock your method directly without deriving that from an interface by making that virtual, but as a best practice it's better to use interfaces instead :
public class Bar
{
public virtual string SayHello()
{
// A complex work to retrieve data from external resource
return "Hello";
}
}
public class Foo
{
private readonly Bar _bar;
public Foo(Bar bar)
{
_bar = bar;
}
public string Greeting()
{
return _bar.SayHello();
}
}
[TestFixture]
public class FooTests
{
[Test]
public void Bar_ShouldReturn_HiInsteadOfHello()
{
Bar fakeBar = A.Fake<Bar>();
A.CallTo(() => fakeBar.SayHello()).Returns("Hi");
Foo sut = new Foo(fakeBar);
Assert.AreEqual(sut.Greeting(), "Hi");
}
}
Apparently all I had to do was to pass the IBar interface as the parameter instead of the normal Bar class, because AutoMockData took care of everything.
So yeah the answer was to make the Foo class
public class Foo
{
internal IBar bar { get; }
internal Foo(Bar bar)
{
this.bar = bar;
}
}
and the test becomes
[Theory]
[AutoMockData]
internal void CtorShouldCreateInstance(IBar bar)
{
var sut = new Foo(bar);
Assert.NotNull(sut);
}
Thanks everyone for all the help!

C++ Way of handling multiple inheritance for interfaces

I am looking at refactoring a lot of code and have discussed a bit in relations to the best way of handling inheritance. Given the following three classes
class Listener_Interface {
public:
virtual void message(data& data);
}
class Timing_Interface {
public:
virtual void timerEvent(data& data);
}
class Action_Interface {
public:
virtual void action(data& data);
}
There is a need for a class to implement all these plus provide a some extra specifik methods.
Should I inherit like this:
class NewClass_Interface :
public Listener_Interface,
public Timing_Interface,
public Action_Interface {
public:
virtual void newMethod();
}
class NewClass : NewClass_Interface {
....
}
or
class NewClass_Interface {
public:
virtual void newMethod();
}
class NewClass :
public NewClass_Interface
public Listener_Interface,
public Timing_Interface,
public Action_Interface {
....
}
To me the previous seems more correct and easier to test etc. But for some reason all the classes and code looks lite the latter.
It depends on your logic. Sometimes you might want your NewClass_Interface to not necessarily have a relation with Listener, Timing and Action. But since it doesn't look like the case here, I agree with you. The better you constraint the use of your interfaces, the more reliable your code will be.
So I would go with this:
class Listener_Interface {
public:
virtual void message(data& data) = 0;
}
class Timing_Interface {
public:
virtual void timerEvent(data& data) = 0;
}
class Action_Interface {
public:
virtual void action(data& data) = 0;
}
(Observe how I make your methods pure virtual in order to make your classes real interfaces)
class NewClass_Interface :
public Listener_Interface,
public Timing_Interface,
public Action_Interface
{
public:
virtual void newMethod() = 0;
}
class NewClass : NewClass_Interface {
....
}
This way you'll have better control on what is going on.
Also, I would advise you of using the most common standard for Interfaces naming: IListener, ITiming, IAction and INewClass.

Assign mock object to protected member

class foo
{
// some functions which uses class member t
protected:
Test t;
};
Class Test
{
// some functions
}
and I mocked the class test and how to assign the mock object to class foo? because I am going to test foo class.
Does I undestand you right? Your productive code is in the class foo and it uses functionality which is provided by a class. In your case Test? Please use Dependency Injection to avoid such problems. Create an interface which Test is deriving from it. For example:
// Productive Code
class TestInterface {
virtual void TestMethod() = 0;
};
class ProductiveTest : public TestInterface {
...
}
class foo
{
public:
foo(TestInterface const& t) : t_(t) {}
// some functions which uses class member t
protected:
TestInterface& t_;
};
// Test Code
class Test : public TestInterface {
MOCK_METHOD0(TestMethod, void());
}
So you can test foo also in isolation.

Failing in Code coverage Test with a simple class constructor

I have a class:
public class SourceServerProvider : ISourceServerProvider
{
private readonly ISourceServer _sourceServer;
public SourceServerProvider()
:this(new SourceServer())
{ }
public SourceServerProvider(ISourceServer sourceServer)
{
_sourceServer = sourceServer;
}
}
MS code coverage test complaints to this block:
public SourceServerProvider()
:this(new SourceServer())
{ }
I don't know how to write a unit test for above block. Please advise..
I just tested it with the followig code:
public class SourceServerProvider : ISourceServerProvider
{
private readonly ISourceServer _sourceServer;
public SourceServerProvider()
: this(new SourceServer())
{ }
public SourceServerProvider(ISourceServer sourceServer)
{
_sourceServer = sourceServer;
}
}
public interface ISourceServer
{
}
public class SourceServer : ISourceServer
{
}
public interface ISourceServerProvider
{
}
and wrote this test
public class Class1
{
[Test]
public void test()
{
var a = new SourceServerProvider();
}
}
Code Coverage says it is fully covered:
so please add the result you are getting or create asimple test that call the default ctor

How do you unit test an interface?

For example, there is a interface IMyInterface, and three classes support this interface:
class A : IMyInterface
{
}
class B : IMyInterface
{
}
class C : IMyInterface
{
}
In the simplest way, I could write three test class : ATest, BTest, CTest and test them separately. However, since they support the same interface, most test code would be the same, it's hard to maintain. How can I use a simple and easy way to test a interface that is supported by different class?
(previously asked on the MSDN forums)
If you want to run the same tests against different implementers of your interface using NUnit as an example:
public interface IMyInterface {}
class A : IMyInterface { }
class B : IMyInterface { }
class C : IMyInterface { }
public abstract class BaseTest
{
protected abstract IMyInterface CreateInstance();
[Test]
public void Test1()
{
IMyInterface instance = CreateInstance();
//Do some testing on the instance...
}
//And some more tests.
}
[TestFixture]
public class ClassATests : BaseTest
{
protected override IMyInterface CreateInstance()
{
return new A();
}
[Test]
public void TestCaseJustForA()
{
IMyInterface instance = CreateInstance();
//Do some testing on the instance...
}
}
[TestFixture]
public class ClassBTests : BaseTest
{
protected override IMyInterface CreateInstance()
{
return new B();
}
}
[TestFixture]
public class ClassCTests : BaseTest
{
protected override IMyInterface CreateInstance()
{
return new C();
}
}
To test an interface with common tests regardless of implementation, you can use an abstract test case, and then create concrete instances of the test case for each implementation of the interface.
The abstract (base) test case performs the implementation-neutral tests (i.e. verify the interface contract) while the concrete tests take care of instantiating the object to test, and perform any implementation-specific tests.
Could create methods that take a parameter of type IMyInterface and have the actual test methods just call those methods passing in different concrete classes.
You do not test the interface directly, but you may write an abstract class that tests the contract a particular implementation should extend. A test of a concrete class would then extend the abstract class
If you're using NUnit, then you could use Grensesnitt:
public interface ICanAdd {
int Add(int i, int j); //dont ask me why you want different adders
}
public class winefoo : ICanAdd {
public int Add(int i, int j)
{
return i + j;
}
}
interface winebar : ICanAdd {
void FooBar() ;
}
public class Adder1 : winebar {
public int Add(int i, int j) {
return i + j;
}
public void FooBar() {}
}
public class Adder2 : ICanAdd {
public int Add(int i, int j) {
return (i + 12) + (j - 12 ); //yeeeeeaaaah
}
}
[InterfaceSpecification]
public class WithOtherPlugins : AppliesToAll<ICanAdd>
{
[TestCase(1, 2, 3)]
[TestCase(-1, 2, 1)]
[TestCase(0, 0, 0)]
public void CanAddOrSomething(int x, int y, int r)
{
Assert.AreEqual(subject.Add(x, y), r);
}
[TestCase(1, 2, Result = 3)]
[TestCase(-1, 2, Result = 1)]
[TestCase(0, 0, Result = 0)]
public int CannAddOrSomethingWithReturn(int x, int y) {
return subject.Add(x, y);
}
}