Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 935
TERACytE Avatar asked May 09 '13 15:05

TERACytE


People also ask

How do you run a TestNG class from the main method?

More details here. // Create object of TestNG Class TestNG runner=new TestNG(); // Create a list of String List<String> suitefiles=new ArrayList<String>(); // Add xml file which you have to execute suitefiles. add("C:\\Automation Test\\Git\\vne_automation\\testng. xml"); // now set xml file for execution runner.

Does TestNG require main method execute?

As main() method is needed to run the Java program and while writing tests in TestNg we don't use main() method,we use Annotations instead. Annotations in TestNG are lines of code that can control how the method below them will be executed. So, in short you don't need to write main() method, TestNg do that by itself.

Can you run a TestNG test without the TestNG XML file?

When we assign group or groups to test methods in TestNG, to include or exclude groups to run we use TestNG xml. Do you know that we can run groups without TestNG xml and can include, exclude groups easily compare to updation in TestNG xml? Yes, we can do.


3 Answers

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();
like image 152
TERACytE Avatar answered Nov 15 '22 10:11

TERACytE


You can load your usual xml in main using org.testng.xml.Parser and org.testng.xml.XmlSuite

String xmlFileName = "testng.xml";
List<XmlSuite> suite;
try
{
    suite = (List <XmlSuite>)(new Parser(xmlFileName).parse());
    testng.setXmlSuites(suite);
    testng.run();
}
catch (ParserConfigurationException e)
{
    e.printStackTrace();
}
catch (SAXException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
}
like image 40
KCD Avatar answered Nov 15 '22 11:11

KCD


If that is your folder structure, and not just a type, it's wrong. The package name is represented as a folder structure, not one folder with the package name.

So it should be src/test/java/com/some/path/tests/MyTests.java

Also, make sure your test classes are actually in the Jar file. If you're using maven to build the Jar, your test classes will not be included by default.

like image 43
NilsH Avatar answered Nov 15 '22 10:11

NilsH