Pytest Example with Allure Report

Now Let us create a python file and implement allure report logs for the test methods.

  • Create a Python file test_PytestAllure2.py
  • Import allure and pytest modules
  • Create four methods. Out of those four methods decorate one method with a pytest fixture to skip.
  • Here we are making one method to Fail with assert False. This is to show in the report.
  • Now create a method which is used to save the logs in allure report as allureLogs(text).


test_PytestAllure2.py

import pytest
import allure


def test_methodA():
    allureLogs("Launching app")
    allureLogs("This a Method A Step-1")
    allureLogs("This a Method A Step-2")
    print("This is method A")


def test_methodB():
    print("This is method B")
    allureLogs("This a Method B")


@pytest.mark.skip
def test_methodC():
    print("This is method C")


def test_methodD():
    print("This is method D")
    assert False


def allureLogs(text):
    with allure.step(text):
        pass

Code Explanations

1. First we need to create python file test_pytestallure2.py

2. Now import both pytest and allure modules

import pytest
import allure

3. Now Create python methods in the test_pytestallure2.py file and decorate one method with a pytest fixture to skip.

4. Fail one method with assert False. This is to show in the report.

5. Create a method to save steps in allure report

Create a user defined method as allureLogs() which takes a string argument.

def allureLogs(text):
   with allure.step(text):
       Pass

Now, We can use this method in python test methods to save the logs in allure reports.

allureLogs("Contact Us Form page opened")


Create a method to save screenshots

Create a method and add below allure attach method to it. This helps to take screenshots and attach to allure reports.

allure.attach(self.driver.get_screenshot_as_png(), name=text, attachment_type=AttachmentType.PNG)

Here self.driver is an appium driver object.Please look at the appium framework or selenium framework to use this screenshot method.