Thiago Lopes Silva
3 min readMar 29, 2021

Running python tests on GitHub

Chapadão, Pipa/RN, Brazil

Hello folks. I’d like to share with you a problem that I’ve faced when I tried to integrate the framework pytest on a pipeline started by a job on GitHub. So, let’s go further and start putting all things together.

Initially, I’ve created a project named cryptography-cli to create a PEM RSA key using MVP+Clean architecture wIth some unit testing.

So, I divided to ran locally all my unit testing using the following commands and everything was ok.

python -m pytest --import-mode=append tests/
pytest ran locally

but….. after I ran the same code on a Github pipeline it shows the following error.

So, what is wrong with the code? Well, you need to set some steps on your pipeline on GitHub to run properly all unit tests as will be shown in the next sections.

Configuring the workflow pipeline

First of all, you need to create a new file with the extension .yaml on folder .workflows/ in the root folder and follow some steps as will be shown below.

Configuring the build strategy

In the file created you need to configure the job build strategy using python version 3.8.8 together with the latest Ubuntu version.

jobs:
build:
strategy:
matrix:
python-version: [3.8.8]
runs-on: ubuntu-latest

Configuring the pull and python version

In this step, we’ll configure the job to pull the latest code on current branch and set the python version using the plugin actions/setup-python@v1

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Switch to Current Branch
run: git checkout ${{ env.BRANCH }}

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

Configuring the test dependencies

In this step, we’ll configure the job to install all dependencies by creating the step named Install dependencies by using the following commands.

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -e .

But… wait! This step is very important because it will download all the project dependencies using a special file named requirements.txt using a virtual environment from the command pip install -e that help us to run our unit testing on Github Action.

You should use the command pip install -e . to configure properly your unit testing to run on the Github action.

Configuring the job to run the unit testing

Finally, in this step, it will run all unit testing of the project. So to do it you need to use use the following code.

- name: Run unit tests
run: python -m pytest --import-mode=append tests/

And after you configure all job’s step the pipeline will work properly. :)

If you need to get more information about this steps you can give it a try and go to my personal GitHub.

Thanks for your reading and I hope it helps you. See you in the next post.

Keep in touch and follow me on Twitter or Linkedin and don’t forget to clap if you enjoin it. :D

Thiago Lopes Silva
Thiago Lopes Silva

No responses yet