4. 4
Why writing tests?
●
Correctness of code… now
●
Correctness of code… tomorrow
●
Prevent past bugs to haunt you again
●
Give you confidence when refactoring
5. 5
How to write tests?
➔
Unit
➔
Integration
➔
Functional
➔
Generated
➔
Performance
10. 10
Test should be fast
●
Developers should not hesitate to run
them
●
Allow to run the subsets targeting the
changes
●
Avoid:
– heavy computation
– slow network connection
11. 11
Test should be simple
●
Test your code, not your dependencies
●
Simple… but not trivial
– Test the behavior not the implementation
●
Simple enough to allow to fix failure
without debugging
●
Easier to maintain
12. 12
Test should be automated
●
Continuous Integration (Buildbot,
Jenkins, Drone etc.)
– Run on each commit (or pull request)
– Test on multiple environments (tox, docker, …)
– Feedback to author about failure
(responsibility)
13. 13
Tools for testing in Python
●
Test Case:
– unittest, doctest, mock
●
Test runners:
– unittest, pytest, nose, tox
●
Behavior driven development ⚠
– behave, lettuce
●
Generated tests
– hypothesis
20. 20
Unit test in Python
●
Use explicit docstring
●
Use:
– assertEqual instead of assert(a == b)
– assertRaises (context manager)
– assertWarns, assertLogs
– assertListEqual, assertDictEqual, etc.
– addCleanup (register tearDown in setUp)