TestNG单元测试框架使用示例

文章目录

  • 1 什么是 TestNG?
  • 2 TestNg有哪些优点?
  • 3 如何编写 TestNG测试用例的步骤
  • 4 在开发工具中使用TestNG插件
  • 5 Selenium+TestNG 使用实例

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-8project.build.sourceEncoding>
    properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>8source>
                    <target>8target>
                configuration>
            plugin>
        plugins>
    build>
    <dependencies>
        <dependency>
            <groupId>org.testnggroupId>
            <artifactId>testngartifactId>
            <version>6.14.3version>
            <scope>testscope>
        dependency>
    dependencies>

5 Selenium+TestNG 使用实例

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

  • 下载FireFox 驱动,https://github.com/mozilla/geckodriver/releases , 根据系统版本下载放到D:\WebDriver目录。
  • 在项目\src\test\java创建包和测试类
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.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.17version>
                <configuration>
                    <testFailureIgnore>truetestFailureIgnore>
                    <suiteXmlFiles>
                        <file>res/TestNG.xmlfile>
                    suiteXmlFiles>
                    
                configuration>
            plugin>

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


<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/

你可能感兴趣的:(Selenium,WebDriver,实战(Java))