testng

作者: 百炼 | 来源:发表于2017-09-13 16:24 被阅读0次

[TOC]

参考

  1. tesgng官网
  2. TestNG+Maven+IDEA环境搭建
  3. Spring Start

步骤

前提

准备一个web项目,可以使用mvn命令直接生成一个web项目,注意会提示选择版本。

mvn archetype:generate  -DarchetypeCatalog=internal -DgroupId=cn.everlook.myweb -DartifactId=myweb -DarchetypeArtifactId=maven-archetype-webapp 
  1. pom.xml文件中添加依赖
  <dependencies>
      <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
      </dependency>
  </dependencies>
  1. 添加maven插件
<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>
                        <!--<arg>endorseddirs=${endorsed.dir}</arg>-->
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <file>src/main/resources/res/testNG.xml</file>
                    </suiteXmlFiles>
                    <!--<workingDirectory>target/</workingDirectory>-->
                </configuration>
            </plugin>
        </plugins>
    </build>
  1. 新建一个要测试的类
import org.testng.Assert;
import org.testng.annotations.Test;

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

    @Test
    public void testcase2() {
        Assert.assertTrue(true);
        System.out.println("testcase1");
    }
}
  1. testNG.xml(位置在<file>src/main/resources/res/testNG.xml</file>中指定)
<?xml version="1.0" encoding="utf-8" ?>
<suite name="testproj" parallel="false">
    <test name="testDemo1">
        <!--<packages>-->
        <!--<package name="com.testproj.Demo"/>-->
        <!--</packages>-->
        <classes>
            <class name="TestDemo"></class>
        </classes>
    </test>
</suite>
  1. 控制台执行测试
    注意: 1. 该命令中-DxmlFileName是指定testNG.xml的目录
    2. testNG.xml 可以在<file>src/main/resources/res/testNG.xml</file> 也可以使用-DxmlFileName指定
mvn -f pom.xml clean test
mvn -f pom.xml clean test  -DxmlFileName=src/main/resources/testNG.xml

相关文章

网友评论

      本文标题:testng

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