forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@factory with dataProvider changes order of iterations
Closes testng-team#799
- Loading branch information
1 parent
d61e30c
commit 13275dd
Showing
9 changed files
with
289 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.testng.internal; | ||
|
||
import org.testng.ITestNGMethod; | ||
|
||
import java.util.Comparator; | ||
|
||
public class Systematiser { | ||
|
||
private Systematiser() { | ||
//Utility class. Defeat instantiation. | ||
} | ||
|
||
public static Comparator<Graph.Node> getComparator() { | ||
Comparator<Graph.Node> comparator = null; | ||
String text = System.getProperty("testng.order", Order.INSTANCES.getValue()); | ||
|
||
Order order = Order.parse(text); | ||
switch (order) { | ||
case INSTANCES: | ||
comparator = new Comparator<Graph.Node>() { | ||
@Override | ||
public int compare(Graph.Node o1, Graph.Node o2) { | ||
return o1.getObject().toString().compareTo(o2.getObject().toString()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Instance_Names"; | ||
} | ||
}; | ||
break; | ||
|
||
case METHOD_NAMES: | ||
comparator = new Comparator<Graph.Node>() { | ||
@Override | ||
public int compare(Graph.Node o1, Graph.Node o2) { | ||
if (o1.getObject() instanceof ITestNGMethod && o2.getObject() instanceof ITestNGMethod) { | ||
String n1 = ((ITestNGMethod) o1.getObject()).getMethodName(); | ||
String n2 = ((ITestNGMethod) o1.getObject()).getMethodName(); | ||
return n1.compareTo(n2); | ||
} | ||
return o1.getObject().getClass().getName().compareTo(o2.getObject().getClass().getName()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Method_Names"; | ||
} | ||
}; | ||
break; | ||
|
||
case NONE: | ||
default: | ||
} | ||
|
||
return comparator; | ||
} | ||
|
||
enum Order { | ||
METHOD_NAMES("methods"), | ||
INSTANCES("instances"), | ||
NONE("none"); | ||
|
||
Order(String value) { | ||
this.value = value; | ||
} | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public static Order parse(String value) { | ||
if (value == null || value.trim().isEmpty()) { | ||
return INSTANCES; | ||
} | ||
for (Order each : values()) { | ||
if (each.getValue().equalsIgnoreCase(value)) { | ||
return each; | ||
} | ||
} | ||
return INSTANCES; | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/test/github799/EnsureInstancesAreOrderedViaFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package test.github799; | ||
|
||
import org.testng.Assert; | ||
import org.testng.IInvokedMethod; | ||
import org.testng.IInvokedMethodListener; | ||
import org.testng.ITestNGListener; | ||
import org.testng.ITestResult; | ||
import org.testng.Reporter; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.Test; | ||
import org.testng.collections.Lists; | ||
import test.SimpleBaseTest; | ||
|
||
import java.util.List; | ||
|
||
public class EnsureInstancesAreOrderedViaFactories extends SimpleBaseTest { | ||
|
||
@Test | ||
public void testMethod() { | ||
System.setProperty("testng.order", "none"); | ||
runTest(TestSample.class, "1", "2", "3", "4"); | ||
} | ||
|
||
@Test | ||
public void randomOrderTestMethod() { | ||
System.setProperty("testng.order", "none"); | ||
runTest(ReverseOrderTestSample.class, "4", "1", "3", "2"); | ||
} | ||
|
||
@Test | ||
public void methodsOrderTest() { | ||
System.setProperty("testng.order", "methods"); | ||
runTest(MethodsTestSample.class, "android", "angry", "birds"); | ||
} | ||
|
||
@Test | ||
public void testInstancesOrder() { | ||
System.setProperty("testng.order", "instances"); | ||
runTest(InstanceTestSample.class, "Master Oogway:90", "Master Shifu:50"); | ||
} | ||
|
||
private void runTest(Class<?> clazz, String... expected) { | ||
TestNG tng = create(clazz); | ||
OrderEavesdropper listener = new OrderEavesdropper(); | ||
tng.addListener((ITestNGListener) listener); | ||
tng.run(); | ||
|
||
for (int i = 0; i < expected.length; i++) { | ||
String actual = listener.messages.get(i); | ||
Assert.assertEquals(actual, expected[i]); | ||
} | ||
} | ||
|
||
public static class OrderEavesdropper implements IInvokedMethodListener { | ||
List<String> messages = Lists.newArrayList(); | ||
|
||
@Override | ||
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { | ||
} | ||
|
||
@Override | ||
public void afterInvocation(IInvokedMethod method, ITestResult testResult) { | ||
messages.addAll(Reporter.getOutput(testResult)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test.github799; | ||
|
||
import org.testng.Assert; | ||
import org.testng.Reporter; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Factory; | ||
import org.testng.annotations.Test; | ||
|
||
public class InstanceTestSample { | ||
private String name; | ||
private int age; | ||
|
||
@Factory(dataProvider = "dp") | ||
public InstanceTestSample(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
@DataProvider(name = "dp") | ||
public static Object[][] getData() { | ||
return new Object[][]{ | ||
{"Master Shifu", 50}, | ||
{"Master Oogway", 90} | ||
}; | ||
} | ||
|
||
@Test | ||
public void testMethod() { | ||
Reporter.log(toString()); | ||
Assert.assertNotNull(this.name); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name + ":" + age; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package test.github799; | ||
|
||
import org.testng.Assert; | ||
import org.testng.Reporter; | ||
import org.testng.annotations.Test; | ||
|
||
public class MethodsTestSample { | ||
@Test | ||
public void angry() { | ||
String methodName = Reporter.getCurrentTestResult().getMethod().getMethodName(); | ||
Reporter.log(methodName); | ||
Assert.assertNotNull(methodName); | ||
} | ||
|
||
@Test | ||
public void birds() { | ||
String methodName = Reporter.getCurrentTestResult().getMethod().getMethodName(); | ||
Reporter.log(methodName); | ||
Assert.assertNotNull(methodName); | ||
} | ||
|
||
@Test | ||
public void android() { | ||
String methodName = Reporter.getCurrentTestResult().getMethod().getMethodName(); | ||
Reporter.log(methodName); | ||
Assert.assertNotNull(methodName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package test.github799; | ||
|
||
import org.testng.Assert; | ||
import org.testng.Reporter; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Factory; | ||
import org.testng.annotations.Test; | ||
|
||
public class ReverseOrderTestSample { | ||
int num; | ||
|
||
@Factory(dataProvider = "data") | ||
public ReverseOrderTestSample(int n) { | ||
num = n; | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] data() { | ||
return new Object[][]{{4}, {1}, {3}, {2}}; | ||
} | ||
|
||
@Test | ||
public void test() { | ||
Reporter.log(Integer.toString(num)); | ||
Assert.assertTrue(num > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package test.github799; | ||
|
||
import org.testng.Assert; | ||
import org.testng.Reporter; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Factory; | ||
import org.testng.annotations.Test; | ||
|
||
public class TestSample { | ||
int num; | ||
|
||
@Factory(dataProvider = "data") | ||
public TestSample(int n) { | ||
num = n; | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] data() { | ||
return new Object[][]{{1}, {2}, {3}, {4}}; | ||
} | ||
|
||
@Test | ||
public void test() { | ||
Reporter.log(Integer.toString(num)); | ||
Assert.assertTrue(num > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters