美文网首页接口自动化测试单元测试Unit Test
(四)TestNG学习之路—注解详述之@Test

(四)TestNG学习之路—注解详述之@Test

作者: Tomandy | 来源:发表于2018-08-31 16:49 被阅读2次

    前言

    TestNG提供的诸多注解中,用得最频繁的估计是@Test了,该篇文章将对@Test注解进行详述。

    @Test详述

    import org.testng.annotations.*;
    
    public class TestNGHelloWorld1 {
        @BeforeTest
        public void bfTest(){
            System.out.println("TestNGHelloWorld1 beforTest!");
        }
    
        @Test()
        public String helloWorldTest1(){
            System.out.println("TestNGHelloWorld1 Test1!");
            return "@Test return!";
        }
    
        @AfterTest
        public void AfTest(){
            System.out.println("TestNGHelloWorld1 AfterTest!");
        }
    }
    

    如下所示,testng.xml配置allow-return-values为false(默认为false),执行测试会忽略@Test注解的helloWorldTest1方法。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="All Test Suite" allow-return-values="false">
        <test verbose="2" preserve-order="true" name="D:/IntelliJ_IDEA_workspace/TestNG/src/test/resources">
            <classes>
                <class name="TestNGHelloWorld1"/>
            </classes>
        </test>
    </suite>
    

    输出结果为:

    TestNGHelloWorld1 beforTest!
    TestNGHelloWorld1 AfterTest!
    
    ===============================================
    Default Suite
    Total tests run: 0, Failures: 0, Skips: 0
    ===============================================
    
    • description属性
      描述@Test,在测试报告体现。
    @Test(description = "test")
        public void helloWorldTest1() {
            System.out.println("TestNGHelloWorld1 Test1!");
        }
    
    • enabled属性
      设置为false,执行测试会忽略@Test注解的helloWorldTest1方法。
        @Test(enabled = false)
        public void helloWorldTest1() {
            System.out.println("TestNGHelloWorld1 Test1!");
        }
    
    • groups属性
      对测试方法进行分组,可在类级别或方法级别添加组,类级别分组,表示类里面的所有方法都属于该分组,如下所示。
    import org.testng.annotations.*;
    
    @Test(groups = "test")
    public class TestNGHelloWorld1 {
        @BeforeTest
        public void bfTest() {
            System.out.println("TestNGHelloWorld1 beforTest!");
        }
    
        @Test(groups = {"test1","test2"})
        public void helloWorldTest1() {
            System.out.println("TestNGHelloWorld1 Test1!");
        }
    
        @Test(groups = {"test3"})
        public void helloWorldTest2() {
            System.out.println("TestNGHelloWorld1 Test2!");
        }
    
        @AfterTest
        public void AfTest() {
            System.out.println("TestNGHelloWorld1 AfterTest!");
        }
    }
    

    运行分组test分组,但不运行test1分组。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="All Test Suite">
        <test verbose="2" preserve-order="true" name="D:/IntelliJ_IDEA_workspace/TestNG/src/test/resources">
            <groups>
                <run>
                    <include name="test"/>
                    <exclude name="test1"/>
                </run>
            </groups>
    
            <classes>
                <class name="TestNGHelloWorld1"/>
            </classes>
        </test>
    </suite>
    

    输出结果:

    TestNGHelloWorld1 beforTest!
    TestNGHelloWorld1 Test2!
    TestNGHelloWorld1 AfterTest!
    
    ===============================================
    All Test Suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
    

    xml配置组名称等也支持正则表达式,开发/测试人员可更灵活的配置测试,例如:

    <groups>
                <run>
                    <!--匹配以test开始的所有组-->
                    <include name="test.*"/>
                    <exclude name="test1"/>
                </run>
    </groups>
    
    <classes>
            <class name="TestNGHelloWorld1">
                <methods>
                    <!--匹配包含Test2串的方法-->
                    <include name=".*Test2.*"/>
                </methods>
            </class>
    </classes>
    
    • dependsOnGroups属性
      定义组之间的依赖关系,可使用正则表达式作为参数。
    import org.testng.Assert;
    import org.testng.annotations.*;
    
    public class TestNGHelloWorld1 {
        @BeforeTest
        public void bfTest() {
            System.out.println("TestNGHelloWorld1 beforTest!");
        }
    
        @Test(groups = {"test1"})
        public void helloWorldTest1() {
            System.out.println("TestNGHelloWorld1 Test1!");
            Assert.assertEquals("1","2");
        }
    
        //test3依赖于test1组,如果test1组执行失败,则test3跳过测试
        @Test(groups = {"test3"},dependsOnGroups = "test1")
        public void helloWorldTest2() {
            System.out.println("TestNGHelloWorld1 Test2!");
        }
    
        @AfterTest
        public void AfTest() {
            System.out.println("TestNGHelloWorld1 AfterTest!");
        }
    }
    

    执行结果:

    TestNGHelloWorld1 beforTest!
    TestNGHelloWorld1 Test1!
    
    java.lang.AssertionError: expected [2] but found [1]
    Expected :2
    Actual   :1
     <Click to see difference>
    
    
        at org.testng.Assert.fail(Assert.java:93)
        at org.testng.Assert.failNotEquals(Assert.java:512)
        at org.testng.Assert.assertEqualsImpl(Assert.java:134)
        at org.testng.Assert.assertEquals(Assert.java:115)
        at org.testng.Assert.assertEquals(Assert.java:189)
        at org.testng.Assert.assertEquals(Assert.java:199)
        at TestNGHelloWorld1.helloWorldTest1(TestNGHelloWorld1.java:14)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
        at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
    
    
    Test ignored.
    TestNGHelloWorld1 AfterTest!
    
    ===============================================
    All Test Suite
    Total tests run: 2, Failures: 1, Skips: 1
    ===============================================
    
    • dependsOnMethods属性
      定义方法之间的依赖关系,与dependsOnGroups用法类似。可使用正则表达式作为参数。另外,如果依赖于一个碰巧有多个重载版本的方法,那么会调用所有重载的方法。
    • timeout属性
      设定超时时间(单位为ms),如果执行测试超过该设定时间,则当做失败处理。
    @Test(timeOut = 2000)
    public void helloWorldTest2() throws InterruptedException {
          Thread.sleep(3000);
          System.out.println("TestNGHelloWorld1 Test2!");
    }
    
    • invocationTimeOut属性
      该属性和invocationCount结合使用才会工作,耗时值不能超过设置的最大毫秒数。
    @Test(invocationCount = 5, invocationTimeOut = 4000)
    public void helloWorldTest3() throws InterruptedException{
         Thread.sleep(1000);
          System.out.println("lsss");
     }
    
    • invocationCount属性
      表示执行的次数。
    @Test(threadPoolSize = 3,invocationCount = 2,timeOut = 2000)
    public void helloWorldTest2() throws InterruptedException {
          Thread.sleep(3000);
          System.out.println("TestNGHelloWorld1 Test2!");
    }
    
    • threadPoolSize属性
      表示线程池的内线程的个数。
    @Test(threadPoolSize = 3,invocationCount = 2,timeOut = 2000)
    public void helloWorldTest2() throws InterruptedException {
          Thread.sleep(3000);
          System.out.println("TestNGHelloWorld1 Test2!");
    }
    
    • successPercentage属性
      当前方法执行所期望的success的百分比。如下所示,执行10次,如果成功9次,则认为测试通过。
    @Test(invocationCount = 10,successPercentage = 9)
    public void helloWorldTest1() {
          System.out.println("TestNGHelloWorld1 Test1!");
          Assert.assertEquals("1","1");
    }
    
    • dataProvider属性
      结合@DataProvider使用,后续文章详述。
    • dataProviderClass属性
      结合@DataProvider使用,后续文章详述。
    • alwaysRun属性
      如果为true,则该测试方法依然会被运行即使其所依赖的方法执行失败。为false的话,则该测试方法会被skip如果其所依赖的方法执行失败。
    import org.testng.Assert;
    import org.testng.annotations.*;
    
    public class TestNGHelloWorld1 {
        @BeforeTest
        public void bfTest() {
            System.out.println("TestNGHelloWorld1 beforTest!");
        }
    
        @Test()
        public void helloWorldTest1() {
            System.out.println("TestNGHelloWorld1 Test1!");
            Assert.assertEquals("2", "1");
        }
    
        @Test(dependsOnMethods = "helloWorldTest1",alwaysRun = true)
        public void helloWorldTest2() throws InterruptedException {
            System.out.println("TestNGHelloWorld1 Test2!");
        }
    
        @AfterTest
        public void AfTest() {
            System.out.println("TestNGHelloWorld1 AfterTest!");
        }
    }
    

    执行结果:

    TestNGHelloWorld1 beforTest!
    TestNGHelloWorld1 Test1!
    
    java.lang.AssertionError: expected [1] but found [2]
    Expected :1
    Actual   :2
     <Click to see difference>
    
    
        at org.testng.Assert.fail(Assert.java:93)
        at org.testng.Assert.failNotEquals(Assert.java:512)
        at org.testng.Assert.assertEqualsImpl(Assert.java:134)
        at org.testng.Assert.assertEquals(Assert.java:115)
        at org.testng.Assert.assertEquals(Assert.java:189)
        at org.testng.Assert.assertEquals(Assert.java:199)
        at TestNGHelloWorld1.helloWorldTest1(TestNGHelloWorld1.java:15)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
        at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
    
    TestNGHelloWorld1 Test2!
    TestNGHelloWorld1 AfterTest!
    
    ===============================================
    Default Suite
    Total tests run: 2, Failures: 1, Skips: 0
    ===============================================
    
    • expectedExceptions属性
      属性expectedExceptions是一组类,包含了这个测试方法中预期会抛出的异常列表。如果没有抛出异常,或抛出的异常不再该属性的类表中,那么TestNG就会认为这个测试方法失败了。以下测试执行通过。
    @Test(expectedExceptions = ArithmeticException.class)
    public void helloWorldTest1() {
          System.out.println("TestNGHelloWorld1 Test1!");
          int c = 1/0;
          Assert.assertEquals("1", "1");
    }
    
    • expectedExceptionsMessageRegExp属性
      异常信息正则表达式。以下测试执行通过。
    @Test(expectedExceptions = ArithmeticException.class,expectedExceptionsMessageRegExp = ".*zero")
    public void helloWorldTest1() {
          System.out.println("TestNGHelloWorld1 Test1!");
          int c = 1/0;
          Assert.assertEquals("1", "1");
    }
    
    • suiteName属性
      测试套件名称。
    • testName属性
      测试名称,跟description属性作用类似。
    • singleThreaded属性
      如果设置为true,那么这个测试类中的所有方法都保证在同一个线程中运行,即使测试当前使用parallel="methods"运行。这个属性只能在类级别使用,如果在方法级别使用,它将被忽略。
    • retryAnalyzer属性
      TestNG重试机制,后续文章详解。
    • skipFailedInvocations属性
      是否跳过失败的调用,如果不跳过,可能会导致测试用例的死锁。
    • ignoreMissingDependencies属性
      如果为true,找不到依赖也继续执行。
    import org.testng.Assert;
    import org.testng.annotations.*;
    
    public class TestNGHelloWorld1 {
        @BeforeTest
        public void bfTest() {
            System.out.println("TestNGHelloWorld1 beforTest!");
        }
    
        @Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = ".*zero")
        public void helloWorldTest1() {
            System.out.println("TestNGHelloWorld1 Test1!");
            int c = 1 / 0;
            Assert.assertEquals("1", "1");
        }
    
        @Test(dependsOnGroups = "test",ignoreMissingDependencies = true)
        public void helloWorldTest2() {
            Assert.assertEquals("1", "1");
            System.out.println("TestNGHelloWorld1 Test2!");
    
        }
    
        @AfterTest
        public void AfTest() {
            System.out.println("TestNGHelloWorld1 AfterTest!");
        }
    }
    
    • priority属性
      标注测试方法的优先级。较低的优先级将优先执行。

    相关文章

      网友评论

        本文标题:(四)TestNG学习之路—注解详述之@Test

        本文链接:https://www.haomeiwen.com/subject/wfufwftx.html