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

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

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