Unit testing tests individual units of source code to determine if they are fit for use. It is different from integration testing, which combines modules, and validation testing, which checks that a system meets specifications. The goal of unit testing is to isolate and test each part of a program to show that individual parts are correct. Unit testing provides benefits such as finding problems early, facilitating automated testing and change, and improving software quality. Guidelines for effective unit testing include knowing the objective of each test, making tests self-sufficient, ensuring tests are deterministic, following naming conventions, duplicating code for readability, testing results not implementation, and using isolation frameworks.
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
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
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.