在 TestNG 中执行测试
Execution of tests in TestNG
我有 2 个测试需要 运行 接连进行几次。
想要的场景是:"first test"、"second test"、"first test"、"second test"等等...
实际场景是:"first test", "first test", "second test", "second test".
@Test (priority = 1, invocationCount = 3)
public void first() {
System.out.println("first test");
}
@Test (priority = 2, invocationCount = 3)
public void second() {
System.out.println("second test");
}
如何实现我想要的场景?
这里的另一个要求是,在第一次测试时,Android phone 应该是第一个设备,iOS phone 应该是第二个。
在第二次测试中,iOS phone 应该是第一个设备,Android 应该是第二个。
所以这意味着我需要使用不同的 xml 文件。
<test name="TwoDevices - ios first">
<parameter name="appName" value="App2"/>
<parameter name ="device" value="IOS/iphone6_plus"/>
<parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
<classes>
<class name="com.TestFactory"/>
</classes>
</test>
尝试使用 @AfterMethod
注释
@Test (invocationCount=3)
public void first() {
System.out.println("first test");
}
@AfterMethod
public void second() {
System.out.println("second test");
}
使用 depends on method
属性 的 TestNG:
@Test (priority=1,invocationCount = 3)
public void first() {
System.out.println("first test");
}
@Test (priority=2,invocationCount = 3, dependsOnMethods = { "first" })
public void second() {
System.out.println("second test");
}
你可以使用a factory和运行它(而不是测试class):
public class TestFactory {
@Factory
public static Object[] createInstances() {
int number = 3;
Object[] result = new Object[number];
for (int i = 0; i < number; i++) {
result[i] = new Test();
}
return result;
}
}
public class Test {
@Test
public void first() {
System.out.println("first test");
}
@Test(dependsOnMethods="first")
public void second() {
System.out.println("second test");
}
}
我更改 xml 文件后问题解决了:
<test name="TwoDevices - ios first">
<parameter name="appName" value="App2"/>
<parameter name ="device" value="IOS/iphone6_plus"/>
<parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
<classes>
<class name="com.MyDebug">
<methods>
<include name="first"></include>
</methods>
</class>
</classes>
<test name="TwoDevices - ANDROID first">
<parameter name="appName" value="App2"/>
<parameter name="device" value="ANDROID/lg4_v5"/>
<parameter name="secondDevice" value="IOS/iphone6_plus"/>
<classes>
<class name="com.MyDebug">
<methods>
<include name="second"></include>
</methods>
</class>
</classes>
发生的情况是 class 中只有一个方法运行。第一次执行后,运行第二个方法。
我有 2 个测试需要 运行 接连进行几次。
想要的场景是:"first test"、"second test"、"first test"、"second test"等等...
实际场景是:"first test", "first test", "second test", "second test".
@Test (priority = 1, invocationCount = 3)
public void first() {
System.out.println("first test");
}
@Test (priority = 2, invocationCount = 3)
public void second() {
System.out.println("second test");
}
如何实现我想要的场景?
这里的另一个要求是,在第一次测试时,Android phone 应该是第一个设备,iOS phone 应该是第二个。 在第二次测试中,iOS phone 应该是第一个设备,Android 应该是第二个。 所以这意味着我需要使用不同的 xml 文件。
<test name="TwoDevices - ios first">
<parameter name="appName" value="App2"/>
<parameter name ="device" value="IOS/iphone6_plus"/>
<parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
<classes>
<class name="com.TestFactory"/>
</classes>
</test>
尝试使用 @AfterMethod
注释
@Test (invocationCount=3)
public void first() {
System.out.println("first test");
}
@AfterMethod
public void second() {
System.out.println("second test");
}
使用 depends on method
属性 的 TestNG:
@Test (priority=1,invocationCount = 3)
public void first() {
System.out.println("first test");
}
@Test (priority=2,invocationCount = 3, dependsOnMethods = { "first" })
public void second() {
System.out.println("second test");
}
你可以使用a factory和运行它(而不是测试class):
public class TestFactory {
@Factory
public static Object[] createInstances() {
int number = 3;
Object[] result = new Object[number];
for (int i = 0; i < number; i++) {
result[i] = new Test();
}
return result;
}
}
public class Test {
@Test
public void first() {
System.out.println("first test");
}
@Test(dependsOnMethods="first")
public void second() {
System.out.println("second test");
}
}
我更改 xml 文件后问题解决了:
<test name="TwoDevices - ios first">
<parameter name="appName" value="App2"/>
<parameter name ="device" value="IOS/iphone6_plus"/>
<parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
<classes>
<class name="com.MyDebug">
<methods>
<include name="first"></include>
</methods>
</class>
</classes>
<test name="TwoDevices - ANDROID first">
<parameter name="appName" value="App2"/>
<parameter name="device" value="ANDROID/lg4_v5"/>
<parameter name="secondDevice" value="IOS/iphone6_plus"/>
<classes>
<class name="com.MyDebug">
<methods>
<include name="second"></include>
</methods>
</class>
</classes>
发生的情况是 class 中只有一个方法运行。第一次执行后,运行第二个方法。