logo logo

Built-in Cloud Reporting Tool for Pytest and Unittest

main post image

Software testing is a way to check if the actual program, application or software product matches the expected requirements defined by the client and is free of defects. Such testing is performed on the product using either manual or automated testing tools to evaluate points of interest and features in the product for the purposes of checking the functionality, finding errors, gaps or missing requirements.

Software testing is an essential part of the software development lifecycle. It is done only so that the best product is released and reaches the client. In case any problems exist, testing helps detect them, and they are taken care of to not show up in the final release. Unit testing is a type of software testing which is all about testing the individual components of the product.

Using this method of testing, both testers and developers can identify problems at a very early stage in the development lifecycle and address them before they become bigger and more expensive problems to fix later down the line.

Proper unit testing practices include:

  1. Give us a high-quality product.
  2. Provide a simpler debugging process.
  3. Isolate sections of the product and validate their functionality.
  4. Assure to reduce the cost of bugs as they are resolved at their earliest.

Some of the best Python unit testing frameworks include PyTest and Unittest.

Pytest Python Testing Framework

PyTest is an open-source Python based testing framework that is considered to be generally all-purpose but is especially used for functional and API testing. It can be installed with:

pip install -U pytest

Test methods with this framework must have their names begin with ‘test_’.

The following example will perform two basic HTTP Get requests to the public MetaWeather API and perform assertions on the response code and the response body.

import pytest
import requests
import json

def get_response_status_code():
    response = requests.get("https://www.metaweather.com/api/location/search/?query=San Francisco")
    return response.status_code

def get_response_body():
    response = requests.get("https://www.metaweather.com/api/location/search/?query=San Francisco")
    return json.dumps(response.json())
def test_response_status_code():
    assert get_response_status_code() == 200

def test_response_location_type():
    assert ('City' in get_response_body())

And to run your tests, you will need to use the following command:

pytest

This will run all files named ‘test_*.py’ or ‘*_test.py’.

Running the test displays the following results in the console:

PyTest Results

As we can see, both tests were executed and passed.

Should one of the tests fail, for example if we changed the test_response_status_code() to assert a 404 response status code, we will receive direct indication of where and what was the cause of failure in the console:

PyTest Results - Failure

More details on PyTest can be found in their documentation.

Unittest Python Testing Framework

Unittest is another Python framework and is the very first Python-based automated unit testing framework that was designed to work with the standard Python library. During development, it was inspired by JUnit, and is often referred to as PyUnit.

Unlike PyTest, Unittest is a standard module, and doesn’t need to be installed.

In Unittest, you will need to create a class and define it as a test case to include your tests inside it. The test methods will have ‘test_*’ as their names.

The following example will assert if the summation of two numbers is equal to a specified value.

import unittest

def add(x, y):
  return x + y

class Test(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(add(4, 5), 9)

if __name__ == ‘__main__’:
    unittest.main()

When running the main method, the test will be executed, and we’ll see the results in the console.

Unittest execution results

As we can see, one test has been executed and passes.

In case we change the assertion to something incorrect, such as:

self.assertEqual(add(3, 5), 9)

We will receive an indication of the cause of failure in the console:

Unittest execution results

More details can be found in the Unittest documentation.

Integrating Pytest & Unittest with TestProject Python OpenSDK

The TestProject Python OpenSDK is a single, integrated interface for scripting with the most popular open source test automation frameworks (Selenium & Appium), and as such, can be integrated with PyTest and Unittest for the added benefit of built-in cloud reporting 📊

To get started, you need to complete the following prerequisites:

The TestProject Python OpenSDK is available on PyPI. All you need to do is add it as a Python module using:

pip3 install testproject-python-sdk

The minimum Python version required is 3.6. Then to complete the integration, you’ll need to complete the following two steps:

1) First, you’ll need to initialize a TestProject driver to enable automatic reporting. This can be done in a fixture in your test to guarantee the driver is initialized before any test in your suite is executed.

❗ Please note that in addition to drivers for web browsers and mobile platforms, you can also initialize the generic driver for non-UI tests as seen below with a PyTest fixture example:

@pytest.fixture(scope="module", autouse=True)
def driver():
    driver = webdriver.Generic(token="YOUR_DEVELOPER_TOKEN",
                              project_name="Cloud Reporting",
                              job_name="API Test")
    yield driver
    driver.quit()

To initialize the driver, you’ll need to use your developer token from the TestProject integrations SDK section, located here either in the driver constructor or as an environment variable on your system or run configuration named TP_DEV_TOKEN.

2) Second, to enable the reporting of the failed assertions, you’ll need to use the:

@report_assertion_errors

Annotation on the method that performs the assertion check.

For example:

@report_assertion_errors
def test_response_status_code():
    assert get_response_status_code() == 200

After running the tests, in case of any assertion errors during the test execution, they will be reported automatically for you, and appear in an organized matter in the TestProject reports section under the project specifically stated during the driver’s initialization:

TestProject Test Reports

These reports are easily accessible to your teammates, simplifying the collaboration process by using TestProject as a central reporting hub for your automation.

Every report can even be generated into either a full or summary PDF report, including your screenshots if any have been taken during the execution.

TestProject PDF Report

Your project will also include many detailed velocity reports and execution statistics relating to all test and job executions in that specific project.

TestProject HTML Test Reports

TestProject HTML Test Reports

TestProject HTML Test Reports

You can find more details on the TestProject Python OpenSDK on our official GitHub documentation. For reporting assertion errors, you can find more information at the reporting section of the documentation available here.

Happy Python Testing! 😎

David Goichman

About the author

David Goichman

Automation has been a great passion of mine from day one. Software Engineer at TestProject.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

Test, Deploy & Debug in < 1 hr

Leverage a cross platform open source automation framework for web & mobile testing using any language you prefer, and benefit from built-in dashboards & reports. Free & open source.
Get Started
FacebookLinkedInTwitterEmail