java 如何参数化 Maven surefire 插件,以便我可以选择要运行的 TestNG 套件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11397315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parametrize Maven surefire plugin so I can choose which TestNG suites to run
提问by maestr0
I've got many test suites in TestNG. These are XML files. I want to be able to choose multiple XML suites when running integration-test from maven.
我在 TestNG 中有很多测试套件。这些是 XML 文件。我希望在从 maven 运行集成测试时能够选择多个 XML 套件。
Currently I can add the suite files to pom.xml like this:
目前我可以像这样将套件文件添加到 pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${pathToMySuiteFile_1}</suiteXmlFile>
<suiteXmlFile>${pathToMySuiteFile_1}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
This solution has some limitations. I can only change a path to the test suite I've got defined in pom.xml. So in my example it always has to be two files. I'm not able to run, lets say, 5 suites or just one.
此解决方案有一些限制。我只能更改我在 pom.xml 中定义的测试套件的路径。所以在我的例子中,它总是必须是两个文件。我无法运行,比如说,5 套或仅一套。
Is there a way to somehow parametrize the whole section "suiteXmlFiles" in pom.xml ?
有没有办法以某种方式对 pom.xml 中的整个部分“suiteXmlFiles”进行参数化?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
${multiple_paths_ToMySuiteFiles}
</suiteXmlFiles>
</configuration>
</plugin>
Running everything that matches given test group is not an option for me: I don't want to load all the suites I've got and then run just the selected tests using groups in TestNG suite. The reason being that a report that gets generated after running all the test suites with group filters is different from a report when just the selected test suites were run.
运行与给定测试组匹配的所有内容对我来说不是一个选择:我不想加载我拥有的所有套件,然后使用 TestNG 套件中的组仅运行选定的测试。原因是在使用组过滤器运行所有测试套件后生成的报告与仅运行所选测试套件时生成的报告不同。
回答by norbert
According to User Propertyof suiteXmlFiles
You can use:
根据您的用户属性,suiteXmlFiles
您可以使用:
mvn test -Dsurefire.suiteXmlFiles=test1.xml,test2.xml
回答by mekdev
We also hit this issue with our tests. The current workaround that we use now is to define a property variable in the property section and inject it in sure-fire suiteXmlFiles block.
我们的测试也遇到了这个问题。我们现在使用的当前解决方法是在属性部分定义一个属性变量,并将其注入到万无一失的suiteXmlFiles 块中。
<properties>
<!-- Default suites -->
<batsSuiteFile>${project.build.testOutputDirectory}/BatsTests.xml</batsSuiteFile>
<smokeSuiteFile>${project.build.testOutputDirectory}/SmokeTests.xml</smokeSuiteFile>
<!-- Default suite files if not being specified from mvn command line -->
<defaultSuiteFiles>${batsSuiteFile},${smokeSuiteFile}</defaultSuiteFiles>
<suiteFile>${defaultSuiteFiles}</suiteFile>
</properties>
Then in the plugin section...
然后在插件部分...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<suiteXmlFiles>
<!-- Suite file injection parameter from command line -->
<suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
If nothing is specified from the command line it will fall back and default to the 2 suites specified above in the properties section. Then if you want to kick off a specified set of suite files you can do:
如果没有从命令行指定任何内容,它将回退并默认使用上面属性部分中指定的 2 个套件。然后,如果您想启动一组指定的套件文件,您可以执行以下操作:
mvn test -DsuiteFile=test1.xml,test2.xml
Surprisingly from the maven docs you expect that the arg suiteXmlFiles should just override this from the command line and should just accept a comma delimited list of testng xmls http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#suiteXmlFiles
令人惊讶的是,从 maven 文档中,您期望 argsuiteXmlFiles 应该从命令行覆盖它,并且应该只接受逗号分隔的 testng xmls 列表 http://maven.apache.org/plugins/maven-surefire-plugin/test- mojo.html#suiteXmlFiles
If anyone has any other better way please share.
如果有人有其他更好的方法,请分享。
回答by Eugene Kuleshov
You can use groupsparameter or specify -Dgroups=... in the command line:
您可以使用groups参数或在命令行中指定 -Dgroups=... :
(TestNG/JUnit47 provider with JUnit4.8+ only) Groups for this test. Only classes/methods/etc decorated with one of the groups specified here will be included in test run, if specified. For JUnit, this parameter forces the use of the 4.7 provider This parameter is ignored if the suiteXmlFiles parameter is specified. .
(仅限 JUnit4.8+ 的 TestNG/JUnit47 提供程序)此测试的组。如果指定,只有用此处指定的组之一修饰的类/方法/等才会包含在测试运行中。对于 JUnit,此参数强制使用 4.7 提供程序。如果指定了suiteXmlFiles 参数,则会忽略此参数。.