testNG.xml文件

作者: 迷糊银儿 | 来源:发表于2018-11-02 22:32 被阅读20次

项目地址:https://gitee.com/neimenggudaxue/BasicTest

在xml配置文件里,不仅可以选择某些需要执行的测试脚本,还可以排除某些不需要运行的测试脚本。

一、创建配置文件testNG.xml

1)首先要声明一个suite的名字,用于描述将要运行的测试脚本集,可以根据自己需要任意命名,最终这个名字会在testng的测试报告中看到。

<?xml version="1.0" encoding="utf-8" ?>
<suite name="Suite1" verbose="1">
   
</suite>

2)如果选择的测试脚本是基于组的(使用了@Test (groups={"group1"})这样的注解),那么接下来需要声明如何使用这些组:包含或者排除。
使用include标签标注某些组:那么在选择的测试脚本中,只有属于那些组的测试脚本会被运行。那些未被选中的测试脚本,或者被选中却不属于某些组的测试脚本都不会被运行。
使用exclude标签标注某些组:那么在选择的脚本中,只有不属于那些组的测试脚本会被运行。
同时使用include标签和exclude标签:那么拥有被include标注的组的那些脚本会被运行,拥有被exclude标注的脚本不会被运行。有一个例外是,一个组同时被include和exclude标注了,那么拥有这个组的脚本会被运行。


1.选择一个类中的全部测试脚本
Test.xml

<?xml version="1.0" encoding="utf-8" ?>
<suite name="Suite2" verbose="1">
    <test name="test">
        <classes>
            <class name="testNG.TestNGTest" />
        </classes>
    </test>
</suite>
//右键执行Test.xml结果
testNG.TestNGTesttest3
testNG.TestNGTesttest3
testNG.TestNGTesttest3
testNG.TestNGTesttest2
testNG.TestNGTesttest2

2.选择一个包中的全部测试脚本(包含子包)
testNGProfile.xml

<?xml version="1.0" encoding="utf-8" ?>
<suite name="testNgProfile" verbose="1">
    <test name="test1">
        <packages>
            <package name="testNG2">

            </package>
        </packages>
    </test>
</suite>
//右键执行testNGProfile.xml结果
testNG2.TestNGProfile2.test1
testNG2.TestNGProfile2.test2

3.选择一个类中的部分测试脚本
testNGProfile1.xml

<?xml version="1.0" encoding="utf-8" ?>
<suite name="suit2" verbose="1">
    <test name="allTestsInAClass">
        <classes>
            <class name="testNG.TestNGProfile">
                <methods>
                    <include name="test2"/>
                    <exclude name="test1"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>
//右键执行testNGProfile1结果
testNG.TestNGProfile.test2

4.选择一个包中的某些组
testNGProfile2.xml

<?xml version="1.0" encoding="utf-8" ?>
<suite name="suit3" verbose="1">
    <test name="test3">
        <groups>
            <run>
                <include name="group1" />
            </run>
        </groups>
        <packages>
            <package name="testNG2.*"/>
        </packages>
    </test>
</suite>
//右键执行testNGProfile2.xml结果
testNG2.TestNGProfile2.test1

5.选择排除包中的某些组
testNGProfile3.xml

<?xml version="1.0" encoding="utf-8" ?>
<suite name="suit3" verbose="1">
    <test name="test3">
        <classes>
            <class name="testNG.TestNGProfile">
                <methods>
                    <exclude name="test1"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>
//右键执行testNGProfile3.xml结果
testNG.TestNGProfile.test2

二、在maven的pom.xml文件中配置testng.xml,注意testNG.xml的路径

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>qufang</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.30</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgs>
                        <arg>-Xlint:unchecked</arg>
                        <arg>-Xlint:deprecation </arg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <file>res/testNG.xml</file>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

相关文章

  • 2.数据驱动测试

    1)通过testng.xml传递参数: 适用于简单的参数,可以在testng.xml文件中指定; 注意:在xml数...

  • testNG.xml文件

    项目地址:https://gitee.com/neimenggudaxue/BasicTest在xml配置文件里,...

  • TestNG-简介(markdown)

    简介~~注解 以及XML文件testng.xml配置 commandLine~~ 一、写case的常规步骤: 二、...

  • TestNG笔记(三):testng.xml 配置文件

    TestNG提供的annotaions用来辅助定义测试类。TestNG的testng.xml配置文件用来辅助定义执...

  • 接口测试之TestNG中使用testng.xml摘录

    针对testng.xml,分不同层级进行xml运行时配置文件的编写,一个典型的xml文件配置如下:

  • 批量执行测试用例

    想要批量执行测试用例,可以使用TestNG这个插件,配合maven使用。 1. 创建testng.xml文件 1)...

  • 参考站点

    一、TestNG的testng.xml配置概述 二、TestNG.xml参数配置-如何控制部分执行@test方法 ...

  • TestNG.xml

    testng.xml文件是为了更方便的是配置测试用例,测试套件,利用这个文件,我们可以跑同一个类或者多个不同类里面...

  • Web自动化Selenium+TestNG实践(一)

    登录测试 登录成功的测试代码如下: 登录失败的测试代码如下: testng.xml配置文件内容如下: 查看测试报告

  • TestNG官方文档记录+例子运行

    testng.xml 使用maven工程时,我们可以在工程中添加testng.xml来运行。运行xml,可以使用I...

网友评论

    本文标题:testNG.xml文件

    本文链接:https://www.haomeiwen.com/subject/rujhxqtx.html