zoukankan      html  css  js  c++  java
  • TestNG – Run multiple test classes (suite test)

    In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test.

    1. Test Classes

    Review following three test classes.

    TestConfig.java
    package com.mkyong.testng.examples.suite;
    
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.BeforeTest;
    
    //show the use of @BeforeSuite and @BeforeTest
    public class TestConfig {
    
    	@BeforeSuite
    	public void testBeforeSuite() {
    		System.out.println("testBeforeSuite()");
    	}
    
    	@AfterSuite
    	public void testAfterSuite() {
    		System.out.println("testAfterSuite()");
    	}
    
    	@BeforeTest
    	public void testBeforeTest() {
    		System.out.println("testBeforeTest()");
    	}
    
    	@AfterTest
    	public void testAfterTest() {
    		System.out.println("testAfterTest()");
    	}
    
    }
    
    TestDatabase.java
    package com.mkyong.testng.examples.suite;
    
    import org.testng.annotations.Test;
    
    public class TestDatabase {
    
    	@Test(groups = "db")
    	public void testConnectOracle() {
    		System.out.println("testConnectOracle()");
    	}
    
    	@Test(groups = "db")
    	public void testConnectMsSQL() {
    		System.out.println("testConnectMsSQL");
    	}
    
    	@Test(groups = "db-nosql")
    	public void testConnectMongoDB() {
    		System.out.println("testConnectMongoDB");
    	}
    
    	@Test(groups = { "db", "brokenTests" })
    	public void testConnectMySQL() {
    		System.out.println("testConnectMySQL");
    	}
    
    }
    
    TestOrder.java
    package com.mkyong.testng.examples.suite;
    
    import org.testng.annotations.Test;
     
    public class TestOrder {
     
    	@Test(groups={"orderBo", "save"})
    	public void testMakeOrder() {  
    	  System.out.println("testMakeOrder");
    	}  
     
    	@Test(groups={"orderBo", "save"})
    	public void testMakeEmptyOrder() {  
    	  System.out.println("testMakeEmptyOrder");
    	}  
    	
    	@Test(groups="orderBo")
    	public void testUpdateOrder() {  
    		System.out.println("testUpdateOrder");
    	}  
     
    	@Test(groups="orderBo")
    	public void testFindOrder() {  
    		System.out.println("testFindOrder");
    	}  
    	
    }
    
     

    2. Testng.xml

    To run above test classes, create a XML file – testng.xml (can be any filename) file, and define detail like this :

    testng.xml
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestAll">
    
    	<test name="order">
    		<classes>
    			<class name="com.mkyong.testng.examples.suite.TestConfig" />
    			<class name="com.mkyong.testng.examples.suite.TestOrder" />
    		</classes>
    	</test>
    
    	<test name="database">  
    		<classes>
    			<class name="com.mkyong.testng.examples.suite.TestConfig" />
    			<class name="com.mkyong.testng.examples.suite.TestDatabase" />
    		</classes>
    	</test>
    
    </suite>
    

    Output

    [TestNG] Running:
      C:mkyong_projectsTestNGsrctest
    esources	estng-all.xml
    
    testBeforeSuite()
    
    testBeforeTest()
    testFindOrder
    testMakeEmptyOrder
    testMakeOrder
    testUpdateOrder
    testAfterTest()
    
    testBeforeTest()
    testConnectMongoDB
    testConnectMsSQL
    testConnectMySQL
    testConnectOracle()
    testAfterTest()
    
    testAfterSuite()
    
     

    3. Other Examples

    Here are some common use examples.

    3.1 Specify package names instead of class names:

    testng.xml
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestAll">
    
    	<test name="order">
    		<packages>
    			<package name="com.mkyong.testng.examples.suite.*" />
    		</packages>
    	</test>
    
    </suite>
    

    3.2 Specify methods to include or exclude :

    testng.xml
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestAll">
    
      <test name="order">
    	<classes>
    		<class name="com.mkyong.testng.examples.suite.TestConfig" />
    		<class name="com.mkyong.testng.examples.suite.TestOrder">
    			<methods>
    				<include name="testMakeOrder" />
    				<include name="testUpdateOrder" />
    				<!-- 
    					<exclude name="testMakeOrder" />
    				 -->
    			</methods>
    		</class>
    	</classes>
      </test>
     
    </suite>
    

    Output

    [TestNG] Running:
      C:mkyong_projectsTestNGsrctest
    esources	estng.xml
    
    testBeforeSuite()
    testBeforeTest()
    testMakeOrder
    testUpdateOrder
    testAfterTest()
    testAfterSuite()
    

    3.3 Specify groups to include or exclude :

    testng.xml
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestAll">
    
      <test name="database">
    	<groups>
    		<run>
    			<exclude name="brokenTests" />
    			<include name="db" />
    		</run>
    	</groups>
      
    	<classes>
    		<class name="com.mkyong.testng.examples.suite.TestDatabase" />
    	</classes>
      </test>
     
    </suite>
    

    Output

    [TestNG] Running:
      C:mkyong_projectsTestNGsrctest
    esources	estng.xml
    
    testConnectMsSQL
    testConnectOracle()
    

     

    According to the TestNG dtd, the exclude element is only applicable to the following elements:

    • package - The package description within packages list.
    • methods - The list of methods to include/exclude from this test.
    • run - The subtag of groups used to define which groups should be run.

    The elements cl*** and class cannot be directly excluded; however, you can exclude cl*** through groups:

    @Test(groups = { "ClassTest1" })
    public class Test1 {
    
      public void testMethod1() {
      }
    
      public void testMethod2() {
      }
    
    }
    

    Then you will define the testng.xml:

    <suite>
      <test>
        <groups>
          <run>
            <exclude name="ClassTest1"/>
          </run>
        </groups>
        <cl***>
          <class name="Test1">
        </cl***>
      </test> 
    </suite>
  • 相关阅读:
    基于jQuery的六大表单向导插件
    oracle行转列(动态行转不定列)
    PLSql自动替换---辅助写代码
    ExcelReport第一篇:使用ExcelReport导出Excel
    改HTML5里的input标签的required属性的提示为英文的
    spring boot:用redis+lua实现表单接口的幂等性(spring boot 2.2.0)
    linux(centos8):centos8.1安装(详细过程/图解)(vmware fusion/CentOS-8.1.1911-x86_64)
    linux(centos8):配置docker的cgroup driver为systemd
    linux(centos8):禁用selinux(临时关闭/永久关闭)
    linux(centos8):firewalld对于请求会选择哪个zone处理?
  • 原文地址:https://www.cnblogs.com/melody-emma/p/4922424.html
Copyright © 2011-2022 走看看