Why You Should Automate Your Python Tests with CI/CD


Crista Perlton

Crista Perlton


npm Package Approval Flows & Connectors 07th December, 2023

Making Sense of npm Package Licenses 30th November, 2023


Why You Should Automate Your Python Tests with CI/CD

Posted on .

Picture this: a fantastic Python app with a growing number of tests, employing either unittest or pytest. Running pytest test_*.py helps you perform unit tests and protects your code from breaking, ensuring your app remains stable even when you update dependencies using pip install --upgrade package_name. Best part? You can kill any bugs before they even annoy your users.

…there’s just one catch — you’re sitting there typing out pytest after pytest to get this all done. It can be easy to forget what needs to be tested, especially if you’re not the only one editing the code.

But what if you could run tests as soon as you or anyone else commits their code? This is why automating Python tests is essential. Not only does it allow your team to concentrate on coding without the burden of manually running tests, but it also helps you identify when a bug was introduced into the codebase.

Best of all though, you can rest easy knowing every code change is immediately tested, sparing you the risk of overlooking critical tests, especially in a collaborative coding environment.

In this article, we’ll dive into the effectiveness of test automation in Python development, focusing on: 

  • How to automate testing with CI/CD
  • What you’ll need to do it, including Git and Requirements.txt 
  • How to set up all of this in a CI/CD server 

How to Automate Python Unit Testing with CI/CD

Executing manually is easy; you can achieve this by simply running a command. However, automating this process requires the use of a CI/CD server. This offers capabilities beyond test automation:

  • Creating “artifacts” containing the same files tested, enabling seamless deployment
  • Deploying applications to test servers, and notifying users when testing is ready
  • Releasing applications into production environments. 

In this article, BuildMaster is used as an example, known for its excellent free version, user-friendly interface, and the ability to deploy to various targets. Though there are numerous other tools available, such as Jenkins which remains a popular choice. 

Before incorporating a CI/CD tool into your workflow though, we’ll need to quickly look at using source control (which you likely are already doing) and employing requirements.txt lock files (which you may already have in place). These practices will set a solid foundation for your CI/CD implementation!

Best Practices for Source Control (Git)

Git is a well-known and widely used version control system that most of us are already familiar with and use in development.  Love it or hate it, Git is an integral part of modern software development. When it comes to Python development, the commit feature in Git is particularly powerful. 

A commit represents the state of your code at a specific point in time. Instead of relying on testing and deploying from a production or release branch, you can do so from a specific commit. This approach ensures that the exact code you tested is the code that will be deployed. 

Commits in Git are identified by long SHA1 hashes, for example:


These hashes, while uniquely identifying commits, are not user-friendly. This is where BuildMaster comes into play, providing a user-friendly interface to track which commit is being tested or ready for deployment. 

BuildMaster also simplifies the process of comparing code between different commits. You can easily visualize and compare code changes within BuildMaster through the “Changes” tab. This functionality helps teams maintain code quality and track changes effectively. 

Best Practices for Dependencies (Requirements.txt)

When you add a PyPi or Conda package to your Python setup, it typically installs the newest version. Although convenient, it might lead to problems in your application. The code you’ve tested could break before it reaches production due to changes in these versions.

The real challenge is in maintaining consistency between package versions. it’s vital to ensure that all packages in your environment are using both compatible versions and compatible interdependencies.

Let’s take “urllib3” as an example. Even if you ran pip install urllib3==1.26.16 to install it from, pip will install the latest version. Conda behaves in the same way. This is where requirements.txt comes in, which:

  • Lists the exact version of dependencies used by your application. 
  • Ensures predictable builds. 
  • Guarantees that exact versions are always installed. 

Here’s a sample of what that might look like: 


By specifying exact versions of dependencies in requirements.txt, you can ensure that your application always uses the same ones. This makes it easier to maintain and reproduce your development environment. Generating this file is easy: 

  • For pip, just run python -m pip freeze > requirements.txt 
  • For conda, just run conda list --export > requirements.txt 

You’ll also need to update the file whenever you upgrade or add dependencies. To do this, commit changes to git like you would any other, then: 

  • For pip, run python -m pip install -r requirements.txt 
  • For conda, run conda install --yes --file requirements.txt 

Save time by Using a BuildMaster CI/CD Server for Python Testing 

Once you’re comfortable using Git, it’s time to implement CI/CD system. This will automate both the development and deployment processes –saving hours wasted on manually typing out all the tests yourself!

Let’s use BuildMaster to do this. and see just how quick and easy it is. We’ll start by creating a new application and linking to our Git repository with our app.

Next, you’ll want to make sure you set it as a Python application 

After your application is set up, you’ll need to create the “Build” script for your application.  This can be done by going to “Settings” > “Scripts” and then selecting “Add Script” 

And that’s all there is to it! Your application is now set up for automated testing and deployment, with build triggers in place to automatically create a build on each commit, and tests running automatically every time someone checks in code.

Boost Your Python Code with Test Automation 

Automated testing in Python development, including the use of pytest and unittest can give your development process a boost, ensuring a more efficient experience while maintaining the reliability of your applications. The main benefits of this are that it:

  • Simplifies your teams’ workflow
  • Identifies issues quickly and easily
  • Eliminates the need for manual testing
  • Tests code changes immediately as they happen

Implementing a CI/CD system is easy, give it a try using BuildMaster’s free version

To uncover more on Python test automation and much more, check out our Python in the Enterprise eBook. Download your copy for free today!

Crista Perlton

Crista Perlton