自动化框架:Java+SpringBoot+IntelliJ IDEA+TestNG+maven


简介:

语言:java

框架:SpringBoot 

集成开发工具:IntelliJ IDEA

测试框架:TestNG 

项目管理工具:maven

本章重点介绍TestNG的使用。

一.testNG介绍

     TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit,   功能都差不多, 只是功能更加强大,使用也更方便

Java中已经有一个JUnit的测试框架了。  TestNG比JUnit功能强大的多。  测试人员一般用TestNG来写自动化测试。  开发人员一般用JUnit写单元测试。

官方网站: http://testng.org/doc/index.html

二、TestNG的优点

  2.1 漂亮的HTML格式测试报告

  2.2 支持并发测试

  2.3 参数化测试更简单

  2.4 支持输出日志

  2.5 支持更多功能的注解

三、编写TestNG测试用例的步骤

  3.1 使用 Eclipse生成TestNG的测试程序框架

  3.2 在生成的程序框架中编写测试代码逻辑

  3.3 根据测试代码逻辑,插入TestNG注解标签

  3.4 配置Testng.xml文件,设定测试类、测试方法、测试分组的执行信息

  3.5 执行TestNG的测试程序

四、TestNG安装使用

工程的pom.xml中添加如下内容即可:

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
        </dependency>

示例代码:

package com.huaxi.huaxitest;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.huaxi.base.BaseTest
public class TestNGCase extend BaseTest{ @BeforeClass public void testBefore() throws Exception{ System.out.println("this is before class"); } @Test public void test002() throws Exception{ System.out.println("this is TestNG test case"); } @AfterClass public void testAfter() throws Exception{ System.out.println("this is after class"); } }

需要继承AbstractTestNGSpringContextTests

package com.huaxi.base;

import com.huaxi.huaxitest.HuaxitestApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;


@SpringBootTest(classes = HuaxitestApplication.class)
public class BaseTest extends AbstractTestNGSpringContextTests {
}

 

运行结果截图:

五、TestNG常用注解

注解

描述

@BeforeSuite

注解的方法将只运行一次,运行所有测试前此套件中。

@AfterSuite

注解的方法将只运行一次此套件中的所有测试都运行之后。

@BeforeClass

注解的方法将只运行一次先行先试在当前类中的方法调用。

@AfterClass

注解的方法将只运行一次后已经运行在当前类中的所有测试方法。

@BeforeTest

注解的方法将被运行之前的任何测试方法属于内部类的 <test>标签的运行。

@AfterTest

注解的方法将被运行后,所有的测试方法,属于内部类的<test>标签的运行。

@BeforeGroups

组的列表,这种配置方法将之前运行。此方法是保证在运行属于任何这些组第一个测试方法,该方法被调用。

@AfterGroups

组的名单,这种配置方法后,将运行。此方法是保证运行后不久,最后的测试方法,该方法属于任何这些组被调用。

@BeforeMethod

注解的方法将每个测试方法之前运行。

@AfterMethod

被注释的方法将被运行后,每个测试方法。

@DataProvider

标志着一个方法,提供数据的一个测试方法。注解的方法必须返回一个Object[] [],其中每个对象[]的测试方法的参数列表中可以分配。

该@Test 方法,希望从这个DataProvider的接收数据,需要使用一个dataProvider名称等于这个注解的名字。

@Factory

作为一个工厂,返回TestNG的测试类的对象将被用于标记的方法。该方法必须返回Object[]。

@Listeners

定义一个测试类的监听器。

@Parameters

介绍如何将参数传递给@Test方法。

@Test

标记一个类或方法作为测试的一部分。

 六、TestNG忽略测试

不想运行某个用例时,可以在测试用例加上@Test(enable = false),  来禁用此测试用例

package com.huaxi.huaxitest;

import org.testng.annotations.Test;


public class TestNGCase {

    @Test
    public void test001() throws Exception{
        System.out.println("this is TestNG test case");
    }
    
    @Test(enabled = false)
    public void test002() throws Exception{
        System.out.println("忽略这个用例");
    }

}

七、TestGN分组测试

package com.huaxi.huaxitest;

import org.testng.annotations.Test;


public class TestNGCase {

    @Test(groups = "group1")
    public void test001() throws Exception{
        System.out.println("一组用例");
    }
    

    @Test(groups = "group2")
    public void test002() throws Exception{
        System.out.println("二组用例");
    }

}

 

八、Test.xml标签

1、选择一个类中的全部测试脚本

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySute">
<test name="TestCaseClass">
<classes>
<class name="com.huaxi.huaxitest.TestNGCase" />
</classes>
</test>
</suite>

2、选择一个类中的部分测试脚本

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySute">
    <test name="TestCaseClass">
        <classes>
            <class name="com.huaxi.huaxitest.TestNGCase" >
                <methods>
                    <include name="test001"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

3、选择一个类中的某些组

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySute">
    <test name="TestCaseClass">
        <groups>
            <run>
                <include name="group1"/>
            </run>
        </groups>
        <classes>
            <class name="com.huaxi.huaxitest.TestNGCase"></class>
        </classes>
    </test>
</suite>

九、Idea+maven+testng+reportng生成测试报告

1、在pom.xml文件中添加maven的依赖包

<dependencies>注意在这个节点下添加下面的依赖包</dependencies>
        <dependency>
            <groupId>org.uncommons</groupId>
            <artifactId>reportng</artifactId>
            <version>1.1.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>

2、再在pom.xml文件添加插件

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>xmlfile/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <properties>
                        <property>
                            <name>usedefaultlisteners</name>
                            <value>false</value>
                        </property>
                        <property>
                            <name>listener</name>
                            <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
                        </property>
                    </properties>
                    <workingDirectory>target/</workingDirectory>
                </configuration>
            </plugin>
    <build>
        <plugins>
            上面的内容是这个节点下添加,不要添加错位置了
        </plugins>
    </build>

3、在testng.xml中配置监听器

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySute">
    <test name="TestCaseClass">
        <classes>
            <class name="com.huaxi.huaxitest.TestNGCase"></class>
        </classes>
    </test>
    <listeners>
        <listener class-name="org.uncommons.reportng.HTMLReporter"/>
        <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
    </listeners>
</suite>

4、IDEA配置

 在Idea中打开Run-Edit Configurations...

 

 在Listeners标签下勾选“Use default reporters”

最后运行testng.xml,自动生成test-output目录,在html目录下找到index.html

 

打开index.html

 

 

参考文档:

https://blog.csdn.net/df0128/article/details/83243822

https://www.cnblogs.com/seven7777/p/9517186.html

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM