Skip to content

Python

Python is a very common language used in the System Health Lab. This documentation is to explain how the example works in this repository.

Installation of Packages

You will want to install packages to run automated-testing, but as well as to run all the tests in your local machine.

Create Environment

Create your own environment to isolate the packages. Refer to more information here.

Conda Create new Environment

In my local environment, I have done the following:

1
conda create --name test
where test is the name of my environment

Install Packages

Just in case you are unfamiliar of this, just type:

1
pip install -r requirements.txt

Running your tests locally

Just type

1
py.test

or

1
pytest

This will run all your test files that have prefix "test_"

What is Pytest?

Pytest is justs python test runner. There are lot of them, but I chose this for this example.

Most common usecase

  1. Copy paste the example to your own repo
  2. Replace /src files with your own source files and create your own tests and put them in /tests with prefix test_
  3. Go to the .github/workflows/test.yml and change the PROJECT_NAME to your project name (make sure to ask do step 1 of Setup Overview first)
  4. Commit

After that setup, everytime you do a git commit in the repository, the CI pipeline runs and tests your code and emails you (through your github email) when problems arises. You also get your reports at https://allure.systemhealthlab.com/.

Files and Folders Explanation

Source Files /src

These are files that you use as part of your software/reports that you want to keep all together. In my example, I have the following files:

1
2
__init__.py
addition.py

??? note "What is __init__.py? This file enables python to search for multi-directory files. This is helpful if you want to move your scripts in different places. Note that by doing this, the imports will all be relative to where you are running:

1
python script.py

Test Files /tests

This contains all your tests. It is very important that it has a prefix "test_" so that it could be detected as a test script. You could separate it to multiple files and multiple functions within each file as you please.

Import Path

Note that the syntax

1
from src import addition
will only apply with respect to the execution path. For example when I run my test file in the root directory:
1
python tests/test_addition.py

Testing Configuration pytest.ini

This is just a configuration file on how to run the test. Customise it as you please, but most cases you don't need to. Refer to more information here.

Upload Script send_and_generate.py

This is just a script that sends the test_results folder upon running tests

Git Ignore .gitignore

This is just a file that specifies all files/folders git will ignore. The test runner usually creates caches and results that are not usually uploaded in source controls, but uploaded somewhere such as the Allure Reporting Server.

CI Pipeline .github/workflows/test.yml

This is a github action file that executes codes on a specific event. Change this as you please, but I have set it up for the most common use case in the System Health Lab (pushing on master). If you look at the documentation for Setup Overview then you would be familiar with the high-level step of this.

Requirements requirements.txt

This is just a file that contains dependencies in the test runner setup.