testNg使用详解

基本注解使用 对比 xml使用(以下作用相同)

  • 注解
public class CaseOne {

    @Test
    public void login(){
        System.out.println("登录");
    }

    @Test
    public void pay(){
        System.out.println("支付");
    }

    @BeforeTest
    public void beforeTest(){
        System.out.println("beforeTest----");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("beforeMethod----");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println("beforeClass----");
    }

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("beforeSuite----");
    }
}

运行结果

beforeSuite----
beforeTest----
beforeClass----
beforeMethod----
登录
beforeMethod----
支付
  • xml文件
public class suiteConfig {

    @BeforeTest
    public void beforeTest(){
        System.out.println("beforeTest----");
    }
    
    @BeforeSuite
    public void beforeSuite(){
        System.out.println("beforeSuite----");
    }
}
public class LoginTest {

    @Test
    public void LoginSuccess(){
        System.out.println("登录成功");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("beforeMethod----");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println("beforeClass----");
    }
}
public class PayTest {

    @Test
    public void paySuccess(){
        System.out.println("支付成功");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("beforeMethod++++");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println("beforeClass++++");
    }
}
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="login">
        <classes>
            <class name="com.it.cases.suite.suiteConfig"/>
            <class name="com.it.cases.suite.LoginTest"/>
        </classes>
    </test>

    <test verbose="2" preserve-order="true" name="pay">
        <classes>
            <class name="com.it.cases.suite.PayTest"/>
            <class name="com.it.cases.suite.suiteConfig"/>
        </classes>
    </test>
</suite>

运行结果

beforeSuite----
beforeTest----
beforeClass----
beforeMethod----
登录成功
beforeTest----
beforeClass++++
beforeMethod++++
支付成功

分组测试

  • 使用注解
public class GroupOnMethod {

    @Test(groups = {"groupA"})
    public void test1(){
        System.out.println("groupA中test1");
    }

    @Test(groups = {"groupA"})
    public void test2(){
        System.out.println("groupA中test2");
    }


    @Test(groups = {"groupB"})
    public void tes3(){
        System.out.println("groupB中test3");
    }


    @BeforeGroups({"groupA"})
    public void beforeGroups1(){
        System.out.println("beforeGroups1......");
    }


    @BeforeGroups({"groupB"})
    public void beforeGroups2(){
        System.out.println("beforeGroups2......");
    }
}
  • 使用xml
public class groupTest {

    @Test(groups = "login") // 也可以作用在类
    public void login(){
        System.out.println("登录");
    }

    @Test(groups = "pay")
    public void pay(){
        System.out.println("支付");
    }
}
<suite name="All Test Suite">
    <!--
        只运行组名称为pay的方法
    -->
    <test verbose="2" preserve-order="true" name="pay1">
        <groups>
            <run>
                <include name="pay"/>
            </run>
        </groups>
        <classes>
            <class name="com.it.cases.groups.groupTest"/>
        </classes>
    </test>
</suite>

运行结果

支付

参数化

  • 使用xml
public class paramTest {
    @Test
    @Parameters({"name","age"})
    public void test(String name, int age){
        System.out.println("name: "+name + "  age: "+age);
    }
}
<suite name="All Test Suite">
    <test name="param">
        <classes>
            <class name="com.it.cases.paramTest">
                <parameter name="name" value="zs"/>
                <parameter name="age" value="10"/>
            </class>
        </classes>
    </test>
</suite>

运行结果

name: zs  age: 10
  • 使用注解
public class DataProviderTest {

    /**
     * 基本使用
     */
    @Test(dataProvider = "data")
    public void test(String name, Integer age){
        System.out.println(name+age);
    }

    @DataProvider(name = "data")
    public Object[][] dataProvide(){

        Object[][] o = new Object[][]{
                {"zs",10},
                {"ls",20}
        };
        return o;
    }

    /**
     *条件化,使用反射
     */

    @Test(dataProvider = "DATA")
    public void test1(String name, Integer age){
        System.out.println(name+age);
    }

    @Test(dataProvider = "DATA")
    public void test2(String name, Integer age){
        System.out.println(name+age);
    }

    @DataProvider(name = "DATA")
    public Object[][] DATAProvide(Method method){  // 使用反射
        Object[][] o = null;
        if(method.getName().equals("test1")){
            o = new Object[][]{
                {"aa",10},
                {"bb",20}
            };
        }else if(method.getName().equals("test2")){
            o = new Object[][]{
                {"cc",10},
                {"dd",20}
            };
        }
        return o;
    }
}

依赖测试

public class DependTest {
    @Test
    public void test1(){
        System.out.println("test1");
        throw new RuntimeException();
    }

    @Test(dependsOnMethods = "test1")
    public void test2(){
        System.out.println("test2");
    }
}

异常测试

public class ExceptException {

    @Test(expectedExceptions = RuntimeException.class)
    public void runtimeException(){
        System.out.println("异常测试");
        throw new RuntimeException();
    }
}

超时测试

public class TimeOutTest {

    @Test(timeOut = 3000)
    public void testsuccess() throws InterruptedException {
        Thread.sleep(2000);
    }

    @Test(timeOut = 3000)
    public void testfaile() throws InterruptedException {
        Thread.sleep(4000);
    }
}

多线程测试

  • 使用注解
public class MutiThreadOnAnno {
    // 线程池=3,线程=5
    @Test(threadPoolSize = 3,invocationCount = 5)
    public void test(){
        System.out.println(Thread.currentThread().getId());
    }
}
  • 使用xml
public class MultiThreadOnXML {

    @Test
    public void test1(){
        System.out.println(Thread.currentThread().getId());
    }

    @Test
    public void test2(){
        System.out.println(Thread.currentThread().getId());
    }


    @Test
    public void test3(){
        System.out.println(Thread.currentThread().getId());
    }
}
<suite name="suitename" parallel="methods" thread-count="2">
    <!--
        https://www.cnblogs.com/longronglang/p/10878262.html
        在<suite>标签中用parallel和thread-count两个属性声明多线程:
            parallel:代表线程级别
                tests级别:
                    不同的test标签下的用例可以在不同线程下执行
                    相同的test标签下的用来只能在同一个线程中执行
                classes级别:
                    不同的classes标签下的用例可以在不同的线程中执行
                    相同的classes标签下的用例只能在同一个线程中执行
                methods级别:
                    所有的用例都可以在不同的线程中执行
            thread-count代表:最大并发线程数
    -->
    <test name="testname">
        <classes name="classname">
            <class name="com.it.cases.MultiThreadOnXML"></class>
        </classes>
        <classes>
            <class name="com.it.cases.MultiThreadOnXML"></class>
        </classes>
    </test>
</suite>

代码

参考:
https://blog.csdn.net/lovedingd/article/details/106784561
https://cloud.tencent.com/developer/article/1928956

posted @ 2022-07-11 22:08  扬帆去远航  阅读(85)  评论(0)    收藏  举报