SlideShare a Scribd company logo
eleks.comeleks.com
Unit & Integration Testing
A way to a better software
Introduction
The main work is
performed in
implementation &
testing phases.
Unit tests save time &
improve design.
What is a unit test?
A unit test is a piece of a code (usually a method) that
invokes another piece of code and checks the correctness
of some assumptions afterward.
If the assumptions turn out to be wrong, the unit test has
failed. A unit is a method or function.
What a unit test should not do?
Unit tests are not about finding bugs, they’re all about code
verification.
Unit tests are not supposed to be built on top of existing
design for the sake of code coverage.
In fact, they should drive your system towards flexible &
testable design, thus providing an interface to verify the
correctness of the input/output of the unit.
Unit tests scope
The scope of Unit testing is narrow, it covers the
Unit or small piece of code under test.
Therefore while writing a unit test shorter codes
are used that target just a single class.
Unit tests should have no dependencies on
code outside the unit tested.
What exactly should be tested?
- Tests should be written for a specific CUT
- Tests should cover only the API of this CUT and are not
supposed to test the implementation details of a unit
- Test only units with some logic (e.g. methods and
functions not properties)
- It makes sense to test only those units over which you
have control
- Test CUTs against interfaces
Single Responsibility
Test behaviour, not methods:
- One method, multiple behaviours -> Multiple Tests
- One behaviour, multiple methods -> One Test
- A method calls private & protected methods
- A method calls properties
- One behaviour, one result -> One Assert
- Multiple asserts are okay as long as they check the
same behaviour
What makes a good unit test?
A good unit test should have the following properties:
- It should be automated and repeatable.
- It should be easy to implement.
- It should be relevant tomorrow.
- Anyone should be able to run it at the push of a button.
- It should run quickly.
- It should be consistent in its results.
- It should have full control of the unit under test.
What makes a good unit test?
A good unit test should have the following properties:
- It should be fully isolated from other tests.
- When it fails, it should be easy to detect what was
expected and determine how to pinpoint the problem.
- Fails when the logic against which it was tested changes
- Doesn’t make you want to comment them
- Doesn’t call other tests
- Are implemented quickly
- Covers multiple behaviours
How to verify that test is correct?
After implementing a test try changing the
behaviour of the unit, so it down not meet the
requirement anymore, and run it again. It should
fail.
Failing tests are as important as passing ones.
What is an integration test?
Integration testing is testing a unit of work
without having full control over all of it and using
one or more of its real dependencies, such as
time, network, database, threads, random
number generators, and so on.
Integration tests scope
The scope of Integration testing is wide, it
covers the whole application under test and it
requires much more effort to put together.
Integration testing is dependent on other
outside systems like databases, hardware
allocated for them etc.
Benefits of unit & integration tests
- Helps to improve and
build flexible design
- Verifies that code is
working as intended
- Helps to stick to
requirements
- Is a good source for
code documentation
- Unit tests are a form of
sample code
- Detect failing logic
upfront
- Measures the
completion of a class
- Pinpoints the problem
location
- Simplifies integration
Test structure patterns
A A AArrange
(Given)
Assert
(Then)
Act
(When)
Naming convention
UnitOfWork_StateUnderTest_ExpectedBehavior()
1. Unit of work - name of method you are testing
2. State under test - short name for the state/input used for current test
3. Expected behavior - short name for expected result of the test which
you’re going to check
Test Driven Development
Many people feel that the best time to write unit
tests for software is after the software has been
written, but a growing number prefer writing unit
tests before the production code is written. This
approach is called test-first or test-driven
development (TDD).
Red - Green -
Refactor
Write a failing test to prove code or
functionality is missing from the end
product.
Make the test pass by writing
production code that meets the
expectations of your test.
Refactor your code.
Additional resources
The Art of Unit Testing: with Examples in C#, Second Edition
by Roy Osherove
Writing Great Unit Tests: Best and Worst Practices:
http://blog.stevensanderson.com/2009/08/24/writing-great-
unit-tests-best-and-worst-practises/
eleks.com
Inspired by Technology.
Driven by Value.

More Related Content

What's hot

Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven TestingMaveryx
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
An Introduction To Automated API Testing
An Introduction To Automated API TestingAn Introduction To Automated API Testing
An Introduction To Automated API Testing
Sauce Labs
 
Junit
JunitJunit
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Data driven Automation Framework with Selenium
Data driven Automation Framework with Selenium Data driven Automation Framework with Selenium
Data driven Automation Framework with Selenium
Edureka!
 
Test Automation
Test AutomationTest Automation
Test Automation
nikos batsios
 
Agile Testing
Agile Testing Agile Testing
Regression testing
Regression testingRegression testing
Regression testing
Anamta Sayyed
 
Automated Test Framework with Cucumber
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with Cucumber
Ramesh Krishnan Ganesan
 
Automated Testing with Agile
Automated Testing with AgileAutomated Testing with Agile
Automated Testing with Agile
Ken McCorkell
 
Non Functional Testing
Non Functional TestingNon Functional Testing
Non Functional TestingNishant Worah
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
Knoldus Inc.
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
Marius Crisan
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
Agile testing
Agile testingAgile testing
Agile testing
Yogita patil
 
Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber
Knoldus Inc.
 
Unit testing
Unit testingUnit testing
Unit testing
princezzlove
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
CodeOps Technologies LLP
 

What's hot (20)

Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
An Introduction To Automated API Testing
An Introduction To Automated API TestingAn Introduction To Automated API Testing
An Introduction To Automated API Testing
 
Junit
JunitJunit
Junit
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Data driven Automation Framework with Selenium
Data driven Automation Framework with Selenium Data driven Automation Framework with Selenium
Data driven Automation Framework with Selenium
 
Test Automation
Test AutomationTest Automation
Test Automation
 
Agile Testing
Agile Testing Agile Testing
Agile Testing
 
Regression testing
Regression testingRegression testing
Regression testing
 
Automated Test Framework with Cucumber
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with Cucumber
 
Automated Testing with Agile
Automated Testing with AgileAutomated Testing with Agile
Automated Testing with Agile
 
Non Functional Testing
Non Functional TestingNon Functional Testing
Non Functional Testing
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Agile testing
Agile testingAgile testing
Agile testing
 
Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber
 
Unit testing
Unit testingUnit testing
Unit testing
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 

Similar to Unit & integration testing

Software Testing Strategies ,Validation Testing and System Testing.
Software Testing Strategies ,Validation Testing and System Testing.Software Testing Strategies ,Validation Testing and System Testing.
Software Testing Strategies ,Validation Testing and System Testing.
Tanzeem Aslam
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
Marcelo Busico
 
Object Oriented Testing
Object Oriented TestingObject Oriented Testing
Object Oriented Testing
AMITJain879
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
Mohamed Taman
 
Software testing
Software testingSoftware testing
Software testing
Bhagyashree pathak
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
Priya Sharma
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
Unit testing
Unit testingUnit testing
Unit testing
Vinod Wilson
 
Test Process
Test ProcessTest Process
Test Process
tokarthik
 
Software testing
Software testingSoftware testing
Software testing
Nitin Dhanuka
 
Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
Deepak Singhvi
 
Software testing part
Software testing partSoftware testing part
Software testing part
Preeti Mishra
 
Software unit4
Software unit4Software unit4
Software unit4
Himanshu Awasthi
 
SOFTWARE TESTING UNIT-4
SOFTWARE TESTING UNIT-4  SOFTWARE TESTING UNIT-4
SOFTWARE TESTING UNIT-4
Mohammad Faizan
 
Testing in Software Engineering.docx
Testing in Software Engineering.docxTesting in Software Engineering.docx
Testing in Software Engineering.docx
8759000398
 

Similar to Unit & integration testing (20)

Testing
TestingTesting
Testing
 
Software Testing Strategies ,Validation Testing and System Testing.
Software Testing Strategies ,Validation Testing and System Testing.Software Testing Strategies ,Validation Testing and System Testing.
Software Testing Strategies ,Validation Testing and System Testing.
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
 
Object Oriented Testing
Object Oriented TestingObject Oriented Testing
Object Oriented Testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
Software testing
Software testingSoftware testing
Software testing
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Process
Test ProcessTest Process
Test Process
 
Software testing
Software testingSoftware testing
Software testing
 
Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Testing
Testing Testing
Testing
 
Software testing part
Software testing partSoftware testing part
Software testing part
 
Software unit4
Software unit4Software unit4
Software unit4
 
SOFTWARE TESTING UNIT-4
SOFTWARE TESTING UNIT-4  SOFTWARE TESTING UNIT-4
SOFTWARE TESTING UNIT-4
 
Testing in Software Engineering.docx
Testing in Software Engineering.docxTesting in Software Engineering.docx
Testing in Software Engineering.docx
 

Recently uploaded

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 

Recently uploaded (20)

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 

Unit & integration testing

  • 1. eleks.comeleks.com Unit & Integration Testing A way to a better software
  • 2. Introduction The main work is performed in implementation & testing phases. Unit tests save time & improve design.
  • 3. What is a unit test? A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. A unit is a method or function.
  • 4. What a unit test should not do? Unit tests are not about finding bugs, they’re all about code verification. Unit tests are not supposed to be built on top of existing design for the sake of code coverage. In fact, they should drive your system towards flexible & testable design, thus providing an interface to verify the correctness of the input/output of the unit.
  • 5. Unit tests scope The scope of Unit testing is narrow, it covers the Unit or small piece of code under test. Therefore while writing a unit test shorter codes are used that target just a single class. Unit tests should have no dependencies on code outside the unit tested.
  • 6. What exactly should be tested? - Tests should be written for a specific CUT - Tests should cover only the API of this CUT and are not supposed to test the implementation details of a unit - Test only units with some logic (e.g. methods and functions not properties) - It makes sense to test only those units over which you have control - Test CUTs against interfaces
  • 7. Single Responsibility Test behaviour, not methods: - One method, multiple behaviours -> Multiple Tests - One behaviour, multiple methods -> One Test - A method calls private & protected methods - A method calls properties - One behaviour, one result -> One Assert - Multiple asserts are okay as long as they check the same behaviour
  • 8. What makes a good unit test? A good unit test should have the following properties: - It should be automated and repeatable. - It should be easy to implement. - It should be relevant tomorrow. - Anyone should be able to run it at the push of a button. - It should run quickly. - It should be consistent in its results. - It should have full control of the unit under test.
  • 9. What makes a good unit test? A good unit test should have the following properties: - It should be fully isolated from other tests. - When it fails, it should be easy to detect what was expected and determine how to pinpoint the problem. - Fails when the logic against which it was tested changes - Doesn’t make you want to comment them - Doesn’t call other tests - Are implemented quickly - Covers multiple behaviours
  • 10. How to verify that test is correct? After implementing a test try changing the behaviour of the unit, so it down not meet the requirement anymore, and run it again. It should fail. Failing tests are as important as passing ones.
  • 11. What is an integration test? Integration testing is testing a unit of work without having full control over all of it and using one or more of its real dependencies, such as time, network, database, threads, random number generators, and so on.
  • 12. Integration tests scope The scope of Integration testing is wide, it covers the whole application under test and it requires much more effort to put together. Integration testing is dependent on other outside systems like databases, hardware allocated for them etc.
  • 13. Benefits of unit & integration tests - Helps to improve and build flexible design - Verifies that code is working as intended - Helps to stick to requirements - Is a good source for code documentation - Unit tests are a form of sample code - Detect failing logic upfront - Measures the completion of a class - Pinpoints the problem location - Simplifies integration
  • 14. Test structure patterns A A AArrange (Given) Assert (Then) Act (When)
  • 15. Naming convention UnitOfWork_StateUnderTest_ExpectedBehavior() 1. Unit of work - name of method you are testing 2. State under test - short name for the state/input used for current test 3. Expected behavior - short name for expected result of the test which you’re going to check
  • 16. Test Driven Development Many people feel that the best time to write unit tests for software is after the software has been written, but a growing number prefer writing unit tests before the production code is written. This approach is called test-first or test-driven development (TDD).
  • 17. Red - Green - Refactor Write a failing test to prove code or functionality is missing from the end product. Make the test pass by writing production code that meets the expectations of your test. Refactor your code.
  • 18. Additional resources The Art of Unit Testing: with Examples in C#, Second Edition by Roy Osherove Writing Great Unit Tests: Best and Worst Practices: http://blog.stevensanderson.com/2009/08/24/writing-great- unit-tests-best-and-worst-practises/