unit testing, principles
DESCRIPTION
TRANSCRIPT
![Page 1: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/1.jpg)
Unit Testing,Principles
@QuadraticBEJul. 2014
![Page 2: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/2.jpg)
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 [email protected]
www.quadratic.be
![Page 3: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/3.jpg)
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
![Page 4: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/4.jpg)
Unit testing is different from Integration testing
in which individual software modules are combined and tested as a group in
their runtime environment
![Page 5: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/5.jpg)
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
![Page 6: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/6.jpg)
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
![Page 7: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/7.jpg)
As a result,it affords several
benefits
![Page 8: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/8.jpg)
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
![Page 9: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/9.jpg)
In test-driven development (TDD), unit tests are even created before the
code itself is written
![Page 10: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/10.jpg)
Unit testing facilitates change
Unit testing allows the programmer to refactor code at a later date, and make
sure the module still works correctly
![Page 11: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/11.jpg)
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)
![Page 12: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/12.jpg)
Unit testing improves
Software Quality
![Page 13: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/13.jpg)
> Guidelines please...
![Page 14: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/14.jpg)
First a few terminology
Assertiona statement that something should be true
Test Suitea collection of tests
Pass / Successtest that runs ok
![Page 15: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/15.jpg)
Fail / Failuretest that does not pass
Fixtureall 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 Objectssimulated objects that mimic the behavior of real
(complex) objects in controlled ways
![Page 16: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/16.jpg)
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)
![Page 17: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/17.jpg)
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
![Page 18: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/18.jpg)
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
![Page 19: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/19.jpg)
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()
![Page 20: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/20.jpg)
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
![Page 21: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/21.jpg)
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
![Page 22: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/22.jpg)
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.
![Page 23: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/23.jpg)
Arrggh! Anything else?
![Page 24: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/24.jpg)
Adopt Unit Tests
Make them part of your development tasks
(as documentation ;) )
![Page 25: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/25.jpg)
Avoid that !“I’ve no time…”
“It’s not my job…”“Tests ? But I never do bugs…”
“The real coder doesn’t test...”
![Page 26: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/26.jpg)
Sourceshttp://en.wikipedia.org/wiki/Unit_testing
http://esj.com/articles/2012/09/24/better-unit-testing.aspx
![Page 27: Unit testing, principles](https://reader034.vdocuments.site/reader034/viewer/2022042613/5484ceaa5806b5d1588b4674/html5/thumbnails/27.jpg)
Thanks