1 of 11

Byte-sized RSE: Session 3

Testing your Python Code

Session Introduction

Jeremy Cohen, EPSRC RSE Fellow, Imperial College London

Steve Crouch, SSI Research Software Group Leader, University of Southampton

The UNIVERSE-HPC (EP/W035731/1) project is funded through the ExCALIBUR programme and is a collaboration between the universities of Edinburgh, Oxford, Southampton and Imperial College London.

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

2 of 11

Session Overview

  • Testing your code - introduction
  • Types of testing
  • Mocking
  • Related best practices
    • Code style/linting
    • Continuous Integration (CI)
  • Interactive tutorial - unit testing in practice

Byte-sized RSE

Testing your (Python) code

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

3 of 11

Testing your code - Why test your code?

  • Testing your code enables you to check that it behaves as expected
    • Important for supporting maintenance/long-term maintainability
    • Can pick up unexpected changes in behaviour of existing code resulting from future changes
    • Gives confidence to your code’s users of its correctness and provides invocation examples
  • As a codebase gets larger, debugging can become increasingly difficult
    • Tests can help you pick up problems before they become a runtime bug
    • A failing test will also help pinpoint where the problem lies

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

4 of 11

Testing your code

  • General principles of testing apply across languages
    • Most programming languages provide tools and frameworks to support testing
  • A set of tests for a codebase are known as a “test suite”
  • Code coverage
    • Coverage tools tell you how much of your code is being tested
    • Useful to gain insight into which parts of code aren’t being tested

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

5 of 11

Testing your code

A passing test suite (even with 100% code coverage)

DOES NOT MEAN that your code is bug free!!

(Writing tests can also be very time consuming - your initial focus should be on prioritising the most critical parts of the code - those that deliver greatest value or involve the most complexity.)

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

6 of 11

Testing your code

Time for a quick quiz!...

Go to menti.com and enter code 1762 1286

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

7 of 11

Types of testing - Levels

  • Unit testing
    • Test small, independent pieces of functionality, e.g. specific functions in the code
  • Integration testing
    • Next level up from unit testing - test blocks of code/modules together to verify they work as expected
  • System testing
    • Test full system functionality, e.g. top-level inputs/outputs/interactions

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

8 of 11

Types of testing - Approaches

  • Different approaches exist for the operation of tests
  • Where expected results are known, it is possible to check they are provided - either as fixed results or within some confidence interval
  • Where exact outputs not known (e.g. a Monte Carlo simulation), property-based testing may be used to run a test with a range of input values that are expected to have a shared output property

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

  • Things that worked before can stop working after a change to the code - regression testing can be used to identify these “software regressions:

9 of 11

Mocking

  • When running tests, you often want to test a very specific piece of functionality
  • Dependencies on external objects/functions complicate this (do you know they work as expected?)
  • Mocking allows you to replace dependencies with “mocked” objects or functions that behave as you tell them to
  • E.g. a function modifies data and writes it to a file
    • Mock the file writer object - mocked object stores “written” data
    • Check that the data written is as expected, no file created

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

10 of 11

Related best practices

  • Code style / linting
    • Different languages have different code style conventions (e.g. PEP8 in Python)
    • Following these makes code more readable by others and easier to maintain
    • Linting tools check that code conforms to specified styling properties - often run automatically
  • Continuous Integration (CI)
    • Automate the running of key processes in response to code changes
    • E.g. Automatically run tests and linter on every change/commit

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code

11 of 11

Testing in Python, in practice

  • That’s all for the background material
  • Now it’s your chance to have a go at writing some tests….

…over to Steve for the interactive part of the session

The content of these slides is licensed under CC-BY 4.0 © Jeremy Cohen, Imperial College London, Steve Crouch, University of Southampton.

Byte-sized RSE

Testing your (Python) code