如何从 Maven 运行testng.xml

如何从 Maven 运行testng.xml

原文: https://howtodoinjava.com/testng/how-to-execute-testng-tests-with-maven-build/

在此 testng maven 示例中,我们将学习如何使用 maven 构建文件pom.xml执行 testng 测试。 如果您打算自动执行测试测试,并使它们成为项目构建过程本身的一部分,则此知识很重要。

1. 编写 TestNG 测试

以下是我们已经在两个先前教程中编写的两个测试。

public class RegularExpressionGroupTest
{
	@Test(groups = { "include-test-one" })
	public void testMethodOne() {
		System.out.println("Test method one");
	}

	@Test(groups = { "include-test-two" })
	public void testMethodTwo() {
		System.out.println("Test method two");
	}

	@Test(groups = { "test-one-exclude" })
	public void testMethodThree() {
		System.out.println("Test method three");
	}

	@Test(groups = { "test-two-exclude" })
	public void testMethodFour() {
		System.out.println("Test method Four");
	}
}

public class ParallelSuiteTest 
{
	String testName = "";

	@BeforeTest
	@Parameters({ "test-name" })
	public void beforeTest(String testName) {
		this.testName = testName;
		long id = Thread.currentThread().getId();
		System.out.println("Before test " + testName + ". Thread id is: " + id);
	}

	@BeforeClass
	public void beforeClass() {
		long id = Thread.currentThread().getId();
		System.out.println("Before test-class " + testName + ". Thread id is: "
				+ id);
	}

	@Test
	public void testMethodOne() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method " + testName
				+ ". Thread id is: " + id);
	}

	@AfterClass
	public void afterClass() {
		long id = Thread.currentThread().getId();
		System.out.println("After test-method  " + testName
				+ ". Thread id is: " + id);
	}

	@AfterTest
	public void afterTest() {
		long id = Thread.currentThread().getId();
		System.out.println("After test  " + testName + ". Thread id is: " + id);
	}
}

2. 编写 TestNG 套件

让我们看看两个测试的 testng 套件文件testng.xmlsuites-test-testng.xml

<suite name="Group of group Suite" verbose="1">
  <test name="Group of group Test">
    <groups>
      <define name="include-group">
        <include name="include-test-one" />
        <include name="include-test-two" />
      </define>
      <define name="exclude-group">
        <include name="test-one-exclude" />
        <include name="test-two-exclude" />
      </define>
      <run>
        <include name="include-group" />
        <exclude name="exclude-group" />
      </run>
    </groups>
    <classes>
      <class name="com.howtodoinjava.groupExamples.RegularExpressionGroupTest" />
    </classes>
  </test>
</suite>

<suite name="Parallel tests" parallel="tests" thread-count="2" >
  <test name="Test One">
    <parameter name="test-name" value="Test One"/>
    <classes>
      <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
    </classes>
  </test>
  <test name="Test Two">
    <parameter name="test-name" value="Test Two"/>
    <classes>
      <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
    </classes>
  </test>
</suite>

3. 在 Maven 中添加 testng 套件

在项目根目录下的pom.xml下添加。 下面给出的文件为 maven 构建项目定义了不同的配置。 每个部分的功能已在代码中作为内联注解提及。

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.test.maven</groupId>
	<artifactId>sample-maven-build</artifactId>
	<version>1</version>
	<name>sample-maven-build</name>
	<build>
		<!-- Source directory configuration -->
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<!-- Following plugin executes the testng tests -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.14.1</version>
				<configuration>
					<!-- Suite testng xml file to consider for test execution -->
					<suiteXmlFiles>
						<suiteXmlFile>testng.xml</suiteXmlFile>
						<suiteXmlFile>suites-test-testng.xml</suiteXmlFile>
					</suiteXmlFiles>
				</configuration>
			</plugin>
			<!-- Compiler plugin configures the java version to be usedfor compiling 
				the code -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- Dependency libraries to include for compilation -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.3.1</version>
		</dependency>
	</dependencies>
</project>

Maven 可以与任何连续集成系统一起使用。 插件maven-surefire-plugin用于配置和执行测试。 这里,所述插件用于为 TestNG 测试配置testng.xmlsuites-test-testng.xml并生成测试报告。

插件maven-compiler-plugin用于帮助编译代码并使用特定的 JDK 版本进行编译。

4. TestNG Maven 示例

现在在命令提示符下使用命令“mvn test”在pom.xml文件上方执行。 您将获得以下结果。

C:\somepath\TestNGExamples>mvn test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model forcom.test.maven:sample-maven-build:jar:1
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 27, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-maven-build 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ sample-maven-build ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\BAML\DFCCUI\workspace_personal\TestNGExamples\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ sample-maven-build ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ sample-maven-build ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\BAML\DFCCUI\workspace_personal\TestNGExamples\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ sample-maven-build ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) @ sample-maven-build ---
[INFO] No tests to run.
[INFO] Surefire report directory: C:\BAML\DFCCUI\workspace_personal\TestNGExamples\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Test method one
Test method two
Before test Test One. Thread id is: 10
Before test Test Two. Thread id is: 11
Before test-class Test One. Thread id is: 10
Before test-class Test Two. Thread id is: 11
Sample test-method Test One. Thread id is: 10
Sample test-method Test Two. Thread id is: 11
After test-method  Test One. Thread id is: 10
After test-method  Test Two. Thread id is: 11
After test  Test One. Thread id is: 10
After test  Test Two. Thread id is: 11
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.86 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.346s
[INFO] Finished at: Wed Dec 03 12:02:26 IST 2014
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------

随时在pom.xml文件上方添加/更新,以满足您的需求。

学习愉快!