[英]TestNG dataproviders with a @BeforeClass
I am trying to run a class with multiple tests under two different conditions.我正在尝试在两种不同的条件下运行具有多个测试的类。 Basically I have a bunch of tests related to a search.
基本上我有一堆与搜索相关的测试。 I am adding new functionality of a new search strategy, and in the meantime want to run the already written tests under both configurations.
我正在添加新搜索策略的新功能,同时希望在两种配置下运行已经编写的测试。 As we have multiple classes each with multiple tests I want to streamline this process as much as possible.
因为我们有多个类,每个类都有多个测试,所以我想尽可能地简化这个过程。 Ideally it'd be great to do the setup in a @BeforeClass with a data provider so that all tests in the class are basically run twice under the different configurations, but doesn't look like this is possible.
理想情况下,最好在带有数据提供程序的 @BeforeClass 中进行设置,以便类中的所有测试基本上在不同的配置下运行两次,但看起来这是不可能的。
Right now I have:现在我有:
public class SearchTest1 {
@Test(dataProvider = "SearchType")
public void test1(SearchType searchType) {
setSearchType(searchType);
//Do the test1 logic
}
@Test(dataProvider = "SearchType")
public void test2(SearchType searchType) {
setSearchType(searchType);
//Do the test2 logic
}
@DataProvider(name = "SearchType")
public Object[][] createData() {
return new Object[][]{
new Object[] {SearchType.scheme1, SearchType.scheme2}
}
}
}
Is there a better way to do this?有一个更好的方法吗?
If you want to avoid having to annotate each and every method with the data provider, you can use a Factory
instead.如果您想避免使用数据提供程序对每个方法进行注释,您可以改用
Factory
。
public class SearchTest1 {
private final SearchType searchType;
public SearchTest1( SearchType searchType ) {
this.searchType = searchType;
}
@Test
public void test2() {
//Do the test2 logic
}
...
}
And your factory class will be:你的工厂类将是:
public class SearchTestFactory {
@Factory
public Object [] createInstances() {
return new Object[] { new SeartchTest1( SearchType.ONE ), new SearchTest1( SearchType.TWO ) };
}
}
See more on this here .在此处查看更多信息。
Then you can either have one factory that enumerates every test class or a separate factory for each, the first one is obviously less flexible, the second one means slightly more code.然后你可以有一个工厂来枚举每个测试类,也可以有一个单独的工厂,第一个显然不太灵活,第二个意味着代码稍微多一些。
You can use parameters in @BeforeClass.您可以在@BeforeClass 中使用参数。 Just use (with some cleanup)
只需使用(进行一些清理)
context.getCurrentXmlTest().getParameters()
context.getCurrentXmlTest().getParameters()
@SuppressWarnings("deprecation")
@BeforeClass
public void setUp(ITestContext context) {
System.out.println(context.getCurrentXmlTest().getAllParameters());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.