Hazelworm

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: http://plugincompat.herokuapp.com/ 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 test_hello.py

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_hello.py F                                          [100%]
============================= FAILURES =============================
___________________________ test_failing ___________________________
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_hello.py . [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.

About the author

I currently work as a Test Automation Consultant at b.ignited. Here I work for different clients in different industries to help them start and speed up their testing cycles

I’ve been testing software since 2000 when I became involved in testing telephone applications and hardware. Since then, I’ve been able to expand my experience by testing a variety of embedded, web, mobile and desktop applications. I have used various development methodologies from waterfall to agile.

I consider myself to be a lifelong learner.