简体   繁体   English

如何在多个测试中重用[RowTest]属性

[英]How to reuse [RowTest] attributes on several tests

There are 2 tests that take the same list of servers in using the Row() attribute 在使用Row()属性时,有2个测试采用相同的服务器列表

Is there a way to avoid duplicating all the Row attributes on both tests? 有没有一种方法可以避免在两个测试中都复制所有的Row属性?

[RowTest]
[Row("server1")]
[Row("server2")]
[Row("server3")]
public void Test1(string server)
{
    //try connecting on port
}

[RowTest]
[Row("server1")]
[Row("server2")]
[Row("server3")]
public void Test2(string server)
{
   //ping server
}

Yes. 是。 You can define a source for the test cases. 您可以定义测试用例的来源。 The source can be either a method, field or a property, may be static or instance. 源可以是方法,字段或属性,可以是静态或实例。 The requirement is that the source should return an IEnumberable or another type that implements it. 要求是源应返回IEnumberable或实现它的其他类型。 Of course the sources are reusable for multiple test methods which is your requirement. 当然,您可以将源重复用于多种测试方法。

You indicate the source for test cases by decorating the method with the TestCaseSourceAttribute . 您可以通过使用TestCaseSourceAttribute装饰方法来指示测试用例的源。 The snippet below is an example from the link I provided. 下面的代码段是我提供的链接中的一个示例。

static int[] EvenNumbers = new int[] { 2, 4, 6, 8 };

[Test, TestCaseSource("EvenNumbers")]
public void TestMethod(int num)
{
    Assert.IsTrue( num % 2 == 0 );
}

Also take into consideration that in the latest verion of NUnit, the is no Row or RowTest attribute, for more information visit this question: What happended to nunit extensions/rowtest? 还应考虑到在NUnit的最新版本中,no是Row或RowTest属性,有关更多信息,请访问以下问题: nunit扩展/行测试发生了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM