Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution order changed from version 6.0.1 to 6.8 #288

Closed
ajay516 opened this issue Oct 6, 2012 · 12 comments · Fixed by #1475
Closed

Execution order changed from version 6.0.1 to 6.8 #288

ajay516 opened this issue Oct 6, 2012 · 12 comments · Fixed by #1475

Comments

@ajay516
Copy link

ajay516 commented Oct 6, 2012

I just upgraded my project (from 6.0.1 to 6.8) and found that the order of execution of tests are changed. In version of 6.0.1, the order of execution is:

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNGMapConfigurator@72ad8bfd
main     BaseTest(Actual1Test) - beforeClass
main  Actual1Test(Actual1Test) - test
main  Actual1Test(Actual1Test) - test3
main  Actual1Test(Actual1Test) - test4 - one
main  Actual1Test(Actual1Test) - test4 - two
main  Actual1Test(Actual1Test) - test4 - three
main  Actual1Test(Actual1Test) - test4 - four
main     BaseTest(Actual1Test) - afterClass
main     BaseTest(Actual2Test) - beforeClass
main  Actual2Test(Actual2Test) - test
main     BaseTest(Actual2Test) - afterClass
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.894 sec

In version 6.8, the order becomes:

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@3301b455
main     BaseTest(Actual1Test) - beforeClass
main  Actual1Test(Actual1Test) - test
main  Actual1Test(Actual1Test) - test4 - one
main  Actual1Test(Actual1Test) - test4 - two
main  Actual1Test(Actual1Test) - test4 - three
main  Actual1Test(Actual1Test) - test4 - four
main     BaseTest(Actual2Test) - beforeClass
main  Actual2Test(Actual2Test) - test
main     BaseTest(Actual2Test) - afterClass
main  Actual1Test(Actual1Test) - test3
main     BaseTest(Actual1Test) - afterClass
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.478 sec

Here are my observations:

  1. All the tests and @BeforeXXX methods are executed together in older version. In the new version, the execution order is mixed
  2. I am creating database in @BeforeClass of base class and tearing it @afterclass methods of BaseClass. When @BeforeClass is called twice, it throws error stating that the database already exists. The codebase I have attached removed that complications and just demonstrates the execution order.
  3. I have also observed that at times, the execution order is same as of old one - while I am not able to figure out why.

Here is my execution environment:

Apache Maven 3.0.4 (r1232337; 2012-01-17 14:14:56+0530)
Maven home: /opt/local/share/java/maven3
Java version: 1.7.0_06, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.7.5", arch: "x86_64", family: "mac"
@ajay516
Copy link
Author

ajay516 commented Oct 6, 2012

@VladRassokhin
Copy link
Contributor

Yeah, i can reproduce that.

Probably workaround: use testng.xml with preserve-order="true".

After some investigations i have found that "dependOnMethods" matter. Without dependency all works fine. Looks like TestNG incorrectly reorders test methods to run.

Simplified code you can find at VladRassokhin/testng-execution-order

@riemenschneider
Copy link

Is there a workaround (except removing all dependsOnMethods)?

@ShijunK
Copy link

ShijunK commented May 3, 2013

any clue of the root cause? action plan? The reason (at least for my use case:selenium ui testing) to choose testng over junit is tests dependencies.

order all tests in xml is not feasible for large project with hundreds, thousands tests.

@cedricrefresh
Copy link
Contributor

If you want precise ordering, you should use dependsOnGroups, not XML (which was never designed to specify ordering).

@ShijunK
Copy link

ShijunK commented May 3, 2013

dependsOnGroups does not work, at least not as my understanding.
with parallel="false", I expect suite runs all tests in single thread, by the order defined either java5 annotations or xml (with preserve-order). depended groups/tests should be executed only once.

given one base class, and two sub classes, depended group in base class

the expected output:

beforeSuite
init
testA1
testA2
testB1
testB2

actual output:

beforeSuite
init
init
testA1
testA2
testB1
testB2

===============================================
Custom suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

suite.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Custom suite" parallel="false">
    <test name="Test dependencies">
        <classes>
            <class name="SubClassA"/>
            <class name="SubClassB"/>
        </classes>
    </test>
</suite>
public class BaseClass {
    @BeforeSuite
    public void beforeSuite() {
        System.out.println("beforeSuite");
    }

    @Test(groups = {"init"})
    public void init(){
        System.out.println("init");
    }
}
public class SubClassA extends BaseClass {
    @Test(dependsOnGroups = {"init.*"})
    public void testA1() {
        System.out.println("testA1");
    }

    @Test(dependsOnMethods = "testA1")
    public void testA2() {
        System.out.println("testA2");
    }
}
public class SubClassB extends BaseClass{
    @Test(dependsOnGroups = {"init.*"})
    public void testB1() {
        System.out.println("testB1");
    }

    @Test(dependsOnMethods = "testB1")
    public void testB2(){
        System.out.println("testB2");
    }
}

tell me, if I understand the documentation in a wrong way. Appreciate, if anyone can tell me how to properly use test dependencies in testng.

@cbeust
Copy link
Collaborator

cbeust commented May 3, 2013

The ordering looks correct to me. What you seem to be wanting is atomicity (invoking groups of methods together)?

@ShijunK
Copy link

ShijunK commented May 3, 2013

how could it be right? per http://testng.org/doc/documentation-main.html#testng-xml
"TestNG will run your tests in the order they are found in the XML file"

my understand is, TestNG executes tests in the order found in the XML file, within each test, annotations (dependsOnGroup, dependsOnMethods) will decide the order.

so, I have SubClassA before SubClassB in xml, I expect TestNG execute SubClassA before doing anything about SubClassB. no?

@cbeust
Copy link
Collaborator

cbeust commented May 3, 2013

The depend annotations override anything that is found in the XML. Either you use preserve-order=true in your XML file or you define dependencies in Java.

@ShijunK
Copy link

ShijunK commented May 3, 2013

Thanks for the clarification. One more question, when you say "override anything that is found in the XML", that also apply to "group-by-instances" / "order-by-instances"? There is no way to archive it by using annotations?

btw, should it be "group-by_instances" or "order-by-instances"? Neither of them exists in http://testng.org/testng-1.0.dtd

from http://testng.org/doc/documentation-main.html

For this ordering, you can use the XML attribute group-by-instances. This attribute is valid either on <suite> or <test>:
  <suite name="Factory" order-by-instances="true">
or
  <test name="Factory" order-by-instances="true">

@adimoshu
Copy link

adimoshu commented Apr 2, 2014

Hello,

I am using the latest version of TestNG (6.8.8) and I don't know how to solve an issue that I have.
I have on Jenkins a job that executes some automation tests, These are TestNG tests and they are part of a suite file (testNG.xml). The execution order is the one from the suite file, but when I look into the testng-result.xml and then in emailable-report.html, the tests are in a random order.

I would like the tests from the reports to be in the order that they have executed. Can you please help me in solving this issue?

Thank you

@ruslan-simonenko
Copy link

Hello,

I've created a pull request that, I believe, fixes this issue:
#554

juherr added a commit to juherr/testng that referenced this issue Jul 15, 2017
@juherr juherr mentioned this issue Jul 15, 2017
2 tasks
cbeust added a commit that referenced this issue Jul 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants