首页 > 解决方案 > 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/

标签: javamaventestng

解决方案


推荐阅读