TestNG单元测试框架使用示例

本文深入探讨TestNG单元测试框架的优势,包括并发测试、参数化支持及HTML报告生成等功能,详细介绍了TestNG的安装、配置及与Selenium结合使用的实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 什么是 TestNG?

TestNG单元测试框架比 Junit单元测试框架更强大,它提供了更多的扩展功能。很大一部分自动化测试工程师已经开始转向使用 TestNG单元测试框架来运行更复杂的自动化测试用例, TestNG是一种开源自动化测试框架,NG就是下一代的意思( Next Generation)。 TESTNG的使用和 Junit有些类似,但是它的设计实现比Junit框架更好,提供了更灵活和更强大的功能。 TESTNG消除了一些老式框架的限制,让程序员通过注解、分组、序列和参数化等多种方式组织和执行自动化测试脚本。

2 TestNg有哪些优点?

  • (1)漂亮的HTML格式测试报告。
  • (2)支持并发测试
  • (3)参数化测试更简单。
  • (4)支持输出日志。
  • (5)支持更多功能的注解

3 如何编写 TestNG测试用例的步骤

  • 第一步:使用 Eclipse生成 TestNG的测试程序框架或者使用IntelliJ IDEA(添加TestNG依赖)
  • 第二步:在生成的程序框架中编写测试代码逻辑。
  • 第三步:根据测试代码逻辑,插入 TestNG注解标签。
  • 第四步:配置 Testng. xml文件,设定测试类、测试方法、测试分组的执行信息。
  • 第五步:执行 TestNG的测试程序。

4 在开发工具中使用TestNG插件

  • 在Eclipse中安装TestNG插件,请搜索相关安装说明。最常用的安装是选择Help-Install New software ,输入 http://beust.com/eclipse/ ,然提示安装
  • IntelliJ 中使用,新建一个maven工程项目,在pom.xml中添加依赖:
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

5 Selenium+TestNG 使用实例

  • 添加相关maven依赖
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.12.0</version>
        </dependency>

package cn.wm;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
public class FirstTestNGDemo {
    public  WebDriver driver;
    String baseUrl = "http:www.sogou.com/";
    @BeforeMethod
    public void beforeMethod() {
        /*
        * The path to the driver executable must be set by the webdriver.gecko.driver system property
          报这个错,是因为你使用了selenium3+Firefox。在selenium3中,使用Firefox,需要添加驱动。
          C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe是驱动放置的位置
        * */
        //System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        System.setProperty("webdriver.firefox.marionette","D:\\WebDriver\\geckodriver.exe");
        driver=new FirefoxDriver();
    }
    @Test
    public void testSearch() {
        driver.get(baseUrl+"/");
        driver.findElement(By.id("query")).sendKeys("TestNG自动化测试");
        driver.findElement(By.id("stb")).click();
    }

    @AfterMethod
    public void afterMethod() {
        driver.quit();
    }
  • 查看测试结果和测试报告
    1)添加maven-surefire-plugin插件 关联TestNG.xml
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <file>res/TestNG.xml</file>
                    </suiteXmlFiles>
                    <!--<workingDirectory>target/</workingDirectory>-->
                </configuration>
            </plugin>

2) 在工程对应的目录编写对应的TestNG.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<suite name="testproj" parallel="false">
    <test name="testDemo1">
        <classes>
            <class name="cn.wm.FirstTestNGDemo"></class>
        </classes>
    </test>
</suite>

3)使用maven执行测试,在IDEA控制台Terminal输入:

mvn -f pom.xml clean test  -DxmlFileName=TestNG.xml

4)如何设置IDEA运行测试时自动生成报告
参考 http://www.testclass.net/testng/report/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值