[PYTHON] Output test function docstring to report with pytest-html

I will summarize how to output docstring to the report of pytest-html as detailed information of the test function.

1. Edit conftest.py

Add the following settings to conftest.py (created if it does not exist) directly under the tests directory. The title is inserted in the third column of the table header, and the information of the test function docstring is inserted in the third column of the table body.

conftest.py


import pytest
from py.xml import html


def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))

def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

2. Write a docstring in your test code

def test_one(self):
    """
This is test 1
    """
    assert True

def test_two(self):
    """
This is test 2
    """
    assert False

3. Test execution

$ pytest --html=report.html

4. Result

The docstring for each test function is displayed in the third column of the table.

pytest-html.png

reference

Recommended Posts

Output test function docstring to report with pytest-html
Test standard output with Pytest
Output to syslog with Loguru
Output to csv file with Python
Output cell to file with Colaboratory
Unit test log output with python
Try to output audio with M5STACK
Output color characters to pretty with python
How to do portmanteau test with python
ARC037 Baum test politely with Python recursive function
I tried to learn the sin function with chainer
Procedure to load MNIST with python and output to png
Add CSV export function to management screen with django-import-export
[Introduction to Python] How to iterate with the range function?
Output log to console with Flask + Nginx on Docker
[Golang] Test the function error termination "os.Exit (1)" with testing
Make a function to describe Japanese fonts with OpenCV
[Python-pptx] Output PowerPoint font information to csv with python
Try to automate pdf format report creation with Python
Python> function> Docstrings> Description to display with help () / .__ doc__
Try to specify the axis with PyTorch's Softmax function