接口自动化测试单元测试Unit Test

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

2018-08-31  本文已影响2人  Tomandy

前言

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
===============================================
@Test(description = "test")
    public void helloWorldTest1() {
        System.out.println("TestNGHelloWorld1 Test1!");
    }
    @Test(enabled = false)
    public void helloWorldTest1() {
        System.out.println("TestNGHelloWorld1 Test1!");
    }
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>
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
===============================================
@Test(timeOut = 2000)
public void helloWorldTest2() throws InterruptedException {
      Thread.sleep(3000);
      System.out.println("TestNGHelloWorld1 Test2!");
}
@Test(invocationCount = 5, invocationTimeOut = 4000)
public void helloWorldTest3() throws InterruptedException{
     Thread.sleep(1000);
      System.out.println("lsss");
 }
@Test(threadPoolSize = 3,invocationCount = 2,timeOut = 2000)
public void helloWorldTest2() throws InterruptedException {
      Thread.sleep(3000);
      System.out.println("TestNGHelloWorld1 Test2!");
}
@Test(threadPoolSize = 3,invocationCount = 2,timeOut = 2000)
public void helloWorldTest2() throws InterruptedException {
      Thread.sleep(3000);
      System.out.println("TestNGHelloWorld1 Test2!");
}
@Test(invocationCount = 10,successPercentage = 9)
public void helloWorldTest1() {
      System.out.println("TestNGHelloWorld1 Test1!");
      Assert.assertEquals("1","1");
}
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
===============================================
@Test(expectedExceptions = ArithmeticException.class)
public void helloWorldTest1() {
      System.out.println("TestNGHelloWorld1 Test1!");
      int c = 1/0;
      Assert.assertEquals("1", "1");
}
@Test(expectedExceptions = ArithmeticException.class,expectedExceptionsMessageRegExp = ".*zero")
public void helloWorldTest1() {
      System.out.println("TestNGHelloWorld1 Test1!");
      int c = 1/0;
      Assert.assertEquals("1", "1");
}
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!");
    }
}
上一篇 下一篇

猜你喜欢

热点阅读