All software operates on data. High quality software needs to be thoroughly tested. This mandates to take the data into account and creates a need for easy and powerful solutions for data driven unit, functional and integration tests. Data driven tests are tests in which the test logic stays the same and the test data changes with each invocation of the test logic.
With TestNG implementing and executing data driven tests is easy as test data can be injected into test methods as method parameters. Unfortunately TestNG doesn't provide any standard for binding test data from external data sources, like XML, or CSV / Excel files, to the test method parameters. It rather leaves it up to each developer to implement her own solution for data binding. To minimize the effort this calls for the provision of a standardized solution.
This presentation shows how easy it is to implement data driven tests with TestNG. It then goes on to introduce the TestNG Data Binding framework. This framework fills the gap and provides a standardized solution for test data binding. It features an open, plug-in based architecture allowing for countless data formats to be bound to test method parameters. Currently it can bind data from CSV, Java .properties, text and XML files.
1. Data Driven Tests with TestNG
by Matthias Rothe
matthias.rothe@unic.com
2. Introduction Example
public boolean isBetween(int i, int lower, int upper)
• Checking whether this method behaves as it should
requires executing it with different values for i, with
fixed values for lower and upper
• While the data changes for each execution the actual
test logic is always exactly the same
6/27/2013 2
3. Data Driven Testing
• Tests depend more on different input and
output data then on different logic
• The code of the test method is fixed
• The test method is invoked several times with
different data
• The data changes with every test method
invocation
• The test logic depends on complex data
structures
6/27/2013 3
4. Support from Test Frameworks
• JUnit: Doesn‘t support data driven tests easily,
essentially it requires the definition of a new
test method per data set or using the
ParameterizedTest pattern
• TestNG: Supports data driven tests naturally
by allowing data to be injected into the test
method via method parameters
6/27/2013 4
5. TestNG Data Providers
• @Test(dataProviderClass, dataProvider)
• @DataProvider(name)
• Class given as dataProviderClass must have a static method
annotated with @DataProvider
• Value of @Test.dataProvider must equal value of
@DataProvider.name
• The static method must either return Iterator<Object[]> or
Object[][]
• Each Object[] must contain values matching the test
method parameters
• There are as many test method invocations as there are
Object[] in the Iterator or Object[][]
6/27/2013 5
6. TestNG Data Providers - Limitations
• All test data is stored in Java classes
– This doesn‘t separate logic from data clearly
– Java classes are inadequate data stores
– Setting up complex data structures can be quite
cumbersome
• There‘s no standard way for loading test data
from other data sources
6/27/2013 6
7. Data Binding
• Allows to store data where it naturally
belongs: in data files or databases
• Loads data from external data sources and
binds it to Java types
• Each kind of data can be stored in the most
appropriate format
6/27/2013 7
8. TestNG Data Binding
• Is built on top of the @DataProvider feature
• Introduces new annotations
– @DataBinding
– @TestInput, @TestOutput
• Currently supported data sources:
– CSV, Properties, Text, XML
6/27/2013 8
9. @DataBinding
• Activates the generic data provider used for
binding data from the data sources
• Can be defined on class and on method level
• Needs to be activated by registering the
TestAnnotationTransformer
– With testng.xml for whole test suites
– In IDE when executing isolated test cases
6/27/2013 9
10. Meta Data Declaration
• Each test class has an associated
.data.properties file containing the data
source configurations for the test methods
within that test class
• Configurations for different test methods are
differentiated by property key prefixes
6/27/2013 10
11. XML Data Source
• See format specification of XML files in the
JavaDoc
• Use for complex hierarchical data structures
• Supported Java types
– Primitives including Strings
– Enums
– Java Beans, up to any nesting level
– Lists of primitives and Java Beans
6/27/2013 11
12. CSV Data Source
• See format specification of CSV files in the
JavaDoc
• Use for tabular test data
• Supported Java types
– Primitives including Strings
– Enums
– Java Beans, but no nested Java Beans
6/27/2013 12
13. Best Practices
• Implement the toString() method for Java
Beans, so the data contained in them will be
available in test reports
• If you reuse the same data in several test
methods, load it first and store it in memory
6/27/2013 13
14. Extending TestNG Data Binding
• The framework is plugin based
• The framework core is data source agnostic
• Each data source is a plugin
• New data sources can be easily added by
implementing a new plugin
• New base packages to search for plugins can
be made known with a testng-
databinding.base-packages file
6/27/2013 14
17. Additional Resources
• Join the community at
http://facebook.com/TestNGDataBinding
• Watch a screencast at
http://www.screenr.com/ut97
• Read articles concerning TestNG at
http://testngtricks.blogspot.com
6/27/2013 17