引入依赖

  1. <!-- https://mvnrepository.com/artifact/org.testng/testng -->
  2. <dependency>
  3. <groupId>org.testng</groupId>
  4. <artifactId>testng</artifactId>
  5. <version>6.14.2</version>
  6. <!--<scope>test</scope>-->
  7. </dependency>

创建文件夹定义

在src/resources/下创建testNG目录:存放测试集合的目录,可根据测试模块创建对应模块文件夹,每个文件夹可以是独立模块,每个模块下可以有模块的测试集合。
src/resources/testNG/testSuites/demo-testSuite.xml

  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  2. <suite name="demo-测试" verbose="1" preserve-order="true">
  3. <test name="deomoTest-case1" preserve-order="true">
  4. <classes>
  5. <class name="testModules.demoTest">
  6. <methods>
  7. <include name="case1"/>
  8. <include name="case2"/>
  9. </methods>
  10. </class>
  11. </classes>
  12. </test>
  13. </suite>

为了能够让所有接口统一运行测试,需建立一个所有的测试集合,测试集合一般放在src/resources/testNg 目录下
src/resources/testNG/testSuiteAll.xml

  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  2. <suite name="接口测试集合" verbose="1" preserve-order="true">
  3. <suite-files>
  4. <suite-file path="testSuites/demo-testSuite.xml"/>
  5. </suite-files>
  6. </suite>

在test/java/testModules目录下创建测试类demoTest.java

  1. package testModules;
  2. import org.testng.annotations.Test;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Properties;
  6. public class demoTest {
  7. private static Properties properties;
  8. @Test(description = "测试1")
  9. public void case1() throws IOException {
  10. InputStream stream = this.getClass().getClassLoader().getResourceAsStream("env.properties");
  11. properties = new Properties();
  12. properties.load(stream);
  13. String env = properties.getProperty("environment");
  14. System.out.println(env);
  15. String host = properties.getProperty("api.host");
  16. System.out.println(host);
  17. }
  18. @Test(description = "测试2")
  19. public void case2() throws IOException {
  20. InputStream stream = this.getClass().getClassLoader().getResourceAsStream("not.found");
  21. properties = new Properties();
  22. properties.load(stream);
  23. }
  24. }