How to provide custom message in pytest-html-reporter

87 views Asked by At

Is there a way to provide custom message for each test when I use [pytest-html-reporter]https://pypi.org/project/pytest-html-reporter/?

I tried both print and logging statement for my tests but they are not shown in the report.

I call the pytest test as follows:

pytest --cache-clear -v -s --capture=tee-sys --html-report=./Reports/report.html --title='Dashboard' --self-contained-html .\DASHBOARD\Tests\test_dashboard.py

The report only shows the Suite,Test case name,Status,Time,Error Message.

Any help much appreciated.

2

There are 2 answers

9
user1753626 On

In my case I have written a custom HTML that allowed me to add a note to the HTML. The pytest-html-reporter have support for adding custom HTML so you could add a function that will do this with the extra fixtur:

Example:

def test_extras(extra):
     extra.append(
     {
        "name": "Text",
        "format": "text",
        "content": "<lorem ipsum> " * 100,
    })

Note this part of code is untested and just a quick example:

# conftest.py
import pytest
report_extras = []

@pytest.fixture
def extra():
    yield report_extras
    
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):

    global report_extras
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])
    report.extra = report_extras 
0
Ram On

Thank you, this is what I have so far. My test function looks like this.

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
request.cls.driver = driver
driver.delete_all_cookies()
driver.get(TestData_common.Url)
yield
driver.quit()

@pytest.mark.usefixtures("setup")
class Test_Counterparty:
   def test_001_login(self, extra):
   extra.append(
        {
            "name": "Text",
            "format": "text",
            "content": "'Successfully clicked AGREE button"
        })
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    print('Successfully clicked AGREE button')
    self.loginPage = LoginPage(self.driver)
    self.loginPage.do_click_agree_button()
    self.driver.maximize_window()

conftest.py looks like this.

import pytest
report_extras = []

@pytest.fixture
def extra():
   yield report_extras

@pytest.mark.hookwrapper
   def pytest_runtest_makereport(item, call):
   global report_extras
   outcome = yield
   report = outcome.get_result()
   extra = getattr(report, 'extra', [])
   report.extra = report_extras

Please note I am using https://pypi.org/project/pytest-html-reporter/ and not the default pytest html report. When I use the default pytest html report, all my custom messages are looking fine in the report.