TestNG基本使用

项目中需要写单测,可以使用TestNG来完成自测,可以加载容器,这样能够大大降低频繁部署到项目环境的时间,提前修复一些小的BUG。

// 这个是全局groups
@Test(groups = "global-group")
public class Testng {

    // 在组件运行之前运行
    @BeforeSuite
    public void beforeSuite() {
        System.out.println("before suite");
    }


    // 在所有测试开始之前运行
    @BeforeTest
    public void beforeTest() {
        System.out.println("beforeTest..");
    }

    // 在类加载之前运行
    @BeforeClass
    public void before() {
        System.out.println("beforeClass ..");
    }

    // 普通测例
    @Test()
    public void execute() {
        System.out.println("execute..");
    }

    @Test
    public void execute2() {
        System.out.println("execute2...");
    }

    @AfterClass
    public void after() {
        System.out.println("afterClass");
    }

    @AfterTest
    public void afterTest() {
        System.out.println("afterTest..");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("afterSuite");
    }

    // 在每个方法之前运行
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("before method..在每个@Test注解方法之前运行");
    }

    // 在内置(非类)分组用例之前
    @BeforeGroups
    public void beforeGroups() {
        System.out.println("before Groups");
    }

    @Test(groups = {"group1"})
    public void executeG1() {
        System.out.println("g1");
    }

    @Test(groups = {"group1"})
    public void executeG2() {
        System.out.println("g2");
    }

    @BeforeGroups(groups = "group1")
    public void beforeG1() {
        System.out.println("before group1");
    }

    // 抛出这个异常是可以通过单测的
    @Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)
    public void exceptEx() {
        int[] array = new int[]{1,2};
        System.out.println(array[3]);
        System.out.println("except..");
    }

    // 是否开启这个测试用例,默认情况是开启的
    @Test(enabled = false)
    public void enable() {
        System.out.println("enable .....###");
    }

    // 超时测试
    @Test(timeOut = 1000)
    public void testTimeout() {
        /*try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            System.out.println("error");
        }*/
    }

    // 参数测试
    @Test(dataProvider = "pTest")
    public void testParam(int a, int b) {
        System.out.println("a = " + a + ", b = " + b);
        Assert.assertEquals(a + 1, b);
    }

    @DataProvider(name = "pTest")
    public Object[][] dataPrivider() {
        return new Object[][]{{1, 2}, {2, 3}, {3, 4}};
    }
}

在测例目录:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">

    <test name="micro-testng-group">

        <groups>
            <run>
                <!-- 这个是分组名称 -->
                <include name="global-group"></include>
                <include name="group1"></include>
            </run>
        </groups>

        <classes>
            <class name="com.micro.Testng"/>
        </classes>

    </test>
</suite>

运行结果:

before suite
beforeTest..
beforeClass ..
before method..在每个@Test注解方法之前运行
before method..在每个@Test注解方法之前运行
execute..
before method..在每个@Test注解方法之前运行
execute2...
before group1
before method..在每个@Test注解方法之前运行
g1
before method..在每个@Test注解方法之前运行
g2
before method..在每个@Test注解方法之前运行
a = 1, b = 2
before method..在每个@Test注解方法之前运行
a = 2, b = 3
before method..在每个@Test注解方法之前运行
a = 3, b = 4
before method..在每个@Test注解方法之前运行
afterClass
afterTest..
afterSuite

===============================================
Suite1
Total tests run: 9, Failures: 0, Skips: 0
===============================================

猜你喜欢

转载自blog.csdn.net/micro_hz/article/details/79214289