SlideShare uma empresa Scribd logo
1 de 27
Baixar para ler offline
Unit Testing,
Principles
@QuadraticBEJul. 2014
So it’s done...
I’m Renato Primavera from Quadratic
I write software that helps customers to manage and
make use of their geographical data
@RenatoPrimavera
renato.primavera@quadratic.be
www.quadratic.be
Unit testing is a method by which
individual units of source code
are tested to determine
if they are fit for use
In Java, it means mainly testing methods
Unit testing is different from
Integration testing
in which individual software modules
are combined and tested as a group in
their runtime environment
Unit testing is also different from
Validation testing
which is the final process of checking
that a software system meets
specifications and that it fulfills its
intended purpose
So the goal of unit testing is to isolate each
part of the program and show that the
individual parts are correct
A unit test provides a strict, written contract
that the piece of code must satisfy
As a result,
it affords several
benefits
Unit testing finds problems early in the
development cycle
When the tests pass, that code is
considered complete
Tests can be played frequently in an automated
way, so any regression is highlighted ASAP
In test-driven development (TDD), unit
tests are even created before the
code itself is written
Unit testing facilitates change
Unit testing allows the programmer to
refactor code at a later date, and make
sure the module still works correctly
Unit testing provides a sort of
living documentation of the system
Developers looking to learn what
functionality is provided by a unit and how
to use it can look at the unit tests to gain
a basic understanding of the unit's
interface (API)
Unit testing improves
Software Quality
> Guidelines please...
First a few terminology
Assertion
a statement that something should be true
Test Suite
a collection of tests
Pass / Success
test that runs ok
Fail / Failure
test that does not pass
Fixture
all the things we need to have in place in order to run a
test and expect a particular outcome. Some people call
this the test context
Test Double / Fake Object / Mock Objects
simulated objects that mimic the behavior of real
(complex) objects in controlled ways
Know what you're testing
A test written without a clear objective in
mind is easy to spot. This type of test is long,
hard to understand, and usually tests more
than one thing
(When a developer has a problem naming a test,
that probably means the test lacks focus)
UT should be self-sufficient
A good unit test should be isolated. Avoid
static variables usage and dependencies on
external data (i.e. database, env settings)
A single test should not depend on running other tests
before it, nor should it be affected by the order of
execution of other tests
UT should be deterministic
The worst test is the one that passes some
of the time. A test should either pass all
the time or fail until fixed
Avoid writing tests with random input, that introduces
uncertainty and prevent to reproduce the failure
Respect Naming Conventions
This is a good test name
public void throwsIllegalArgumentExceptionIfIconIsNull()
This is no so good
public void iconNullThrowsException()
This is terrible
public void throwsException3()
Duplicate code if needed
Readability is very important in unit
testing, so it is acceptable to have
duplicate code
Having to change 4-5 similar tests is preferable to not
understanding one non-duplicated test when it fails
Test results, not implementation
Successful unit testing requires writing
tests that would only fail in case of an
actual error or requirement change
Testing the inner workings of how a feature was implemented
may lead to test failure even if the result is the same
Use Isolation Frameworks
Writing unit tests can be hard when the
class has complex dependencies.
Fake objects can help...
Instead of creating fake objects by hand, we can use mocking
framework to create them with only a few API calls.
Arrggh! Anything else?
Adopt Unit Tests
Make them part of your
development tasks
(as documentation ;) )
Avoid that !
“I’ve no time…”
“It’s not my job…”
“Tests ? But I never do bugs…”
“The real coder doesn’t test...”
Sources
http://en.wikipedia.org/wiki/Unit_testing
http://esj.com/articles/2012/09/24/better-unit-testing.aspx
Thanks

Mais conteúdo relacionado

Mais procurados

UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
suhasreddy1
 
Testing concepts ppt
Testing concepts pptTesting concepts ppt
Testing concepts ppt
Rathna Priya
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
medsherb
 
Software testing.ppt
Software testing.pptSoftware testing.ppt
Software testing.ppt
Komal Garg
 

Mais procurados (20)

Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Testing
TestingTesting
Testing
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Manual testing ppt
Manual testing pptManual testing ppt
Manual testing ppt
 
Manual Vs Automation Testing
Manual Vs Automation TestingManual Vs Automation Testing
Manual Vs Automation Testing
 
Unit testing
Unit testing Unit testing
Unit testing
 
Testing concepts ppt
Testing concepts pptTesting concepts ppt
Testing concepts ppt
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Intro to Manual Testing
Intro to Manual TestingIntro to Manual Testing
Intro to Manual Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
 
Agile testing
Agile testingAgile testing
Agile testing
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
CTFL Module 04
CTFL Module 04CTFL Module 04
CTFL Module 04
 
Software testing.ppt
Software testing.pptSoftware testing.ppt
Software testing.ppt
 
Manual testing
Manual testingManual testing
Manual testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Agile Testing and Test Automation
Agile Testing and Test AutomationAgile Testing and Test Automation
Agile Testing and Test Automation
 
software testing
 software testing software testing
software testing
 

Semelhante a Unit testing, principles

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 

Semelhante a Unit testing, principles (20)

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 & 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.
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applications
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testing
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Object Oriented Testing
Object Oriented TestingObject Oriented Testing
Object Oriented Testing
 
Tdd dev session
Tdd dev sessionTdd dev session
Tdd dev session
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking Skeleton
 

Último

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Último (20)

%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

Unit testing, principles

  • 2. So it’s done... I’m Renato Primavera from Quadratic I write software that helps customers to manage and make use of their geographical data @RenatoPrimavera renato.primavera@quadratic.be www.quadratic.be
  • 3. Unit testing is a method by which individual units of source code are tested to determine if they are fit for use In Java, it means mainly testing methods
  • 4. Unit testing is different from Integration testing in which individual software modules are combined and tested as a group in their runtime environment
  • 5. Unit testing is also different from Validation testing which is the final process of checking that a software system meets specifications and that it fulfills its intended purpose
  • 6. So the goal of unit testing is to isolate each part of the program and show that the individual parts are correct A unit test provides a strict, written contract that the piece of code must satisfy
  • 7. As a result, it affords several benefits
  • 8. Unit testing finds problems early in the development cycle When the tests pass, that code is considered complete Tests can be played frequently in an automated way, so any regression is highlighted ASAP
  • 9. In test-driven development (TDD), unit tests are even created before the code itself is written
  • 10. Unit testing facilitates change Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly
  • 11. Unit testing provides a sort of living documentation of the system Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain a basic understanding of the unit's interface (API)
  • 14. First a few terminology Assertion a statement that something should be true Test Suite a collection of tests Pass / Success test that runs ok
  • 15. Fail / Failure test that does not pass Fixture all the things we need to have in place in order to run a test and expect a particular outcome. Some people call this the test context Test Double / Fake Object / Mock Objects simulated objects that mimic the behavior of real (complex) objects in controlled ways
  • 16. Know what you're testing A test written without a clear objective in mind is easy to spot. This type of test is long, hard to understand, and usually tests more than one thing (When a developer has a problem naming a test, that probably means the test lacks focus)
  • 17. UT should be self-sufficient A good unit test should be isolated. Avoid static variables usage and dependencies on external data (i.e. database, env settings) A single test should not depend on running other tests before it, nor should it be affected by the order of execution of other tests
  • 18. UT should be deterministic The worst test is the one that passes some of the time. A test should either pass all the time or fail until fixed Avoid writing tests with random input, that introduces uncertainty and prevent to reproduce the failure
  • 19. Respect Naming Conventions This is a good test name public void throwsIllegalArgumentExceptionIfIconIsNull() This is no so good public void iconNullThrowsException() This is terrible public void throwsException3()
  • 20. Duplicate code if needed Readability is very important in unit testing, so it is acceptable to have duplicate code Having to change 4-5 similar tests is preferable to not understanding one non-duplicated test when it fails
  • 21. Test results, not implementation Successful unit testing requires writing tests that would only fail in case of an actual error or requirement change Testing the inner workings of how a feature was implemented may lead to test failure even if the result is the same
  • 22. Use Isolation Frameworks Writing unit tests can be hard when the class has complex dependencies. Fake objects can help... Instead of creating fake objects by hand, we can use mocking framework to create them with only a few API calls.
  • 24. Adopt Unit Tests Make them part of your development tasks (as documentation ;) )
  • 25. Avoid that ! “I’ve no time…” “It’s not my job…” “Tests ? But I never do bugs…” “The real coder doesn’t test...”