testNg学习记录

testNG.xml文件

2018-11-02  本文已影响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>
上一篇 下一篇

猜你喜欢

热点阅读