Connecting an Allure reporter to an arbitrary python test framework
- Tutorial
Automation needs human-readable test reports - what was tested, what test steps, what result. To do this, there is an Allure reporting system .
Allure is supported by most well-known test frameworks, and for unknowns, it requires writing a so-called adapter.
How to connect Allure to any arbitrary framework or not at all?
Using Allure consists of two independent steps:
Generation of json containing all test actions, the so-called allure-results .
Generating a report on the collected results (requires java8 and installing the allure utility: https://bintray.com/qameta/generic/allure2 ). This item is the same for all types of frameworks due to the fact that allure-results is a fairly universal description of tests:
allure generate /some_path_to/allure-results
Writing a full-fledged adapter for allure and integrating it into an arbitrary framework is somewhat time-consuming, so we will use the minimum possible set of actions to just illustrate the idea. The code below is a python adaptation of the ideas presented in the report on the allure-lifecycle in Java, the original .
Install only the kernel of the allure reporter:
pip install allure-python-commons
Let's write a simple test generating allure-results:
# -*- coding: utf-8 -*-
import allure_commons
from allure_commons.utils import now, uuid4
from allure_commons.reporter import AllureReporter
from allure_commons.logger import AllureFileLogger
from allure_commons.model2 import Status
from allure_commons.model2 import TestResult
from allure_commons.model2 import TestStepResult
def check_some_thing(some):
print ("Some is %s" % some)
return some
if __name__ == '__main__':
print ("Start main")
# Init allure
allurelogdir = "reportsx"
logger = AllureReporter()
file_logger = AllureFileLogger(allurelogdir)
allure_commons.plugin_manager.register(file_logger)
# Start testcase
case_uuid = uuid4()
testcase = TestResult(uuid=case_uuid, fullName='Hello, Habr')
logger.schedule_test(case_uuid, testcase)
# TestStep
allure_step = TestStepResult(name='Привет, habrateststepname (да здравствует русский язык)', start=now())
current_step_uuid = uuid4()
logger.start_step(None, current_step_uuid, allure_step)
check_some_thing('something') # origial procedure for testing
logger.stop_step(current_step_uuid, stop=now(), status=Status.PASSED)
testcase.status = Status.PASSED
logger.close_test(case_uuid)
After starting this code will generate the results in the "reports" folder
As you can see, the code for working with the test step (start_step and stop_step) fits perfectly with the decorator pattern and should be implemented by it.
Arrange these blocks of code according to your test framework and collect fake tests with the results of manual testing, perform allure generate and you will get a ready-made solution for standardizing department reports.