plaidshirt
plaidshirt

Reputation: 5671

Execute tests sequentially in TestNG

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

Upvotes: 3

Views: 3625

Answers (3)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14736

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.

Upvotes: 6

Edwin Buck
Edwin Buck

Reputation: 70909

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".

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58774

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

Upvotes: 2

Related Questions