2

Below is the HTML report generated by PyTest with Test case name as "test_demo.py::test_demo_framework[0]" instead of this method's name I want to display some actual tests name like "Verify username and password" which ideally describes what kind of test was performed and gives a better understanding to the Report reader, obviously from method name/number, it's hard to identify what was tested as part this Tests.

I want to rename the yellow highlighted Testcases's name.

HTML Report generated by PyTest with meaningless Tests name

PraveenS
  • 115
  • 13
  • 1
    You may want to look at his [answer](https://stackoverflow.com/questions/59996968/pytest-best-way-to-add-test-description-long-test-name-in-the-report-with-out) – syfluqs Feb 29 '20 at 16:22

2 Answers2

3

Use this hookwrapper in conftest:

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result() 
    test_fn = item.obj
    docstring = getattr(test_fn, '__doc__')
    if docstring:
        report.nodeid = docstring

And for each test you run, you will have a specific name. Also you can make it dynamic(using a parameter) - eg:

def test_username(my_username):
 test_username.__doc__ = "Verify username and password for: " + str(my_username)
Ana G
  • 46
  • 2
1

I'm not worthy enough yet to comment, so here goes the answer.

xdist indeed does not like changed report.nodeid, but there's a workaround:

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result() 
    test_fn = item.obj
    docstring = getattr(test_fn, '__doc__')
    if docstring:
        report.docstring = docstring

And then you can use the following hook to overwrite the TC name in the report (again, in conftest.py):

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    for cell in cells:
        if cell.attr.class_ == "col-name":
            if hasattr(report, "docstring"):
                cell[0] = report.docstring
            break
karro
  • 11
  • 3