目录

学习资料

关于 Case 结果


学习资料

TestNG

源码下载

$ git clone git://github.com/cbeust/testng.git

$ cd testng

$ ./build-with-gradle

关于Case执行

TestNG执行顺序

@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest→@AfterSuite

@BeforeClass---@AfterClass

类实例化前, 被执行, 主要用于设置环境变量等, 与SpringTestContext结合用的时候要注意, 这种情况下@autowire的bean还未实例化

@BeforeTest----@AfterTest

整个测试类开始前, 被执行, 主要用户塞值, 或者进行mock(Object)的初始化, 此方法只会运行一次

@BeforeMethod-----@AfterMethod

每个测试方法执行前, 进行调用, 和@BeforeTest的主要区别在于, 你如果需要每次清空你测试的一些上下文, 那么需要配合@AfterMethod一起使用

关于Case结果获取

实现接口org.testng.IReporter

最终获取 Case result list,需等待所有 test suite 运行完,才能获取最终报告。

比如需要自定义报告样式,为 PDF 格式或其他定制格式

实现接口org.testng.ITestListener 

实时获取 Case 运行结果,并可自定义 success、fail、skip 之后的动作

关于自定义测试方法执行前后 AOP

Hook 方式

如源码,提供一个 run 接口,用于测试方法执行前和后提供了切入点。相当于 AOP 中 Around,在 Test Case 执行前后,做一些自定义操作。

package org.testng;

public interface IHookable extends ITestNGListener {

void run(IHookCallBack var1, ITestResult var2);

}
@Override
    public void run(IHookCallBack iHookCallBack, ITestResult result) {
        // do something before test case run

        iHookCallBack.runTestMethod(result);

        // do something after test case run
    }

IInvokedMethodListener方式

如源码,提供 beforeInvocation 和 afterInvocation,可在测试方法执行前后,做一些自定义操作。

package org.testng;

public interface IInvokedMethodListener extends ITestNGListener {
    default void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
    }

    default void afterInvocation(IInvokedMethod method, ITestResult testResult) {
    }

    default void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {
    }

    default void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {
    }
}
 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐