Create a python test environment from scratch

Create a python test environment from scratch

To automate tests, you can make use of a test environment. It is possible to make it by yourself. But why should you? There are already many very good environments. I like the pytest framework. That is an environment that is very flexible. The other benefit is that it runs with python. With pytest I can create a python test environment from scratch in seconds.

Pytest is a (unit) test framework that is ideal to create small tests. It is a framework that scales. This means that it has the capability to create more complex tests. It can run tests in parallel too.

There is a plugin system embedded with many plugins. You can find plugins at the following page: There is more. It is possible to make custom plugins. I will show you how to to that in a later article.

How to install pytest?

First open a command prompt and go to an empty directory. I assume that python3 is already installed on your system. Go to that empty directory. Execute the following command to create a virtual environment:

python3 -m venv ./myPy3Env

With this command, you created a virtual environment. There is a new directory myPy3Env. This is the virtual environment. You have to activate it first. Execute the the following command to activate the python environment.

source ./myPy3Env/bin/activate

You only have a basic python environment now. There are no libraries present. You have to install the libraries you need. Because we are creating a test environment with pytest, we will install pytest. The following command will do that.

pip install pytest

The python environment is now ready to create all your tests.

Create a first Test Case

We do have a real test environment at this moment. The next thing to do is to create a first test case. Let’s create a new empty directory where you can store the test cases and other useful files.

irst you should create a requirements file. That file will contain all our dependencies. At this moment our environment contains only pytest and its dependencies. You can re-create the same environment with the requirements file.

mkdir myproject
cd myproject
pip freeze > requirements.txt

You can re-create the same environment with the requirements file and pip.

pip install -r requirements.txt

Create now an empty python file with a name that starts with test. The intention is to write our test cases in that file. Test in the name of the file is important. Pytest searches for files with test in the name to execute the tests.

mkdir test

Put a first real test in the test file

def test_failing():
    assert True == False

The test case itself also contains the word test. Pytest will execute all functions with the name test in it. At this point we have one failing test.

Running the Test

Start to run the test now with the command pytest This gives an output as follows:

$ pytest
======================= test session starts ========================
collected 1 item
test/test\ F                                         [100%]
============================= FAILURES =============================
def test\_failing():
>       assert True == False
E       assert True == False
===================== 1 failed in 0.32 seconds =====================

Our first test failed. That is the intention. We added a True equals False in the test. As you know, that is not correct. Change now the False to True and run the test again.

collected 1 item  
test/test\ .                                         [100%]  
===================== 1 passed in 0.19 seconds =====================

The test is not failing anymore. It is very simple to create a python test environment from scratch with some tests with pytest. Now you can try to make more tests by yourself.