Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: DataProvider can now take an ITestContext parameter
- Loading branch information
cbeust
committed
Dec 28, 2006
1 parent
e58af1d
commit 10efd59
Showing
11 changed files
with
178 additions
and
43 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
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
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,50 @@ | ||
package test.dataprovider; | ||
|
||
import org.testng.Assert; | ||
import org.testng.ITestContext; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Tests that when a DataProvider is declared with an ITestContext, | ||
* this parameter is correctly passed. | ||
* | ||
* Created on Dec 28, 2006 | ||
* @author <a href="mailto:cedric@beust.com">Cedric Beust</a> | ||
*/ | ||
public class TestContextSampleTest { | ||
|
||
/** | ||
* @return As many parameters as the name of the included group | ||
*/ | ||
@DataProvider(name = "testContext") | ||
public Object[][] createContext(ITestContext ctx) { | ||
// ppp("CONTEXT:" + ctx); | ||
String[] groups = ctx.getIncludedGroups(); | ||
|
||
int n = groups.length > 0 ? new Integer(groups[0]): 0; | ||
Object[] result = new Object[n]; | ||
for (int i = 0; i < n; i++) { | ||
result[i] = "foo"; | ||
} | ||
|
||
return new Object[][] { | ||
new Object[] { result }, | ||
}; | ||
} | ||
|
||
private static void ppp(String s) { | ||
System.out.println("[TestContextSampleTest] " + s); | ||
} | ||
|
||
@Test(dataProvider = "testContext", groups="10") | ||
public void verifyTen(Object[] objects) { | ||
Assert.assertEquals(objects.length, 10); | ||
} | ||
|
||
@Test(dataProvider = "testContext", groups="5") | ||
public void verifyFive(Object[] objects) { | ||
Assert.assertEquals(objects.length, 5); | ||
} | ||
|
||
} |
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,46 @@ | ||
package test.dataprovider; | ||
|
||
import org.testng.Assert; | ||
import org.testng.TestListenerAdapter; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.Test; | ||
|
||
public class TestContextTest { | ||
|
||
@Test | ||
public void verifyTen() { | ||
verify("10", "verifyTen", 1, 0); | ||
} | ||
|
||
@Test | ||
public void verifyFive() { | ||
verify("5", "verifyFive", 1, 0); | ||
} | ||
|
||
@Test | ||
public void verifySix() { | ||
// Not including any group, so the two test methods should fail | ||
verify(null, null, 0, 2); | ||
} | ||
|
||
private void verify(String groupName, String passed, int passedCount, int failedCount) { | ||
TestNG tng = new TestNG(); | ||
tng.setVerbose(0); | ||
tng.setTestClasses(new Class[] { TestContextSampleTest.class }); | ||
if (groupName != null) { | ||
tng.setGroups(groupName); | ||
} | ||
TestListenerAdapter al = new TestListenerAdapter(); | ||
tng.addListener(al); | ||
tng.run(); | ||
|
||
if (passedCount > 0) { | ||
Assert.assertEquals(al.getPassedTests().size(), passedCount); | ||
Assert.assertEquals(al.getPassedTests().get(0).getMethod().getMethodName(), passed); | ||
} | ||
|
||
if (failedCount > 0) { | ||
Assert.assertEquals(al.getFailedTests().size(), failedCount); | ||
} | ||
} | ||
} |
Oops, something went wrong.