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?
You can load your usual xml in main using
org.testng.xml.Parser
andorg.testng.xml.XmlSuite
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.
I used the following in my main() method and it worked.