I have multiple test classes, which should be executed sequentially. I created testng.xml file with following content.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MyTestSuite1" verbose="2" parallel="methods" thread-count="1">
<listeners>
<listener class-name="utils.TestNGListener"></listener>
</listeners>
<test name="Regression" parallel="false" verbose="2">
<classes>
<class name="test.LoginTest" />
<class name="test.ClearTest" />
<class name="test.SendMessageTest" />
</classes>
</test>
</suite>
I created main() method for project to provide entry point.
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
TestNG testNG = new TestNG();
String xmlFileName = "testng.xml";
List<XmlSuite> suite = (List<XmlSuite>)(new Parser(xmlFileName).parse());
testNG.setXmlSuites(suite);
testNG.run();
}
I am not sure, how to execute test suite in specified order, got error message.
Exception in thread "main" org.testng.TestNGException: Cannot find class in classpath: test.LoginTest
Output of tree command:
C:.
├───.idea
│ └───libraries
├───META-INF
├───out
│ └───artifacts
├───resources
│ └───leanftjar
├───RunResults
│ └───Resources
│ ├───Snapshots
│ └───User
├───src
│ ├───main
│ │ ├───java
│ │ │ ├───hu
│ │ │ │ └───mysoft
│ │ │ ├───jar
│ │ │ │ └───META-INF
│ │ │ ├───META-INF
│ │ │ ├───unittesting
│ │ │ └───utils
│ │ └───resources
│ └───test
│ └───java
│ └───test
├───target
│ ├───classes
│ │ ├───hu
│ │ │ └───mysoft
│ │ ├───leanftjar
│ │ ├───unittesting
│ │ └───utils
│ ├───generated-sources
│ │ └───annotations
│ ├───generated-test-sources
│ │ └───test-annotations
│ ├───maven-status
│ │ └───maven-compiler-plugin
│ │ └───compile
│ │ └───default-compile
│ └───test-classes
│ └───test
└───test-output
├───All Test Suite
├───junitreports
└───old
└───All Test Suite
The problem is in your code. By default classes that reside under src/main/java don't have visibility into classes that reside in src/test/java. So when you create the TestNG instance in your main() method from src/main/java TestNG is trying to load classes from the same and since it cannot find them it's throwing the exception.
To fix this problem please move the class that contains your main() method into a package under src/test/java and try again. It will work.
In order to execute TestNG tests you should mark test method with org.testng.annotations.Test class
@Test
public void testMyMethod() {
For using testng.xml see example
// 1. To run with testng.xml file, uncomment this one, comment 2 testng.setTestSuites(Arrays.asList("testng.xml"));
For executing with tests order use setPreserveOrder:
testng.setPreserveOrder(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With