Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

java - How to run TestNG tests from main() in an executable jar?

I have an executable JAR which contains all dependencies and test classes. I've confirmed that the main() method is called when I execute the jar. I'm trying to add code to main() so that I can run a specific TestNG test class. From the documentation on TestNG.org this appears to be the way to do it:

    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] { com.some.path.tests.MyTests.class });
    testng.addListener(tla);
    testng.run();

My folder structure is typical:

    /src/main/java/Main.java
    /src/test/java/com/some/path/tests/MyTests.java

However when I try to compile it I get this error:

    java: /src/main/java/Main.java:46: package com.some.path.tests does not exist

Is there anyway I can alter my project so that testng.setTestClasses() in main() can access the test class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I used the following in my main() method and it worked.

    CommandLineOptions options = new CommandLineOptions();
    JCommander jCommander = new JCommander(options, args);

    XmlSuite suite = new XmlSuite();
    suite.setName("MyTestSuite");
    suite.setParameters(options.convertToMap());

    List<XmlClass> classes = new ArrayList<XmlClass>();
    classes.add(new XmlClass("com.some.path.tests.MyTests"));

    XmlTest test = new XmlTest(suite);
    test.setName("MyTests");
    test.setXmlClasses(classes);

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

    TestNG testNG = new TestNG();
    testNG.setXmlSuites(suites);
    testNG.run();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...