(Sorry for the unclear title, please edit it if you can come up with a better one)
I wish to run the same tests over two different data stores, I can create the data stores in the Setup() method.
So should I have a super class that contains all the tests and an abstract SetUp()
method, then have a subclass for each data store?
Or is there a better way?
See "Case insensitive string compare with linq-to-sql and linq-to-objects" for what I am testing.
The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class.
Setup methods (both types) are called on base classes first, then on derived classes. If any setup method throws an exception, no further setups are called. Teardown methods (again, both types) are called on derived classes first, then on the base class.
SetUpAttribute (NUnit 2.0 / 2.5)This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. It is also used inside a SetUpFixture, where it provides the same functionality at the level of a namespace or assembly.
MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn't have direct integration into the native APIs like MsTest does.
A simple solution is this.
All your test-cases are in an abstract class for example in the TestBase-class. For example:
public abstract class TestBase
{
protected string SetupMethodWas = "";
[Test]
public void ExampleTest()
{
Console.Out.WriteLine(SetupMethodWas);
}
// other test-cases
}
Then you create two sub-classes for each setup. So each sub-class will be run a individual with it-setup method and also all inherited test-methods.
[TestFixture]
class TestA : TestBase
{
[SetUp]
public void Setup()
{
SetupMethodWas = "SetupOf-A";
}
}
[TestFixture]
class TestB : TestBase
{
[SetUp]
public void Setup()
{
SetupMethodWas = "TestB";
}
}
This works wonderful. However for simpler tests parameterized tests are a better solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With