testNG 其他小的知识点

Example:

1. TestNG测试异常

  /*
     * Example : Multiple expected exceptions Test is success if either of the
     * exception is thrown
     */
    @Test(expectedExceptions = { DataUpdateException.class, DataNotFoundException.class })
    public void throwIfDataIsNotExists() throws DataUpdateException, DataNotFoundException {
        orderBo.update(data);
    }

2. TestNG忽略测试

有时,我们编写的代码并没有准备就绪,并且测试用例要测试该方法/代码是否失败(或成功)。 在本示例中,注释@Test(enabled = false)有助于禁用此测试用例。

如果使用@Test(enabled = false)注释在测试方法上,则会绕过这个未准备好测试的测试用例。

在本教程中,我们将演示如何使用@Test(enabled = false)来忽略测试方法。

public class TestIgnoreMethod {

    @Test // default enable=true
    public void test1() {
        Assert.assertEquals(true, true);
    }

    @Test(enabled = true)
    public void test2() {
        Assert.assertEquals(true, true);
    }

    @Test(enabled = false)   // Will not be run
    public void test3() {
        Assert.assertEquals(true, true);
    }

}

TestNG中执行超时测试。 “超时”表示如果单元测试花费的时间超过指定的毫秒数,那么TestNG将会中止它并将其标记为失败。

“超时”也可用于性能测试,以确保方法在合理的时间内返回。

 @Test(timeOut = 2000)
    public void testFail() {
        while (true){
            // do nothing
        }

    }

指定要包括或排除某个分组

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestAll">

  <test name="database">
    <groups>
        <run>
            <exclude name="brokenTests" />
            <include name="db" />
        </run>
    </groups>

    <classes>
        <class name="com.yiibai.TestDatabase" />
    </classes>
  </test>

</suite>

@Test属性invocationCountthreadPoolSize在网站上执行负载测试或压力测试。

启动一个包含5个线程的线程池,并运行测试方法5次。

 @Test(invocationCount = 5, threadPoolSize = 5)
    public void testPerformance() {
  ....
}
原文地址:https://www.cnblogs.com/alicegu2009/p/9171813.html