[英]Execute tests sequentially in TestNG
I have multiple test classes, which should be executed sequentially. 我有多个测试类,应该按顺序执行。 I created
testng.xml
file with following content. 我创建了包含以下内容的
testng.xml
文件。
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MyTestSuite1" verbose="2" parallel="methods" thread-count="1">
<listeners>
<listener class-name="utils.TestNGListener"></listener>
</listeners>
<test name="Regression" parallel="false" verbose="2">
<classes>
<class name="test.LoginTest" />
<class name="test.ClearTest" />
<class name="test.SendMessageTest" />
</classes>
</test>
</suite>
I created main()
method for project to provide entry point. 我为项目创建了
main()
方法以提供入口点。
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
TestNG testNG = new TestNG();
String xmlFileName = "testng.xml";
List<XmlSuite> suite = (List<XmlSuite>)(new Parser(xmlFileName).parse());
testNG.setXmlSuites(suite);
testNG.run();
}
I am not sure, how to execute test suite in specified order, got error message. 我不确定,如何按指定顺序执行测试套件,得到错误信息。
Exception in thread "main" org.testng.TestNGException: Cannot find class in classpath: test.LoginTest
线程“main”中的异常org.testng.TestNGException:在类路径中找不到类:test.LoginTest
Output of tree
command: 输出
tree
命令:
C:.
├───.idea
│ └───libraries
├───META-INF
├───out
│ └───artifacts
├───resources
│ └───leanftjar
├───RunResults
│ └───Resources
│ ├───Snapshots
│ └───User
├───src
│ ├───main
│ │ ├───java
│ │ │ ├───hu
│ │ │ │ └───mysoft
│ │ │ ├───jar
│ │ │ │ └───META-INF
│ │ │ ├───META-INF
│ │ │ ├───unittesting
│ │ │ └───utils
│ │ └───resources
│ └───test
│ └───java
│ └───test
├───target
│ ├───classes
│ │ ├───hu
│ │ │ └───mysoft
│ │ ├───leanftjar
│ │ ├───unittesting
│ │ └───utils
│ ├───generated-sources
│ │ └───annotations
│ ├───generated-test-sources
│ │ └───test-annotations
│ ├───maven-status
│ │ └───maven-compiler-plugin
│ │ └───compile
│ │ └───default-compile
│ └───test-classes
│ └───test
└───test-output
├───All Test Suite
├───junitreports
└───old
└───All Test Suite
The problem is in your code. 问题出在您的代码中。 By default classes that reside under
src/main/java
don't have visibility into classes that reside in src/test/java
. 默认情况下,驻留在
src/main/java
下的类不能看到驻留在src/test/java
。 So when you create the TestNG instance in your main() method from src/main/java
TestNG is trying to load classes from the same and since it cannot find them it's throwing the exception. 因此,当您在main()方法中从
src/main/java
创建TestNG实例时,TestNG正在尝试从同一个类加载类,因为它无法找到它们,它会抛出异常。
To fix this problem please move the class that contains your main() method into a package under src/test/java
and try again. 要解决此问题,请将包含main()方法的类移到
src/test/java
下的包中,然后重试。 It will work. 它会工作。
In order to execute TestNG tests you should mark test method with org.testng.annotations.Test
class 要执行TestNG测试,您应该使用
org.testng.annotations.Test
类标记测试方法
@Test
public void testMyMethod() {
For using testng.xml
see example 有关使用
testng.xml
请参阅示例
// 1. To run with testng.xml file, uncomment this one, comment 2 testng.setTestSuites(Arrays.asList("testng.xml"));
For executing with tests order use setPreserveOrder
: 要使用测试顺序执行,请使用
setPreserveOrder
:
testng.setPreserveOrder(true);
If it is a hard requirement to run these steps in order, for your test to pass, it seems that they should be implemented internally in the test. 如果按顺序运行这些步骤是一项艰难的要求,为了让您的测试通过,似乎应该在测试内部实现它们。
@Test
public void doIt() {
login();
clearMessage();
sendMessage();
}
With the appropriate checks for success is going to fail as a unit, not hide the actions from the test maintainers, and provide better debugging output in the event of a test failure than "three tests stitched together as one". 通过适当的成功检查将作为一个单元失败,不会隐藏测试维护人员的操作,并且在测试失败的情况下提供比“将三个测试组合在一起”更好的调试输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.