简体   繁体   中英

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. All @Test methods are in the relevant class (for example LoginTest.class). It run perfectly from the IDE but with Maven (mvn test) it just print BUILD SUCCESS but not run any test…

maven-surefire-plugin in POM.xml:

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

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:

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. From where you are running your maven command? Just go to your project directory where pom.xml is present and run your command, it should work fine. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM