Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Testing

Maurits Kok edited this page May 31, 2021 · 9 revisions

General

Type of tests:

  • Unit test: testing of individual units of source code (scripts, functions, classes).
  • Integration test: testing of a combination of individual units as a group.
  • Regression test: re-running all tests to ensure that the previously developed and tested code still performes after a code change.

Writing tests

pytest supports execution of unittest test cases. The real advantage of pytest comes by writing pytest test cases. pytest test cases are a series of functions in a Python file starting with the name test_.

pytest has some other great features:

  • Support for the built-in assert statement instead of using special self.assert*() methods
  • Support for filtering for test cases
  • Ability to rerun from the last failing test
  • An ecosystem of hundreds of plugins to extend the functionality

Writing the sum test case example for pytest would look like this:

def test_sum():
    assert sum([1, 2, 3]) == 6, "Should be 6"

def test_sum_tuple():
    assert sum((1, 2, 3)) == 6, "Should be 6"

Some general best practices:

  • Name of the test file should be the name of the function you want to test, prefixed by test_. Pytest will automatically recognize all tests with this convention.
  • Tests should be independent and never depend on another test/
  • Test for edge cases

Executing tests

Code coverage

Code coverage measures the percentage of code under tests. A good goal would be to have at least 80% of the code base under unit testing.

Continuous Integration

Resources