TestNG parametric testing
One hip feature available in TestNG is the capability to run parametric tests. By placing parametric data in TestNG’s XML configuration files, a single test case can, in essence, be reused with different data sets, which can even provide different results.
You can delineate a parametric test via the parameters annotation, which maps parameter names to values via copasetic suite files. For example, here is a parametric test containing two parameters– classname, which is of type String and size, which is an int.
/**
* @testng.test
* @testng.parameters value="class_name, size"
*/
public void assertValues(String classname, int size) throws Exception{
Hierarchy hier = HierarchyBuilder.buildHierarchy(classname);
assert hier.getHierarchyClassNames().length == size: "didn't equal!";
}
Parameter values are defined in TestNG’s XML suite files– so for above test, I can map two values to classname and size– java.util.Vector and 2.
<suite name="ambr8">
<test name="ambr8-test">
<parameter name="class_name" value="java.util.Vector"/>
<parameter name="size" value="2"/>
<classes>
<class name="test.com.acme.da.HierarchyTest"/>
</classes>
</test>
</suite>
When I run this test, java.util.Vector and 2 will be passed to this test method by the TestNG framework. The beauty here is that non-programmers can start to get involved too, by providing thier own data values. This, by the way, is also a groovy way to avoid tests that only assert sunny-day scenarios or don’t effectively perform bounds checking, man.
Without this feature, to test 4 variations on parameter values, you’d need to write at least 4 tests, right? Now, you can write one generic test and pull the data values into a more human friendly format, which happens to be similar to the Fit model. Dig it?
| Related odds and ends | ||
|---|---|---|
Monday 14 Aug 2006 | Developer Testing, TestNG
Even more powerful: @DataProvider. Look it up on the TestNG documentation.
You bet! The next article in my series In pursuit of code quality will highlight the DataProvider feature. I guess I should also post an entry too. Thanks!
[...] Parametric testing show down One of my favorite features of TestNG is its hip parametric testing ability, which allows you to create generic test cases and then vary the test values– you can use parameters from XML files or even use the DataProvider feature for a more rich parameter type. In fact, for awhile, because it’s my bag baby, I’ve been espousing TestNG’s out of the box parametric testing features as a reason to use TestNG as a opposed to JUnit for higher level testing. JUnit 4 , however, now supports parametric tests– and you’ll find that its parametric testing is rather similar to TestNG’s DataProvider. [...]