I want to run tests in a specific order from xml file. First, I need to test login feature, then others. I have TestRunner class and testng.xml file. But when I run tests through TestRunner they run randomly, even if tests have dependsOnGroups attribute. Here is my code:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="tests" thread-count="2">
<listeners>
<listener class-name="com.epam.tat.framework.listeners.SuiteListener"/>
<listener class-name="com.epam.tat.framework.listeners.TestListener"/>
</listeners>
<test name="test1">
<classes>
<class name="com.epam.tat.framework.mail.test.MailLoginTest"/>
<class name="com.epam.tat.framework.mail.test.MailRuTest" />
</classes>
</test>
<test name="test2">
<classes>
<class name="com.epam.tat.framework.cloud.test.CloudLoginTest"/>
<class name="com.epam.tat.framework.cloud.test.CloudTest"/>
</classes>
</test>
</suite>
答案 0 :(得分:1)
查看 TestNG 对invoking in a certain order
的评论。
有时,您需要按特定顺序调用测试方法。请参阅下面的示例:
在运行更多测试方法之前,确保已完成并成功执行一定数量的测试方法。
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
在此示例中, method1 ()声明为依赖于方法serverStartedOk(),这保证始终首先调用serverStartedOk()。
更多参考:TestNG_dependencies
答案 1 :(得分:1)
正如@cruisepandet所说,但是你可以引入群组,所以可以在同一群体下解决多个测试,例如下面的例子:
Class
然后xml看起来像这样:
import org.testng.Assert;
import org.testng.annotations.Test;
public class GroupTestExample {
String message = ".com";
MessageUtil messageUtil = new MessageUtil(message);
@Test(groups = { "functest", "checkintest" })
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = ".com";
Assert.assertEquals(message, messageUtil.printMessage());
}
@Test(groups = { "checkintest" })
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "test" + ".com";
Assert.assertEquals(message, messageUtil.salutationMessage());
}
@Test(groups = { "functest" })
public void testingExitMessage() {
System.out.println("Inside testExitMessage()");
message = "www." + "test "+".com";
Assert.assertEquals(message, messageUtil.exitMessage());
}
}
您可以使用包含/排除组并添加一个或多个类。
如果您想要更精细的粒度,请在测试用例运行中订购,为每个测试添加优先级,如下所示:
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<groups>
<run>
<include name = "functest"/>
</run>
</groups>
<classes>
<class name = "GroupTestExample" />
</classes>
</test>
<test name = "test2">
<groups>
<run>
<include name = "checkintest"/>
</run>
</groups>
<classes>
<class name = "GroupTestExample" />
</classes>
</test>
</suite>
所以这只是示例代码的一部分,需要根据您的需要进行调整。但基本上这是做这件事的工具。