Test Suite is a collection of multiple test cases. In testNG the test suite is defined in the tag of the XML file executed <suite>.
In testNG, create testng.xml (file name can be named according to their own favorite, often named testng.xml), can be configured in xml test run according resistant items, excluding items, test methods and test class, and priority, etc. .
step:
First, create testng.xml, resources right in the idea of new-> Create a file in the file test.xml
Second, the configuration testng.xml
Suite in xml tag <suite>, which is the tab Test <test>, then the tag Classes <classes> class tag <class>. suite and test labels must provide the name attribute values, specific values can be named according to their own needs, class need the correct use of the label name you need to perform the correct class name in the package path testing.
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <suite name="test"> 3 <test name="login"> 4 <!-- 配置需要运行的class--> 5 <classes> 6 <class name="com.course.testng.suite.LoginTest"/> 7 <class name="com.course.testng.suite.SuiteConfig"/> 8 9 </classes> 10 </test> 11 <test name="pay"> 12 <!-- 配置需要运行的class--> 13 <classes> 14 <class name="com.course.testng.suite.SuiteConfig"/> 15 <class name="com.course.testng.suite.PayTest"/> 16 </classes> 17 </test> 18 </suite>
Third, the implementation testng.xml
On the xml file, you can run testng.xml right.
Test code
1 package com.course.testng.suite; 2 3 import org.testng.annotations.Test; 4 5 public class LoginTest { 6 @Test 7 public void loginTaoBao() { 8 9 System.out.println("登录淘宝"); 10 } 11 12 }
1 package com.course.testng.suite; 2 3 import org.testng.annotations.Test; 4 5 public class PayTest { 6 @Test 7 public void pay() { 8 System.out.println("支付成功"); 9 } 10 }
1 package com.course.testng.suite; 2 3 import org.testng.annotations.*; 4 5 public class SuiteConfig { 6 7 @BeforeSuite 8 public void beforeSuite() { 9 System.out.println("BeforeSuitec测试套件"); 10 } 11 12 @AfterSuite 13 public void afterSuite() { 14 System.out.println("AfterSuite测试套件"); 15 } 16 17 @BeforeClass 18 is public void beforClass () { . 19 System.out.println ( "method which is based BeforeClass before running" ); 20 is 21 is } 22 is 23 is @AfterClass 24 public void AfterClass () { 25 System.out.println ( "Class AfterClass method after running " ); 26 is } 27 28 @BeforeTest 29 public void beforTest () { 30 System.out.println (" BeforeTest method which is run before the test " ); 31 is 32 } 33 is 34 is @AfterTest 35 public void afterTest () { 36 System.out.println ( "BeforeTest operating method after the test" ); 37 } 38 is 39 40 }
Results of the
. 1 Files \ Workspace \ muke \ AotoTest \ chapter5 \ the src \ main \ Resources \ suite.xml 2 BeforeSuitec test suite . 3 . 4 BeforeTest this method of operation before the test . 5 . 6 Taobao log . 7 . 8 method BeforeTest after running the test . 9 10 BeforeTest this is the test method was run before . 11 12 is successful payment 13 is 14 BeforeTest method operates after the test 15 16 . 17 AfterSuite test suite 18 is . 19 20 is =================== ============================ 21 is Test 22 is the Total Tests RUN: 2, the Failures: 0, Skips: 0 23 is =============================================== 24 25 26 Process finished with exit code 0