TestNG测试框架与surefire插件

1、testng

1.1 testng的介绍

TestNG是Java中的一个测试框架,类似于JUnit功能都差不多,只是功能更加强大,使用也更方。用XML文件运行套件测试,TestNG不仅可以做捆绑类测试,也可以捆绑方法进行测试。

TestNG的运行方式更加灵活,注释更加丰富。对于大型测试套件,我们不希望在某一项测试失败时就得重新运行数千项测试,TestNG 的灵活性在这里尤为有用。

https://img-blog.csdn.net/20181023163741642?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI0MjkyMDc3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70

1.2 testng的常用注解

@Test  --TestNG 的测试类是普通的老式 Java 对象;不需要扩展任何特殊的类,也不需要使用测试方法的任何命名约定:只要用标注 @Test 通知框架这个类的方法是测试

@Test(groups = {"tests.string"}) --定义测试组 TestNG 允许我们通过组的形式来运行多个测试方法。
    配合
<groups>
  <run>
     <exclude name="test.sjd"></exclude>
  </run>
</groups>

@BeforeSuite:带注释的方法将在该套件中的所有测试运行之前运行。
@AfterSuite:带注释的方法将在运行此套件中的所有测试之后运行。

@BeforeTest:带注释的方法将在运行任何属于<test>标记内的类的测试方法之前运行。
@AfterTest:带注释的方法将在所有属于<test>标记内的类的测试方法运行后运行。

@BeforeGroups:将在本组内任何测试方法执行前被执行一次 @BeforeGroups(value="ui")
@AfterGroups:在本组内任何测试方法执行后被调用一次。

@BeforeClass:测试类加载之前运行 只运行一次。
@AfterClass:测试类加载之后运行 只运行一次。

@BeforeMethod:带注释的方法将在每个测试方法之前运行。
@AfterMethod:带注释的方法将在每个测试方法之后运行。

运行顺序@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite
其中{}内的与多少个@Test,就循环执行多少次。

@DataProvider:
为测试方法提供数据,带该注解的方法必须返回一个Object [] [],使用等于此注释名称的dataProvider名称
@DataProvider(name = "testDataProvider")
public Object[][] provider(){
    return new Object[][]{{"hello",12},{"word",34}};
}

@Test(dataProvider = "testDataProvider")
public void testDataProvider(String str, int i){
    System.out.println(str + "---" + i);
}

@Listeners:测试类上定义监听器  @Listeners(value = {AgentInvokedMethodListener.class, AgentReportListener.class})

1.3 testng的Listener

IInvokedMethodListener:在当前测试方法被执行前和执行后注入特定的逻辑

    void afterInvocation(IInvokedMethod method, ITestResult testResult)

    void beforeInvocation(IInvokedMethod method, ITestResult testResult)

IHookable:(AOP)在测试方法执行前后提供了切入点,可在拦截器中加入特定的验证逻辑以决定测试方法是否运行或者跳过

  void run(IHookCallBack iHookCallBack, ITestResult iTestResult)

IReporter:在所有测试方法执行结束后被调用

    void generateReport(java.util.List<XmlSuite> xmlSuites, java.util.List<ISuite> suites, java.lang.String outputDirectory)

ITestListener :在测试方法执行成功、失败或者跳过时指定不同后续行为

    void onTestStart(ITestResult var1);

    void onTestSuccess(ITestResult var1);

    void onTestFailure(ITestResult var1);

    void onTestSkipped(ITestResult var1);

    void onTestFailedButWithinSuccessPercentage(ITestResult var1);

    void onStart(ITestContext var1);

    void onFinish(ITestContext var1);

TestListenerAdapter:实现了 ITestListener,并且成功失败跳过已给出了默认实现

IMethodInterceptor: 对运行顺序是随机的用例进行监听 dependsOnGroups dependsOnMethods

IAnnotationTransformer:需要改动源代码中定义的注释

    void transform(ITest annotation, Class testClass, Constructor testConstructor, Method testMethod);

    annotation 代表就是为 testMethod 定义的 @Test 注释。调用其方法可以更改 @Test 注释属性。例如,下面的代码在运行时将属性 enabled 改为 false 从而禁用了当前的测试方法。

1.4 testng.xml

testng.xml是为了更方便的管理和执行测试用例,同时也可以结合其他工具

1)首先要声明一个suite的名字,用于描述将要运行的测试脚本集,可以根据自己需要任意命名,最终这个名字会在testng的测试报告中看到。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="SuiteName" verbose="1" > 

    <test name="TestName" >

2)如果选择的测试脚本是基于组的(使用了@Test (groups={"student"})这样的注解),那么接下来需要声明如何使用这些组:包含或者排除。如果使用了include标签标注某些组,那么在选择的测试脚本中,只有属于那些组的测试脚本会被运行。那些未被选中的测试脚本,或者被选中却不属于某些组的测试脚本都不会被运行。include和exclude标签的使用方式如下:

<groups>

  <run>

     <include name = "includedGroupName" />

     <exclude name = "excludedGroupName" />

  </run>

</groups>

 

3)选择测试脚本可以从包、类、方法三个层级进行。

  选择一个包

  <packages>

      <package name = "packageName" />

  </packages>

  选择一个类

  <classes>

      <class name = "className" />

  </classes>

  选择一个方法

  <classes>

      <class name = "className" />

         <methods>

            <include name = "methodName" />

         </methods>

      </class>

  </classes>

2、surefire插件

2.1 插件介绍

surefire 插件使用在 maven 生命周期的 test 阶段,是用来做测试使用的,他提供了一些 mvn 操作可以很方便的让我们执行我们需要的测试任务

可以支持同时跑junit和testng用例。

要求搭配 jdk 1.7 以上

要求搭配 maven 3 以上

       官方文档:http://maven.apache.org/surefire/maven-surefire-plugin/

注意:2.12.4默认只解析junit的用例

2.2 插件使用

1)-Dtest参数,通过在mvn命令中增加-Dtest配置项

     mvn test -Dtest=SurefireTest#test -DfailIfNoTests=false

surefire默认会强校验进行报错(认为每个有java代码的module,都应该有单测用例覆盖),可以加-DfailIfNoTests=false 来关闭这项校验

2)通过pom.xml配置方式添加对TestNG的依赖,然后再Surefire插件的configuration中根据测试类的特征配置要进行的测试。这类的典型配置如下:

  1. </dependencies>
  2. <build>
  3.   <plugins>
  4.     <plugin>
  5.       <groupId>org.apache.maven.plugins</groupId>
  6.       <artifactId>maven-surefire-plugin</artifactId>
  7.       <version>2.19.1</version>
  8.       <configuration>
  9.         <includes>
  10.           <include>**/*Test*.java</include>
  11.         </includes>
  12.         <excludes>
  13.           <exclude>**/TestCircle.java</exclude>
  14.           <exclude>**/TestSquare.java</exclude>
  15.         </excludes>
  16.       </configuration>
  17.     </plugin>
  18.   </plugins>
  19. </build>
  20.  

另一类是在pom.xml文件中,直接通过TestNG的suite文件,执行其中的测试,而忽略测试类本身的特征。这类的典型配置如下:

  1.      
    <plugins>
  2.     ...
  3.       <plugin>
  4.         <groupId>org.apache.maven.plugins</groupId>
  5.         <artifactId>maven-surefire-plugin</artifactId>
  6.         <version>2.19.1</version>
  7.         <configuration>
  8.           <suiteXmlFiles>
  9.             <suiteXmlFile>testng.xml</suiteXmlFile>
  10.           </suiteXmlFiles>
  11.         </configuration>
  12.       </plugin>
  13.     ...
  14. </plugins>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值