-
Notifications
You must be signed in to change notification settings - Fork 1k
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
@Factory with dataProvider changes order of iterations #799
Comments
The documentation doesn't specify anything about order and test classes are not supposed to be dependent. IMO, it is not a bug |
Maybe not a bug but inconsistent since the behavior is different between Cédric On Fri, Sep 11, 2015 at 8:26 AM, Julien Herr notifications@github.com
|
It is more about the expected order of data-driven iterations rather than dependency between them. If the data comes from an external source, it is only natural for a user to expect to see the iterations in the test report be in this same order. |
Workarround: add The A comment says:
|
Hi @juherr , Can you please elaborate the workaround you have mentioned. I am also facing the same problem as mentioned above, order of instances. |
@satyasrikanth In fact, I've already observed some different behaviors when we implement Then, adding: public String toString() {
return "TestTest[" + num + "]";
} may work (or not because I didn't test it). |
Still an issues in v6.11. Also, I tried adding a toString() method, and it did not give it alphabetical ordering. |
@BrandonDude - I tried the following using 6.11 and it works fine. Can you please help share a sample that is not working for you ? package com.rationaleemotions.github.issue799;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class TestTest {
private 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);
}
@Override
public String toString() {
return Integer.toString(num);
}
} Output
|
@krmahadevan Could you try with a randomized tostring? And maybe test with a randomized itest too? |
@juherr - What are you referring to when you say "randomized toString()" ? Can you please help elaborate so that I can try accordingly. |
@krmahadevan Sure! In fact, the order may be the expected one because of what if: public String toString() {
return Integer.toString(Math.abs(num - 4)); // pseudo random
} and same with public String getName() {
return "Test " + Integer.toString(Math.abs(num - 4)); // pseudo random
} |
That would obviously change the order randomly no ? Since toString() is
returning a random string every time. Just curious: what are we trying to
ascertain ?
|
The order is not supposed to be dependent to the test name. |
@juherr - I am still a bit confused with your expectation. I noticed the following as implementation of @Override
public int compareTo(Node<T> o) {
return getObject().toString().compareTo(o.getObject().toString());
}
Looking at the code, I think only I was thinking that maybe we could change the @Override
public int compareTo(Graph.Node<T> o) {
if ((getObject() instanceof Comparable) && (o.getObject() instanceof Comparable) ) {
return ((Comparable) getObject()).compareTo(o.getObject());
}
return getObject().toString().compareTo(o.getObject().toString());
} Please let me know your thoughts. |
@krmahadevan Yes, it is the cause of the issue. And I think it should be removed because I don't see the reason of it: order is managed by ordering features, and name is definitely not one of them :) But I may miss something too ;) |
@juherr - So shall i go ahead and raise a PR which includes a check to see if |
@krmahadevan First, try to remove the comparable first, if possible.
IMO, it should be managed outside, like an ordering feature (which should already exist with At least, you can replace |
This seems to be a good workaround. I am running 1 suite, 1 test and using @factory and @dataProvider to spawn 90 different classes with different City names. before, I could not force the listed order. Overriding the toString() method is working. I don't understand it, but it works in 6.11.1 |
If this warning and method still needed? |
@JoaoGFarias I supposed we can remove it. Can you make a pull request with the toString removal? |
When a test class is instantiated by a constructor with @factory annotation.
Example:
Expected output would be
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.
The text was updated successfully, but these errors were encountered: