question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Factory with dataProvider changes order of iterations

See original GitHub issue

When a test class is instantiated by a constructor with @Factory annotation. Example:

public class TestTest {
    int num;

    @Factory(dataProvider = "data")
    public TestTest(int n) {
        num = n;
    }

    @DataProvider
    public static Object [][] data() {
        return new Object[][] {{1}, {2}, {3}, {4}};
    }

    @Test
    public void test() {
        System.out.println(num);
    }
}

Expected output would be

1
2
3
4

but real order is different upon every execution. It appears that it essentially depends on test class instance hashcodes, where the list of nodes is sorted by default toString() implementation in org.testng.internal.Graph.java:129.

If dataprovider is specified in the @Test instead of @Factory, the order remains correct.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
juherrcommented, Sep 19, 2015

Workarround: add toString() to TestTest.

The Comparator is on Graph.Node and used during Grapg#topologicalSort.

A comment says:

// Sort the nodes alphabetically to make sure that methods of the same class // get run close to each other as much as possible

0reactions
juherrcommented, Dec 13, 2018

@JoaoGFarias I supposed we can remove it. Can you make a pull request with the toString removal?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - TestNG - Factories and Dataproviders - Stack Overflow
I'm working at a software firm developing a test automation framework to replace our old spaghetti tangled system. Since our system requires a ......
Read more >
Changing test run sequence when using DataProvider
The idea here is to tie down factory to a data provider and then have the factory instantiate each of your test objects...
Read more >
Different return types of DataProvider (TestNG - Part 35)
In this session, I have practically demonstrated Different return types of DataProvider in TestNG (i.e. Object[][], Object[], ...
Read more >
How to Change a Test Name When Using TestNG DataProvider
You will only see the parameters that have been sent to the test function. But in case you implement a reporting mechanize, you...
Read more >
version 1.3 - Spock Framework Reference Documentation
Let's try and change the second condition to stack.size() == 2 . ... If a data provider returns multiple values per iteration (as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found