top of page
Testing your Python Code with PyTest
Every developer has heard the saying that “untested software is broken software.” In this tutorial we will show you the best practices for software testing in Python using the pytest framework. Learners will write tests for several existing functions in a provided library, including testing strings, integers, floats, lists, and arrays. We will also use the pytest-mpl library to test matplotlib plotting functions with image comparison. Topics such as test fixtures, parameterization, and test coverage will also be demonstrated. Finally, students will implement new functionality in the example library and employ test-driven-development practices. This course is targeted at anyone writing code for their own scientific use or for a scientific library and wants to learn effective ways to test that code. Learners are expected to have a grasp on the Python language features, be able to write functions, be able to create and run python scripts, and be comfortable with the command line. Learners are also encouraged to have a GitHub account and be comfortable with git, though it is not necessary for the core testing materials that will be taught. By the end of this tutorial, learners will be able to write tests for numerical and string returning functions, write image tests for plotting functions, and check the coverage of their existing codebase. This knowledge will equip them to be able to implement a test suite on their new or legacy code bases.
This is an intermediate skill level course. We assume that participants: - Can create, edit, and run simple Python scripts unassisted. - Understand basic Python language principles. - Have a GitHub account.
bottom of page