如何通过 maven surefire 插件 运行 选定的 suiteXmlFile

How to run a selected suiteXmlFile via maven surefire plugin

我已经解决了所有关于 SO 的相关问题。但我认为我的问题与其他问题没有什么不同。

我有以下套件 xml 个文件

Initialize.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Start" verbose="1" parallel="tests"
    thread-count="5" configfailurepolicy="continue">


    <test name="Initialize" annotations="JDK" preserve-order="true">
        <parameter name="tags" value="not @42" />
        <classes>
            <class name="com.example.runner.InitializeExecution" />
        </classes>
    </test>

</suite>

Conclude.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Start" verbose="1" parallel="tests"
    thread-count="5" configfailurepolicy="continue">

    <test name="Conclude" annotations="JDK" preserve-order="true">
        <parameter name="tags" value="not @42" />
        <classes>
            <class name="com.example.runner.ConcludeExecution" />
        </classes>
    </test>

</suite>

smoke.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="smoke" verbose="1" parallel="tests"
    thread-count="3" configfailurepolicy="continue" data-provider-thread-count="2">


    <test name="Smoke" annotations="JDK" preserve-order="true">
        <parameter name="tags" value="@dm1" />
        <classes>
            <class name="com.example.runner.smoke" />
        </classes>
    </test>


</suite>

regression.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="smoke" verbose="1" parallel="tests"
    thread-count="3" configfailurepolicy="continue" data-provider-thread-count="2">


    <test name="Regression" annotations="JDK" preserve-order="true">
        <parameter name="tags" value="@dm1" />
        <classes>
            <class name="com.example.runner.regression" />
        </classes>
    </test>


</suite>

pom.xml 片段

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.21.0</version>
                    <configuration>
                        <properties>
                            <property>
                                <name>dataproviderthreadcount</name>
                                <value>2</value>
                            </property>
                        </properties>
                        <suiteXmlFiles>
                            <suiteXmlFile>Initialize.xml</suiteXmlFile>
                            <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                            <suiteXmlFile>Conclude.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <!-- <scope>compile</scope> -->
</dependency>

我需要依次执行 initialize.xml 然后 smoke.xml 或 regression.xml 然后 conclude.xml。 以下面的集合为例:

<suiteXmlFiles>
     <suiteXmlFile>Initialize.xml</suiteXmlFile>
     <suiteXmlFile>smoke.xml</suiteXmlFile>
               <!-- or -->
<!-- <suiteXmlFile>regression.xml</suiteXmlFile> -->
     <suiteXmlFile>Conclude.xml</suiteXmlFile>
</suiteXmlFiles>

如何从 pom 参数化中间的 suiteXmlFile?我尝试使用以下命令但它不起作用并抛出异常。

mvn clean test -Dsurefire.suiteXmlFile=smoke.test
[ERROR] org.apache.maven.surefire.booter.SurefireExecutionException: Exception in provider
[ERROR]     at org.apache.maven.plugin.surefire.InPluginVMSurefireStarter.runSuitesInProcess(InPluginVMSurefireStarter.java:91)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1132)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:978)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:854)
[ERROR]     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR]     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR]     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
[ERROR]     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:290)
[ERROR]     at org.apache.maven.cli.MavenCli.main(MavenCli.java:194)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.lang.reflect.Method.invoke(Method.java:498)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[ERROR] Caused by: java.lang.NullPointerException
[ERROR]     at org.apache.maven.surefire.testng.TestNGXmlTestSuite.locateTestSets(TestNGXmlTestSuite.java:97)
[ERROR]     at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:119)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.lang.reflect.Method.invoke(Method.java:498)
[ERROR]     at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:206)
[ERROR]     at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:160)
[ERROR]     at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:83)
[ERROR]     at org.apache.maven.plugin.surefire.InPluginVMSurefireStarter.runSuitesInProcess(InPluginVMSurefireStarter.java:87)
[ERROR]     ... 25 more
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

如果我使用下面的命令它正在工作但不执行 initialize.xml 和 conclude.xml (注意命令中的 s

mvn clean test -Dsurefire.suiteXmlFiles=smoke.test

我也试过像下面那样创建不同的套件 xml 并通过命令行传递它,但它没有像我预期的那样工作。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

 <suite name="Smoke Suite"  > 
    <suite-files>
           <suite-file path="./Initialize.xml" />
           <suite-file path="./somke.xml" />
           <suite-file path="./Conclude.xml" />
    </suite-files>
</suite>

请帮忙。也感谢任何参考或有用的链接。

我们可以做类似下面的事情吗,其中大部分你已经做过了:

<suiteXmlFiles>
     <suiteXmlFile>Initialize.xml</suiteXmlFile>
     <suiteXmlFile>${smokeORregression}</suiteXmlFile>
     <suiteXmlFile>Conclude.xml</suiteXmlFile>
</suiteXmlFiles>

然后根据需要执行mvn clean test -DsmokeORregression=smoke.xmlmvn clean test -DsmokeORregression=regression.xml

您不需要 Surefire 在 mvn 命令中。