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.
If you are planning on attending the course, please follow the “Before the Course” instructions below before you arrive and contact us with any questions or issues.
This is an intermediate skill level course. We assume that participants:
Planning on attending the workshop or going through this material on your own? We recommend getting setup ahead of time to avoid any bandwidth issues at the venue. In this short guide, we’ll get your environment setup and running.
We’ll be using the conda package manager. So if you don’t already have Anaconda or Miniconda installed, that’s the first step.
bash Miniconda3-latest-MacOSX-x86_64.sh(or whatever the filename of the script you downloaded is).
conda --versionand make sure that you are running at least conda 4.5.X
If you’re having issues, checkout the video tutorial below on installing conda.
We’ve created and environment that has the dependencies we’ll use during this tutorial. It’s in the repository for the tutorial, so we’ll need to clone that repository, then setup the environment. You’ll be setting up your own repository during the tutorial, so no need to fork this one, we just need some of the content.
cdinto the repository.
conda env createin the terminal/Anaconda prompt.
To learn more about conda environments, checkout the following video.
There are a just a couple of other things you’ll want to do:
After the course is complete, we encourage you to implement testing and basic test driven practices in your software development cycle. You can always contact us with questions!