SlideShare a Scribd company logo
1 of 18
Data Driven Tests with TestNG
by Matthias Rothe
matthias.rothe@unic.com
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
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
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
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
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
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
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
@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
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
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
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
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
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
Maven Coordinates
• Group Id: net.sf.testngdatabind
• Artifact Ids:
– testng-databinding-csv
– testng-databinding-properties
– testng-databinding-text
– testng-databinding-xml
• Version: 2.0
6/27/2013 15
Sourceforge
• Project Home:
http://sf.net/projects/testngdatabind
• Important Features
– File download
– Bug reporting
• Complete JavaDoc:
http://testngdatabind.sf.net/apidocs
6/27/2013 16
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
6/27/2013 18

More Related Content

What's hot

TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PBAbhishek Yadav
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introductionDenis Bazhin
 
Introduction of TestNG framework and its benefits over Junit framework
Introduction of TestNG framework and its benefits over Junit frameworkIntroduction of TestNG framework and its benefits over Junit framework
Introduction of TestNG framework and its benefits over Junit frameworkBugRaptors
 
TestNG vs JUnit: cease fire or the end of the war
TestNG vs JUnit: cease fire or the end of the warTestNG vs JUnit: cease fire or the end of the war
TestNG vs JUnit: cease fire or the end of the warOleksiy Rezchykov
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKEAbhishek Yadav
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentationSanjib Dhar
 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Gopi Raghavendra
 
IT Talk TestNG 6 vs JUnit 4
IT Talk TestNG 6 vs JUnit 4IT Talk TestNG 6 vs JUnit 4
IT Talk TestNG 6 vs JUnit 4Andrey Oleynik
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetdevlabsalliance
 

What's hot (20)

TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Test ng for testers
Test ng for testersTest ng for testers
Test ng for testers
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Introduction of TestNG framework and its benefits over Junit framework
Introduction of TestNG framework and its benefits over Junit frameworkIntroduction of TestNG framework and its benefits over Junit framework
Introduction of TestNG framework and its benefits over Junit framework
 
TestNg_Overview_Config
TestNg_Overview_ConfigTestNg_Overview_Config
TestNg_Overview_Config
 
TestNG vs JUnit: cease fire or the end of the war
TestNG vs JUnit: cease fire or the end of the warTestNG vs JUnit: cease fire or the end of the war
TestNG vs JUnit: cease fire or the end of the war
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKE
 
Test ng
Test ngTest ng
Test ng
 
Selenium TestNG
Selenium TestNGSelenium TestNG
Selenium TestNG
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
 
IT Talk TestNG 6 vs JUnit 4
IT Talk TestNG 6 vs JUnit 4IT Talk TestNG 6 vs JUnit 4
IT Talk TestNG 6 vs JUnit 4
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdet
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Junit
JunitJunit
Junit
 

Similar to TestNG Data Binding

A Short and Sweet Guide to Test Data Development
A Short and Sweet Guide to Test Data DevelopmentA Short and Sweet Guide to Test Data Development
A Short and Sweet Guide to Test Data DevelopmentEnov8
 
Data Driven Framework in Selenium
Data Driven Framework in SeleniumData Driven Framework in Selenium
Data Driven Framework in SeleniumKnoldus Inc.
 
DATA WAREHOUSE -- ETL testing Plan
DATA WAREHOUSE -- ETL testing PlanDATA WAREHOUSE -- ETL testing Plan
DATA WAREHOUSE -- ETL testing PlanMadhu Nepal
 
Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databaseselliando dias
 
Data Pipeline Installation Quality
Data Pipeline Installation QualityData Pipeline Installation Quality
Data Pipeline Installation QualityGreenM
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven TestingMaveryx
 
Soap UI - Lesson2
Soap UI - Lesson2Soap UI - Lesson2
Soap UI - Lesson2Qualitest
 
Introduction to SoapUI day 2
Introduction to SoapUI day 2Introduction to SoapUI day 2
Introduction to SoapUI day 2Qualitest
 
Testing and Deployment - Full Stack Deep Learning
Testing and Deployment - Full Stack Deep LearningTesting and Deployment - Full Stack Deep Learning
Testing and Deployment - Full Stack Deep LearningSergey Karayev
 
Qtp manual testing tutorials by QuontraSolutions
Qtp manual testing tutorials by QuontraSolutionsQtp manual testing tutorials by QuontraSolutions
Qtp manual testing tutorials by QuontraSolutionsQUONTRASOLUTIONS
 
Pa Project And Best Practice 2
Pa Project And Best Practice 2Pa Project And Best Practice 2
Pa Project And Best Practice 2alice yang
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering Madhar Khan Pathan
 
2013 OHSUG - Clinical Data Warehouse Implementation
2013 OHSUG - Clinical Data Warehouse Implementation2013 OHSUG - Clinical Data Warehouse Implementation
2013 OHSUG - Clinical Data Warehouse ImplementationPerficient
 
Weka : A machine learning algorithms for data mining
Weka : A machine learning algorithms for data miningWeka : A machine learning algorithms for data mining
Weka : A machine learning algorithms for data miningKeshab Kumar Gaurav
 
Test data documentation ss
Test data documentation ssTest data documentation ss
Test data documentation ssAshwiniPoloju
 
Data Acquisition and Preprocessing Objective.pptx
Data Acquisition and Preprocessing Objective.pptxData Acquisition and Preprocessing Objective.pptx
Data Acquisition and Preprocessing Objective.pptxharsha221mt017
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsMartin Skurla
 
Analytics machine learning in weka
Analytics machine learning in wekaAnalytics machine learning in weka
Analytics machine learning in wekaSudhakar Chavan
 

Similar to TestNG Data Binding (20)

A Short and Sweet Guide to Test Data Development
A Short and Sweet Guide to Test Data DevelopmentA Short and Sweet Guide to Test Data Development
A Short and Sweet Guide to Test Data Development
 
Data Driven Framework in Selenium
Data Driven Framework in SeleniumData Driven Framework in Selenium
Data Driven Framework in Selenium
 
DATA WAREHOUSE -- ETL testing Plan
DATA WAREHOUSE -- ETL testing PlanDATA WAREHOUSE -- ETL testing Plan
DATA WAREHOUSE -- ETL testing Plan
 
Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databases
 
Data Pipeline Installation Quality
Data Pipeline Installation QualityData Pipeline Installation Quality
Data Pipeline Installation Quality
 
Anand Iyer
Anand IyerAnand Iyer
Anand Iyer
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
 
BigData Testing by Shreya Pal
BigData Testing by Shreya PalBigData Testing by Shreya Pal
BigData Testing by Shreya Pal
 
Soap UI - Lesson2
Soap UI - Lesson2Soap UI - Lesson2
Soap UI - Lesson2
 
Introduction to SoapUI day 2
Introduction to SoapUI day 2Introduction to SoapUI day 2
Introduction to SoapUI day 2
 
Testing and Deployment - Full Stack Deep Learning
Testing and Deployment - Full Stack Deep LearningTesting and Deployment - Full Stack Deep Learning
Testing and Deployment - Full Stack Deep Learning
 
Qtp manual testing tutorials by QuontraSolutions
Qtp manual testing tutorials by QuontraSolutionsQtp manual testing tutorials by QuontraSolutions
Qtp manual testing tutorials by QuontraSolutions
 
Pa Project And Best Practice 2
Pa Project And Best Practice 2Pa Project And Best Practice 2
Pa Project And Best Practice 2
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering
 
2013 OHSUG - Clinical Data Warehouse Implementation
2013 OHSUG - Clinical Data Warehouse Implementation2013 OHSUG - Clinical Data Warehouse Implementation
2013 OHSUG - Clinical Data Warehouse Implementation
 
Weka : A machine learning algorithms for data mining
Weka : A machine learning algorithms for data miningWeka : A machine learning algorithms for data mining
Weka : A machine learning algorithms for data mining
 
Test data documentation ss
Test data documentation ssTest data documentation ss
Test data documentation ss
 
Data Acquisition and Preprocessing Objective.pptx
Data Acquisition and Preprocessing Objective.pptxData Acquisition and Preprocessing Objective.pptx
Data Acquisition and Preprocessing Objective.pptx
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() fails
 
Analytics machine learning in weka
Analytics machine learning in wekaAnalytics machine learning in weka
Analytics machine learning in weka
 

Recently uploaded

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 

Recently uploaded (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

TestNG Data Binding

  • 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
  • 15. Maven Coordinates • Group Id: net.sf.testngdatabind • Artifact Ids: – testng-databinding-csv – testng-databinding-properties – testng-databinding-text – testng-databinding-xml • Version: 2.0 6/27/2013 15
  • 16. Sourceforge • Project Home: http://sf.net/projects/testngdatabind • Important Features – File download – Bug reporting • Complete JavaDoc: http://testngdatabind.sf.net/apidocs 6/27/2013 16
  • 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

Editor's Notes

  1. Separation of logic and data: Also an organizational issue: people working on the data might not be Java developers