3

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 
10
  • You are seemingly referencing a class test.LoginTest in your Regressiontests. Does this class exist? Commented May 18, 2018 at 11:04
  • Yes, sure, it is located in test named package. Commented May 18, 2018 at 11:10
  • Okay, just wanted to check the very basics, you never know :) Commented May 18, 2018 at 11:10
  • Maybe the answers on this question can help. Commented May 18, 2018 at 11:11
  • @Ben : No, I am looking for example, where there are more than one test classes. Commented May 20, 2018 at 11:46

3 Answers 3

6
+50

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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); 

3 Comments

I edited main() method, but got error: Cannot find class in classpath: test.LoginTest.
Test classes are under src\test\java\test. All other classes: src\main\java\com\mysoft.
@plaidshirt - Please check my answer.
1

If it is a hard requirement to run these steps in order, for your test to pass, it seems that they should be implemented internally in the test.

@Test public void doIt() { login(); clearMessage(); sendMessage(); } 

With the appropriate checks for success is going to fail as a unit, not hide the actions from the test maintainers, and provide better debugging output in the event of a test failure than "three tests stitched together as one".

1 Comment

Yes, but in this case I use simple method calls from a test class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.