Search code examples
pythonhtmlpython-3.xpytestpytest-html

How to rename the test cases of html report generated by PyTest


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


Solution

  • 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)