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

build: ensure order of tests using the @DataProvider annotation #12

Closed
bitcoder opened this issue Apr 8, 2024 · 5 comments
Closed

build: ensure order of tests using the @DataProvider annotation #12

bitcoder opened this issue Apr 8, 2024 · 5 comments

Comments

@bitcoder
Copy link

bitcoder commented Apr 8, 2024

Review test code, namely of the datadriven tests, to ensure the order of tests and avoid flakiness on the build.

See:

@itkhanz
Copy link

itkhanz commented Apr 15, 2024

just curious as to how the order of tests is effecting the Xray extension?

regardless of the order of execution, the last data/example {5, true} will always fail. Also in the test method body, we are just attaching the same attachment as attribute for all the examples.

Perhaps I wasn't able to spot whats the linkage of this issue with build/release? Could you please elaborate a little whats the issue here so maybe i can also take a detailed look to investigat the problem

@bitcoder
Copy link
Author

The problem is with this datadriven scenario.
TestNG doesn't ensure the order of the created tests... so sometimes the build passes, sometimes it fails because this test fails.

@itkhanz
Copy link

itkhanz commented Apr 15, 2024

Thank you for sharing the detailed issue. So if I understood correctly, the build failed because of the random order of iterations in which the TestNG executes the tests from DataProvider.

We have hardcoded expected results in our test, and we expect the actual results iterations to be in ascending order (as in Data Provider). We should be able to solve this problem by implementing custom comparator for our use iterations object which sorts the iterations based on number parameter's integer value before performing assertions.

JSONArray iterations = (JSONArray)actualTest.getJSONArray("iterations");

Here is how the solution may look like:

JSONArray iterations = (JSONArray) actualTest.getJSONArray("iterations");

// Sort the iterations based on the "number" parameter value
iterations.sort(new Comparator<Object>() {
    @Override
    public int compare(Object o1, Object o2) {
        JSONObject iteration1 = (JSONObject) o1;
        JSONObject iteration2 = (JSONObject) o2;

        int number1 = iteration1.getJSONObject("parameters")
                           .getJSONArray("parameters")
                           .getJSONObject(0)
                           .getInt("value");

        int number2 = iteration2.getJSONObject("parameters")
                           .getJSONArray("parameters")
                           .getJSONObject(0)
                           .getInt("value");

        return Integer.compare(number1, number2);
    }
});

Now that the iterations is sorted, the next set of assert statements should be passed our iterations is now in order.

I look forward to your feedback if you could please try this solution and let me know if it works.

@bitcoder
Copy link
Author

Thanks @itkhanz ; I applied a workaround based on that code snippet with some adjustments.

@bitcoder
Copy link
Author

released in 0.2.0

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

No branches or pull requests

2 participants