Java 如何使用工厂在TestNG中只包含特定方法
使用TestNG,可以在TestNG xml文件中包括/排除特定方法,如下所示:Java 如何使用工厂在TestNG中只包含特定方法,java,xml,testng,Java,Xml,Testng,使用TestNG,可以在TestNG xml文件中包括/排除特定方法,如下所示: <classes> <class name="WebTest"> <methods> <include name="testMethod1"/> </methods> </class> </classes> <classes> <class name="WebTestFac
<classes>
<class name="WebTest">
<methods>
<include name="testMethod1"/>
</methods>
</class>
</classes>
<classes>
<class name="WebTestFactory" />
</classes
问题是,当使用工厂时,TestNG运行类WebTest
的所有带注释的方法。我想做的是这样的:
<classes>
<class name="WebTestFactory">
<methods>
<include name="testMethod1"/>
</methods>
</class>
</classes>
其中testMethod1
是属于类WebTest
的方法,其中WebTest
的实例由WebTestFactory
返回
但是,每当我尝试此测试时,NG都会抱怨WebTestFactory
不包括testMethod1
当将工厂与TestNG一起使用时,是否有办法在XML文件中指定方法包含/排除?因为我知道不可能从工厂中筛选方法。当在同一类中混合工厂方法和测试方法时,使用Include/Exclude。 我打开了一张关于需求的票:请随意评论
作为一种变通方法,您可以尝试实现一种将根据您的业务逻辑(例如,系统属性)禁用测试方法的方法 据我所知,从工厂筛选方法是不可能的。当在同一类中混合工厂方法和测试方法时,使用Include/Exclude。 我打开了一张关于需求的票:请随意评论 作为一种变通方法,您可以尝试实现一种将根据您的业务逻辑(例如,系统属性)禁用测试方法的方法
遵循以下步骤
遵循以下步骤
我的目标有些不同,但也许它也能帮助解决你的问题。我想在一个新的类实例中运行每个测试方法。其主要思想是,使用方法拦截器,您可以定义从没有依赖项的方法运行哪个方法。请注意,您可以通过上下文将变量从数据提供程序传递到拦截器。也许您可以使用它来定义要运行的方法。下面是我如何实现它的:
testng.xml
<test name="test">
<groups>
<run>
<include name="g1"/>
</run>
</groups>
<classes>
<class name="ParallelTest">
<methods>
</methods>
</class>
</classes>
</test>
}
拦截器
public class MethodInterceptor implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<String> methodNamesToRun = new ArrayList<>(methods.size());
List<Object> classInstancesToRun = new ArrayList<>(methods.size());
List<IMethodInstance> result = new ArrayList<>();
for (IMethodInstance methodInstance : methods) {
Object classInstance = methodInstance.getMethod().getInstance();
String methodName = methodInstance.getMethod().getQualifiedName();
if (!classInstancesToRun.contains(classInstance) && !methodNamesToRun.contains(methodName)) {
result.add(methodInstance);
classInstancesToRun.add(classInstance);
methodNamesToRun.add(methodName);
}
}
context.getAttribute("testToRun");
return result;
}
我的目标有些不同,但也许它也能帮助解决你的问题。我想在一个新的类实例中运行每个测试方法。其主要思想是,使用方法拦截器,您可以定义从没有依赖项的方法运行哪个方法。请注意,您可以通过上下文将变量从数据提供程序传递到拦截器。也许您可以使用它来定义要运行的方法。下面是我如何实现它的: testng.xml
<test name="test">
<groups>
<run>
<include name="g1"/>
</run>
</groups>
<classes>
<class name="ParallelTest">
<methods>
</methods>
</class>
</classes>
</test>
}
拦截器
public class MethodInterceptor implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<String> methodNamesToRun = new ArrayList<>(methods.size());
List<Object> classInstancesToRun = new ArrayList<>(methods.size());
List<IMethodInstance> result = new ArrayList<>();
for (IMethodInstance methodInstance : methods) {
Object classInstance = methodInstance.getMethod().getInstance();
String methodName = methodInstance.getMethod().getQualifiedName();
if (!classInstancesToRun.contains(classInstance) && !methodNamesToRun.contains(methodName)) {
result.add(methodInstance);
classInstancesToRun.add(classInstance);
methodNamesToRun.add(methodName);
}
}
context.getAttribute("testToRun");
return result;
}
将
@Factory
符号放在具有数据提供者
属性的测试类的构造函数上,如下所示
public class TestFoo
{
static final String TEST_NAMES = "testNames";
@Nonnull
private final String testName;
@Factory(dataProvider = TEST_NAMES)
public TestFoo(@Nonnull String testName)
{
this.testName = testName;
}
@DataProvider(name = TEST_NAMES)
public static Object[][] testNames()
{
Object[][] names = new Object[][]{{"Foo"}, {"Bar"}, {"Baz"}};
return names;
}
@Test
public void nameTest()
{
System.out.println("Name = " + testName);
Assert.assertFalse(testName.isBlank());
}
}
输出:
Name = Bar
Name = Foo
Name = Baz
===============================================
Default Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
将
@Factory
符号放在具有数据提供者
属性的测试类的构造函数上,如下所示
public class TestFoo
{
static final String TEST_NAMES = "testNames";
@Nonnull
private final String testName;
@Factory(dataProvider = TEST_NAMES)
public TestFoo(@Nonnull String testName)
{
this.testName = testName;
}
@DataProvider(name = TEST_NAMES)
public static Object[][] testNames()
{
Object[][] names = new Object[][]{{"Foo"}, {"Bar"}, {"Baz"}};
return names;
}
@Test
public void nameTest()
{
System.out.println("Name = " + testName);
Assert.assertFalse(testName.isBlank());
}
}
输出:
Name = Bar
Name = Foo
Name = Baz
===============================================
Default Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
感谢您的回复,以及本期的开篇!现在,某种sort+方法拦截器的配置文件似乎是一个不错的选择。感谢您的回复,以及打开这个问题!现在,某种sort+方法拦截器的配置文件似乎是一个不错的选择。真正不鼓励只使用代码的答案。为了帮助未来的读者,请解释一下你正在做什么!这个答案并没有说明如何将其与工厂相结合。只使用代码的答案是不被鼓励的。为了帮助未来的读者,请解释一下你正在做什么!这个答案并没有说明如何将其与工厂相结合。