简体   繁体   English

如何使用 maven mvn 测试命令行运行动态 testng.xml?

[英]How to run dynamic testng.xml using maven mvn test command line?

I created a test runner that create a dynamic testng xml so I can run tests suits with parameters.我创建了一个创建动态 testng xml 的测试运行器,因此我可以运行带有参数的测试套装。 All @Test methods are in the relevant class (for example LoginTest.class).所有@Test 方法都在相关的 class 中(例如 LoginTest.class)。 It run perfectly from the IDE but with Maven (mvn test) it just print BUILD SUCCESS but not run any test…它从 IDE 完美运行,但使用 Maven(mvn 测试)它只打印 BUILD SUCCESS 但不运行任何测试......

maven-surefire-plugin in POM.xml: POM.xml 中的 maven-surefire-plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M3</version>
  <configuration>
<environmentVariables>
  <suite>LoginTest</suite>
</environmentVariables>
     <includes>
        <include>src/test/java/runner/TestRunner.java</include>
     </includes>
  </configuration>
</plugin>

My TestRunner class:我的TestRunner class:

public class TestRunner {

   public static void main(String[] args) {
       XmlSuite suite = new XmlSuite();
       suite.setName("D-Web");
       suite.setThreadCount(1);
       suite.setVerbose(1);
       //suite.setParallel("tests");
       suite.addListener("com.app.listeners.ReportListeners");

       XmlTest test = new XmlTest(suite);
       test.setName("Tests");
       test.setPreserveOrder(true);

       XmlClass testClass = getTestClass();

       List<XmlClass> classes = new ArrayList<>();
       classes.add(testClass);
       test.setXmlClasses(classes);

       TestNG testng = new TestNG();
       List<XmlSuite> suites = new ArrayList<>();
       suites.add(suite);

       testng.setXmlSuites(suites);
       testng.run();
   }

   public static XmlClass getTestClass() {
       switch (System.getenv("suite")) {
           case "LoginTest":
           default:
               return new XmlClass(LoginTest.class);
           case "PurchaseFunnelTest":
               return new XmlClass(PurchaseFunnelTest.class);
           case "QuestionnaireTests":
               return new XmlClass(QuestionnaireTest.class);
       }
   }
}

My LoginTest class:我的登录测试 class:

public class LoginTest {

   @Test
   public void test1() {
            System.out.println("Test 1 is running...");
        }
}

I have just tried your project and it is running from mvn test command.我刚刚尝试了您的项目,它是从mvn test命令运行的。 From where you are running your maven command?从哪里运行 maven 命令? Just go to your project directory where pom.xml is present and run your command, it should work fine.只需将 go 放到 pom.xml 所在的项目目录并运行您的命令,它应该可以正常工作。 Also I have added your LoginTest class in runner package along with your TestRunner class and it is working fine with IDE as well as command line. Also I have added your LoginTest class in runner package along with your TestRunner class and it is working fine with IDE as well as command line.

Please test again and let me know.请再次测试并告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Maven 命令行运行 testng.xml - How to run testng.xml from Maven command line 使用testng.xml使用maven运行单个TestNG测试 - Run single TestNG test with maven by using testng.xml 如何从命令提示符下运行Maven项目testng.xml - how to run maven project testng.xml from the command prompt 如何使用命令提示符运行 testng.xml - How to run testng.xml using command prompt 如何通过mvn命令行传递testng.xml参数值 - how to pass testng.xml parameter value through mvn command line 如何使用 testng.xml 运行测试文件 - How to run the test files by using the testng.xml TestNG.XML 未执行。当通过 maven 运行时 -mvn 测试 - 即使在 POM.Z350FZ6093D396318 中正确配置后也是如此 - TestNG.XML not executing.when run via maven -mvn test - even after proper configurations in POM.XML 如何将动态参数传递给 testNG.xml 运行多个测试 - How to pass dynamic parameter TO testNG.xml run multiple tests 当我从 testng.xml 运行我的测试套件时,我在 Allure 报告中看到了步骤。 但是当使用“mvn clean test”时我没有看到任何步骤 - When I run my test suite from testng.xml, I see Steps in the Allure report. But when using "mvn clean test" I don't see any steps 无法在 Cucumber-Maven(TestNG)中运行 testng.xml - Can't run testng.xml in Cucumber-Maven(TestNG)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM