java - Maven TestNG - Command Line Execution Not Triggering @Before or @After Methods
问题描述
I am trying to execute my Maven project (created in Apache Netbeans) via command line.
Here is my pom.xml
file
<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">
<name>MavenAutomation</name>
<modelVersion>4.0.0</modelVersion>
<groupId>SampleAutomation</groupId>
<artifactId>MavenAutomation</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Global -->
<suiteXmlFile>src/test/java/TestNG/testExecution.xml</suiteXmlFile>
<argLine>-Dfile.encoding=UTF-8</argLine>
<!-- Dependency Versions -->
<testNGVersion>7.3.0</testNGVersion> <!-- TestNG -->
</properties>
<dependencies>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testNGVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<encoding>UTF8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Here is the testExecution.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="My Sample Test" verbose="10"> <!--@BeforeSuite runs here just once-->
<test name="Tests"> <!--@BeforeTest runs here just once-->
<groups>
<define name="include-group">
<include name="DEV" />
</define>
<define name="exclude-group">
<include name="UAT" />
</define>
<run>
<include name="include-group" />
<exclude name="exclude-group" />
</run>
</groups>
<packages>
<package name="myPackage.PackageName"/>
</packages>
<!-- <classes>
<class name="myPackage.PackageName.ClassName" />
</classes>-->
</test> <!--@AfterTest runs here just once-->
</suite> <!--@AfterSuite runs here just once-->
Here is my TestNGBase.java
class file
@Listeners(
{
TestNG_Listeners.ListenersTestNG.class
})
public class TestNGBase extends General_Helper
{
@BeforeTest
public void prepareTest()
{
System.out.println("Prepare Test");
}
@BeforeTest
public void setReporting()
{
System.out.println("Set Reporting");
}
@AfterMethod
public void resetVariables(ITestResult testResult)
{
System.out.println("Reset Variables");
}
@AfterTest
public void executionTearDown()
{
System.out.println("Execution Teardown");
}
}
Here is the class file that contains the @Test
annotation
public class REG001 extends TestNGBase
{
@Test(
description = "Testing TestNG DEV",
groups =
{
"All",
"DEV"
})
public void myTest_DEV()
{
method1();
method2();
method3();
}
}
When I run in Netbeans, I can see in the output pane it says this:
cd C:\MavenSelenium\MavenAutomation;
"JAVA_HOME=C:\\Program Files\\Java\\jdk-15.0.1"
cmd /c "\"C:\\Program Files\\NetBeans-12.2\\netbeans\\java\\maven\\bin\\mvn.cmd\"
-Dtest=myPackage.PackageName.ClassName#myTest_DEV
-Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans-12.2\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar\"
-Dfile.encoding=UTF-8 test-compile surefire:test"
Executing via Netbeans, all the @Before
and @After
annotations ARE executed just fine
When I run via command line, I tried with these commands:
mvn test
mvn test-compile
mvn test-compile surefire:test
Executing via Command Line, all the @Before
and @After
annotations are NOT executed.
I DO think I am missing a config or something in one of the XML files, but I honestly am not sure.
Looking at this post back in 2017, it seems that there was a similar issue back then, but I don't see if / how it was resolved or what the resolution was.
https://github.com/cbeust/testng/issues/1574
One option was to set this alwaysRun=true
by the @Test
annotation, but this too doesn't work.
For reference, I was using this guide on how to accomplish the Maven execution via Command Line:
https://howtodoinjava.com/testng/how-to-execute-testng-tests-with-maven-build/
解决方案
推荐阅读
- ios - 每次我在 Swift 中使用它时都必须启动一个 BLE 外围设备
- javascript - 来自 react-router-dom 的提示问题未触发
- mysql - 在存储过程中调用存储过程并将结果设置为变量时出现问题
- typescript - VSCode API:在终端中运行命令并使用输出
- python - 如何在 PyQt5 中为屏幕上的点移动设置动画
- javascript - 如何在 URL 中调用 echo 包括?
- django - TemplateDoesNotExist django 休息框架
- tensorflow - 无法安装tensorflow js
- python-3.x - 根据条件填充多个数据框列中的空值
- python - Python 中的 ElasticNetCV 与 R 中的 cvglmnet