Testing with Python

🏠 | 🔨 Testing |

Introduction

In this article you will learn how you can test software with Python.

I will start with preparing the working environment, then we will move on to simple tasks that can be assigned to a novice tester.

At the next stage we will look at the unittest library and learn some theory.

The article is aimed at novice testers. For a better understanding of some parts, basic knowledge of Python

Using Python for software testing.

docs.python-requests

programcreek.com/python

Preparing the working environment

To send POST, GET and other HTTP requests with python, you need to add the line to the program text import requests. Request first needs to be installed, for this you need pipenv, and to install pipenv you need pip

sudo apt install python-pip

If you need to update pip

pip install --upgrade pip

Installing pipenv

pip install pipenv

Before installing anything other than pip, make sure you are familiar with working in a virtual environment.

Installing requests

pipenv install requests

Now our python environment is ready for testing interfaces.

Several versions of Python may be installed on your computer. To find out which version your programs use, follow this link

Program examples

Create the file post.py and write the following code

import requests r = requests.post('http://url.html', data = {'site':'aredel.com'})

Run the script

python post.py

Real example 1

Task

There is a server that displays connected devices. Let's call it Device_Manager

The task is to populate the server with new devices, 1000 devices in total.

Each device must have a unique ID. Otherwise we will get an error.

Having looked at the API of this server you can learn that the request that adds a new device looks as follows:

PUT to http://devm.com:4880/manager/rest/control/devices/Unique_ID/apps/Client_Name/status/$timestamp=Some_value

It is also known from the API that this request carries JSON

{"status": {"clientStatusData": {"message":"Everything is OK", "status":"OK" }, "itemsStatuses": [{ "message":"URN.SU Server simulated", "status":"ENABLED", "name":"Connection", "Number":"0" }, { "message":"TopBicycle.RU Power: 250[mW], Session: 1", "status":"OK", "name":"AndreyOlegovih.ru", "Number":"1" }], "runningStatus": { "restarted":"true", "uptimeMSec":10000} }, "clientInfo": { "applicationInfo":{"applicationVersion":"19.61.04.12" }, "deviceInfo": {"itemDescription": "Good Device Number 1", "itemModelId":"Model ID 1", "statusUpdateIntervalMSec":"30000" } }, }

According to the task: itemModelId , itemDescription and uptime must be made unique.

Thus there should be four unique values.

Of course, you can send requests from SOAP UI one by one, manually changing Unique_ID, but it will take a very long time.

Solution. Part 1 - Comments

We write a script in Python using the knowledge gained at the beginning of this article and in the article Python. First we import the required libraries.

Then we divide the URL into three parts: the first and the third remain unchanged, and between them we will insert a variable.

After that we run a loop from 1 to 1000. We will use the values of the variable i to create unique IDs. For this purpose we will create three variables: for clarity they will all end in _var and then I will highlight them in green.

And finally, we create the variable json_string, into which we write the JSON we need with small changes. Instead of the static itemModelId , itemDescription, uptime we insert the variables defined in the previous step.

Watch the indentation carefully, since indentation denotes the body of the loop.

Solution. Part 2 - Code

import requests import json import urllib2 import uuid headers = {"Content-Type": "application/json"} base_url= 'http://device_manager.com:4880 / manager / rest /control / devices /' end_url= ' / apps / Client_Name / status /$timestamp=1529086249' for i in range(1,1000): c=str(i) item_model_var="AASuperDevice"+c item_description_var="Andrei"+c uptime_var= i * 50 Unique_ID_var= "Unique_ID"+c url= base_url+str(Unique_ID_var)+end_url json_string= {"status": {"clientStatusData": {"message":"Everything is OK", "status":"OK" }, "itemsStatuses": [{ "message":"URN.SU Server simulated", "status":"ENABLED", "name":"Connection", "Number":"0" }, { "message":"TopBicycle.RU Power: 250[mW], Session: 1", "status":"OK", "name":"AndreyOlegovih.ru", "Number":"1" }], "runningStatus": { "restarted":"true", "uptimeMSec":uptime_var} }, "clientInfo": { "applicationInfo":{"applicationVersion":"19.61.04.12" }, "deviceInfo": {"itemDescription": item_description_var, "itemModelId":item_model_var, "statusUpdateIntervalMSec":"30000" } }, } data_json=json.dumps(json_string) r=requests.put(url, data=data_json, headers=headers)

Debugging

If something went wrong, you can try the simplest way to track at which moment the error appears - add to the end of the code

print ("i =", i) print ("i string=", c) print (item_model_var) print (u) print (uptime_var) print(r.text)

But keep in mind that in serious software it is better to debug with a debugger.

Unittest

unittest is a testing library that comes with Python by default

Let's look at the simplest example of use.

Let's create two files, calc.py and test_calc.py

#calc.py def add(x, y): """Add Function""" return x + y + 3 def subtract(x, y): """Subtract Function""" return x - y def multiply(x, y): """Multiply Function""" return x * y def divide(x, y): """Divide Function""" if y == 0: raise ValueError("Can not divide by zero!") return x / y

As you can see, an error is deliberately made in the add function - instead of adding the two variables, the number 3 is also added to them

#test_calc.py import unittest import calc #https://docs.python.org/3/library/unittest.html#unittest.TestCase.debug class TestCalc(unittest.TestCase): def test_add(self): result = calc.add(10, 5) self.assertEqual(result, 15) #python3 -m unittest test_calc.py

To run the test, go to the directory with the files and run the following command in the console

python3 -m unittest test_calc.py

F ====================================================================== FAIL: test_add (test_calc.TestCalc) ---------------------------------------------------------------------- Traceback (most recent call last): File "/mnt/c/Users/username/python/unittest/test_calc.py", line 10, in test_add self.assertEqual(result, 15) AssertionError: 18 != 15 ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)

Let's fix the error

#calc.py def add(x, y): """Add Function""" return x + y ...

python3 -m unittest test_calc.py

. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK

To run this test in PyCharm or run it from the console without additionally specifying -m unittest, let's add two lines to the end of the test_calc.py file:

#test_calc.py ... if __name__ == '__main__': unittest.main()

Now the test can be run with the command

python3 test_calc.py

Let's write tests for all the functions from calc.py

Again let's deliberately make an error, for example in the third test

import unittest import calc class TestCalc(unittest.TestCase): def test_add(self): self.assertEqual(calc.add(10, 5), 15) self.assertEqual(calc.add(-1, 1), 0) self.assertEqual(calc.add(-1, -1), -2) def test_subtract(self): self.assertEqual(calc.subtract(10, 5), 5) self.assertEqual(calc.subtract(-1, 1), -2) self.assertEqual(calc.subtract(-1, -1), 0) def test_multiply(self): self.assertEqual(calc.multiply(10, 5), 70) self.assertEqual(calc.multiply(-1, 1), -1) self.assertEqual(calc.multiply(-1, -1), 1) def test_divide(self): self.assertEqual(calc.divide(10, 5), 2) self.assertEqual(calc.divide(-1, 1), -1) self.assertEqual(calc.divide(-1, -1), 1) if __name__ == '__main__': unittest.main()

Let's run the test

python3 test_calc.py

..F. ====================================================================== FAIL: test_multiply (__main__.TestCalc) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/username/.PyCharmCE2018.3/config/scratches/test_calc.py", line 17, in test_multiply self.assertEqual(calc.multiply(10, 5), 70) AssertionError: 50 != 70 ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED (failures=1) Process finished with exit code 1

Note the first line: dots mean the test ran successfully. F - test failure.

..F. means that the first, second and fourth tests passed successfully, and the third has an error

The assertion that returned FALSE is also visible: self.assertEqual(calc.multiply(10, 5), 70)

And the error AssertionError: 50 != 70

With such a detailed result we easily fix the error

self.assertEqual(calc.multiply(10, 5), 70)

Change it to

self.assertEqual(calc.multiply(10, 5), 50)

Let's run the test

python3 test_calc.py

.... ---------------------------------------------------------------------- Ran 4 tests in 0.001s OK Process finished with exit code 0

unittest theory

For those wondering whether unittest is outdated, my humble opinion is that it is not.

In the Python world PyTest is considered more fashionable; one of the reasons is its simpler syntax.

Nevertheless unittest is convenient because it is built into the standard library and because other programming languages also have similar frameworks.

Test Fixture

A single test is called a test case (Test Case).

Often you need to run several test cases that require the same thing to start.

For example, get the same object or launch Selenium Webdriver or something else.

To avoid writing the same thing in every test case, you can use the setUp and tearDown methods, which are created once for each class and will run before each test case.

Such a combination setUp + tearDown is called Test Fixture

Test Fixture = setUp + tearDown

Components of any test

The order of test execution is usually as follows:

Preparation for the test

The action itself, for example running a particular function

Checking the result against the expectation.

Arrange → Act → Assert

Long tests with many alternating actions and checks should be avoided where possible.

It is better to write several small tests than one long one. Then when one of the well-structured tests fails, it will be easier to understand what exactly does not work.

Pytest

Adding Pytest to Pycharm

Settings (Ctrl + Alt + S) → Tools → Python Integrated Tools → Default test runner:

Select pytest. If it has not been added yet, a warning and a Fix button will appear.

You need to click the Fix button

Errors

NameError: name 'pytest' is not defined

You need to import pytest.

import pytest

↓ Python articles ↓
Development in Python
Interactive mode
Virtual environment
str: strings
\: line break
Lists
Loops
Methods
Functions
*args **kwargs
enum
Determine a Python variable type
Working with REST API in Python
Files: write, read, append, context manager…
Download a file over the network
SQLite3: working with a database
datetime: Date and Time in Python
Selenium + Python
Difficulties when working with Python
DJANGO
Flask
Script for a ZPL printer
socket :Python Sockets
subprocess: run bash commands from Python
multiprocessing: several processes at once
psutil: system resources
sys.argv: command line arguments
PyCharm: IDE
pydantic: data validation
paramiko: SSH from Python
enumerate
json.dumps
↓ Articles about Testing ↓
API testing lessons
API testing
Selenium + Python
SOAP UI
JMeter
Bash for QA Engineer
Clumsy 0.2
Python script for ZPL
Python Sockets
Integration Testing

Article author: Andrei Olegovich