IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> TestNG的使用 -> 正文阅读

[开发测试]TestNG的使用

testng在maven项目中的使用

pom.xml

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <forkCount>0</forkCount>
                <testFailureIgnore>true</testFailureIgnore>
                <suiteXmlFiles>
                    <suiteXmlFiles>testng.xml</suiteXmlFiles>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

com.yineng.aos.testng.TestngGroups

package com.yineng.aos.testng;

import org.testng.annotations.Test;

public class TestngGroups {

    @Test(groups = {"group1", "group2"})
    public void testMethod1() {
        System.out.println("group1 group2 testMethod1");
    }

    @Test(groups = {"group1", "group2"})
    public void testMethod2() {
        System.out.println("group1 group2 testMethod2");
    }

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

    @Test(groups = {"group2"})
    public void testMethod4() {
        System.out.println("group2 testMethod4");
    }

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">
        <groups>
            <run>
                <inlude name="group2" />
            </run>
        </groups>
        <classes>
            <class name="com.yineng.aos.testng.TestngGroups" />
        </classes>
    </test>
</suite>

idea中,可以双击执行:

命令行输入命令执行:

mvn test

testng.xml的配置说明

<test>元素是<suite>的子元素,用以定义一个测试用例。定义测试用例可以通过<classes>或<packages>。

<!ELEMENT test (method-selectors?,parameter*,groups?,packages?,classes?) >

1). <classes>表示以测试类的方式定义测试用例,粒度较小。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  
<suite name="Suite1" verbose="1" >
  <test name="Regression1">
    <classes>
      <class name="com.yineng.aos.testng.TestngGroups"/>
      <class name="com.yineng.aos.testng.TestngGroups2"/>
    </classes>
  </test>
</suite>

2). <packages>表示以测试类所在的包的方式定义测试用例,包中的所有测试类都被涉及,粒度较大。

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

<suite name="Suite1" verbose="1" >
  <test name="Regression1"   >
    <packages>
      <package name="com.yineng.aos.testng" />
   </packages>
 </test>
</suite>

3). <groups>元素

我们知道,<suite>中可以定义一个全局的<groups>。而这里<test>元素中也可以定义一个自己的<groups>,其中定义的组仅对当前所在的测试用例可见。示例如下:

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

<test name="login">
  <groups>
    <run>
      <exclude name="group2"  />
      <include name="group1"  />
    </run>
  </groups>
  
  <classes>
     ...
  </classes>
</test>

注意:在testng.xml配置文件中,<suite>中可定义多个<test>,<test>的执行顺序默认按照在<suite>中出现先后顺序。也可提供<test>的preserve-order='false'改变默认顺序。

3. <methods>

具体到测试类的某个方法,对于<classes>中的一个<class>,可以提供<methods>设置测试方法。示例如下:

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

<test name="login">
  <classes>
 
    <class name="com.yineng.aos.testng.TestngGroups">
      <methods>
        <include name="testMethod1" />
        <include name="testMethod2" />
      </methods>
    </class>
 
    <class name="com.yineng.aos.testng.TestngGroups2" />
 
  </classes>
</test>

可通过指定<suiteXmlFiles>属性来运行指定的testng.xml

(将< suiteXmlFile>的value值设置为引用properties更灵活。执行命令时就可指定testng.xml 例如:mvn clean test -DsuiteXmlFile=src/test/resources/xml/a.xml)

 <properties>
        <suiteXmlFile>testng.xml</suiteXmlFile>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <suiteXmlFiles>
                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

mvn clean test -DsuiteXmlFile=src/test/resources/xml/a.xml

更多surefire plugin的使用方法可以通过下面的两种方法查看:

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin:2.22.2 -Ddetail

mvn surefire:help -Ddetail=true -Dgoal=test

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章           查看所有文章
加:2021-07-22 14:29:45  更:2021-07-22 14:32:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/1 22:28:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码