Why is @AfterClass called after tests from other classes?

I tried to run the test cases in a specific order, but no luck. As I can see, methods annotated with @AfterClass are run after methods from another test:

 Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNGMapConfigurator@558e e9d6 RUN class com.example.testng.ITCaseOne.beforeClass() RUN class com.example.testng.ITCaseOne.someCase() RUN class com.example.testng.ITCaseTwo.beforeClass() RUN class com.example.testng.ITCaseTwo.someCase() RUN class com.example.testng.ITCaseOne.anotherCase() RUN class com.example.testng.ITCaseOne.afterClass() RUN class com.example.testng.ITCaseTwo.anotherCase() RUN class com.example.testng.ITCaseTwo.afterClass() Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.25 sec 

And it fails because all @Test of ITCaseTwo should only be called after ITCaseOne.afterClass() (because I use Selenium, and tests from one case should check the corresponding page).

My simple classes are:

 public class ITCaseOne { @BeforeClass public void beforeClass() { System.out.printf("RUN %s.beforeClass()\n", getClass()); } @AfterClass(alwaysRun = true) public void afterClass() { System.out.printf("RUN %s.afterClass()\n", getClass()); } @Test(groups = "std-one") public void someCase() { System.out.printf("RUN %s.someCase()\n", getClass()); } @Test(groups = "logic-one", dependsOnGroups = "std-one") public void anotherCase() { System.out.printf("RUN %s.anotherCase()\n", getClass()); } } 

and

 public class ITCaseTwo { @BeforeClass public void beforeClass() { System.out.printf("RUN %s.beforeClass()\n", getClass()); } @AfterClass(alwaysRun = true) public void afterClass() { System.out.printf("RUN %s.afterClass()\n", getClass()); } @Test(groups = "std-two") public void someCase() { System.out.printf("RUN %s.someCase()\n", getClass()); } @Test(groups = "logic-two", dependsOnGroups = "std-two") public void anotherCase() { System.out.printf("RUN %s.anotherCase()\n", getClass()); } } 

If this is important, I use maven-failsafe-plugin 2.12 and testng 6.4

(By the way, I am also trying to use a package file with preserve-order="true" , but it does not work for me.)

Thanks in advance!

+1
source share
3 answers

I was able to reproduce this behavior, this is a mistake. I'll see. In the meantime, commenting on one of the two dependent groups should correct the wrong behavior.

+2
source

Essentially, you have requirements for dependent methods. If you store them in separate classes, you can make @Test of the second class, which you want to run the second, to depend on one of the groups in the first class . So if you hold @Test (groups = "std-two", dependsOnGroups = "logic-one"), everything should work as you want.

+1
source

I found another way how to fix it using a test suite and preserve-order="true" (inspired by If you need to run tests from 2 classes from testng.xml, why does TestNG choose mehods randomly from classes? ).

Create src/test/config/testng.xml with the following contents

 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Selenium tests" preserve-order="true"> <test name="Test #1"> <classes> <class name="com.example.testng.ITCaseOne" /> </classes> </test> <test name="Test #2"> <classes> <class name="com.example.testng.ITCaseTwo" /> </classes> </test> </suite> 

Access it with maven-failsafe-plugin

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.12</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${basedir}/src/test/config/testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> 

And now it works as expected:

 RUN class com.example.testng.ITCaseOne.beforeClass() RUN class com.example.testng.ITCaseOne.someCase() RUN class com.example.testng.ITCaseOne.anotherCase() RUN class com.example.testng.ITCaseOne.afterClass() RUN class com.example.testng.ITCaseTwo.beforeClass() RUN class com.example.testng.ITCaseTwo.someCase() RUN class com.example.testng.ITCaseTwo.anotherCase() RUN class com.example.testng.ITCaseTwo.afterClass() 
+1
source

Source: https://habr.com/ru/post/900451/


All Articles