maven testng-命令行执行不触发@before或@after方法

6mw9ycah  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(280)

我试图通过命令行执行我的maven项目(在apachenetbeans中创建)。
这是我的 pom.xml 文件

<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>

这是你的名字 testExecution.xml ```

<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-->

这是我的 `TestNGBase.java` 类文件

@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");
}

}

下面是包含 `@Test` 注解

public class REG001 extends TestNGBase
{

@Test(
        description = "Testing TestNG DEV",
        groups =
        {
            "All",
            "DEV"
        })
public void myTest_DEV()
{
    method1();
    method2();
    method3();
}

}

当我在netbeans中运行时,我可以在输出窗格中看到这样的内容:

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"

通过netbeans执行,所有 `@Before` 以及 `@After` 注解执行得很好
当我通过命令行运行时,我尝试了以下命令:

mvn test
mvn test-compile
mvn test-compile surefire:test

通过命令行执行,所有 `@Before` 以及 `@After` 不执行注解。
我确实认为在其中一个xml文件中丢失了一个配置或其他东西,但我真的不确定。
回顾2017年的这篇文章,当时似乎也有类似的问题,但我看不出是否/如何解决,或者解决方案是什么。
https://github.com/cbeust/testng/issues/1574
一个选择是设置这个 `alwaysRun=true` 由 `@Test` 注解,但这也不起作用。
为了便于参考,我使用了以下指南,介绍如何通过命令行完成maven执行:
https://howtodoinjava.com/testng/how-to-execute-testng-tests-with-maven-build/

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题