如何使用两种不同的设置运行一组 nUnit 测试? [英] How do I run a set of nUnit tests with two different setups?
问题描述
(抱歉标题不清晰,如果有更好的请编辑)
(Sorry for the unclear title, please edit it if you can come up with a better one)
我希望在两个不同的数据存储上运行相同的测试,我可以在 Setup() 方法中创建数据存储.
I wish to run the same tests over two different data stores, I can create the data stores in the Setup() method.
那么我是否应该有一个包含所有测试的超类和一个抽象的 SetUp()
方法,然后为每个数据存储创建一个子类?
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?
或者有更好的方法吗?
参见不区分大小写的字符串与 linq-to-sql 和 linq-to-objects 进行比较"以了解我正在测试的内容.
See "Case insensitive string compare with linq-to-sql and linq-to-objects" for what I am testing.
推荐答案
一个简单的解决方案是这样的.
A simple solution is this.
您所有的测试用例都在一个抽象类中,例如在 TestBase 类中.例如:
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
}
然后为每个设置创建两个子类.因此,每个子类都将使用 it-setup 方法以及所有继承的测试方法单独运行.
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
这篇关于如何使用两种不同的设置运行一组 nUnit 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!