Basics of Testing
|
Tests check whether the observed result, from running the code, is what was expected ahead of time.
Tests should ideally be written before the code they are testing is written, however some tests must be written after the code is written.
Assertions and exceptions are like alarm systems embedded in the software, guarding against exceptional behaviour.
Unit tests try to test the smallest pieces of code possible, usually functions and methods.
Integration tests make sure that code units work together properly.
Regression tests ensure that everything works the same today as it did yesterday.
|
Exceptions, Status Values and Tracebacks
|
|
Using Exceptions
|
Exceptions are effectively specialized runtime tests
Exceptions can be caught and handled with a try-except block
Many built-in Exception types are available
|
Assertions
|
Assertions are one line tests embedded in code.
The assert keyword is used to set an assertion.
Assertions halt execution if the argument is false.
Assertions do nothing if the argument is true.
The numpy.testing module provides tools numeric testing.
Assertions are the building blocks of tests.
|
Unit Tests
|
Functions are the atomistic unit of software.
Simpler units are easier to test than complex ones.
A single unit test is a function containing assertions.
Such a unit test is run just like any other function.
Running tests one at a time is pretty tedious, so we will use a framework instead.
|
Running Tests with pytest
|
The pytest command collects and runs tests starting with Test or test_ .
. means the test passed
F means the test failed or erred
x is a known failure
s is a purposefully skipped test
|
Fixtures
|
|
Edge and Corner Cases
|
Functions often fail at the edge of their range of validity
Edge case tests query the limits of a function’s behavior
Corner cases are where two edge cases meet
|
Integration and Regression Tests
|
|
Continuous Integration
|
Servers exist for automatically running your tests
Running the tests can be triggered by a GitHub pull request
CI allows cross-platform build testing
A .travis.yml file configures a build on the travis-ci servers
Many free CI servers are available
|
Test Driven Development
|
Test driven development is a common software development technique
By writing the tests first, the function requirements are very explicit
TDD is not for everyone
TDD requires vigilance for success
|