testng+maven+java+idea

1. 在ide中创建maven项目
创建过程
点击next

  1. GroudId:一般为公司的域名,如www.baidu.com
  2. ArtifiactId:一般为项目的名称

点击finish

2. 在pom.xml中添加testng的有关依赖,如下:

public class TestDemo {
    @Test
    public void testcase2(){
        Assert.assertTrue(true);
        System.out.println("testcase1");
    }
}

3. 创建测试类

public class TestDemo {
    @Test
    public void testcase2(){
        Assert.assertTrue(true);
        System.out.println("testcase1");
    }
}

4.在项目目录中添加testng.xml,最简单的格式如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="TestNG1">
        <classes>
            <class name="TestDemo"></class>
        </classes>
    </test>
</suite>

其中可以添加插件

这里写图片描述

安装完成后需要重启idea

5. maven与testng进行在pom文件中进行关联配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.15</version>
            <configuration>
                <!--<testFailureIgnore>true</testFailureIgnore>-->
                <forkMode>never</forkMode>
                <argLine>-Dfile.encoding=UTF-8</argLine>
                <suiteXmlFiles>
                    <suiteXmlFile>res/testNG.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

suiteXmlFile的路径为testNG.xml的路径

6. 运行testng.xml文件即可
直接运行testng.xml文件后,生成报告如下
这里写图片描述

7. 设置后重新运行testng.xml会创建test-output文件夹
这里写图片描述
这里写图片描述
8. 打开test-output文件夹中的index.html文件
生成如下的测试报告
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_23114525/article/details/81459802
今日推荐