I am running a parametrized test and I want to use kind of parametrized docstring in the html report. Normally, without the parametrization, it is the docstring of each test that I see as the description for the particular test. Now, with the parametrization, it is, of course, always the same text. Can I add a name, or some unique text from a file for each test?
For now, I have this easy setup:
def load_json_file(filename) -> list:
""" Load the data from the given json file, return a list. """
with open(filename, 'r') as openfile:
json_object = json.load(openfile)
return list(json_object.items())
# data source from a file
def data_from_browser():
return load_json_file('given_data.json')
# data source will be later from a browser
def desired_data():
return load_json_file('desired_data.json')
# for the purpose of the html report
def list_of_ids():
# could be, probably, loaded from a file
return ["set1", "set2", "set3"]
@pytest.mark.parametrize("given, expected", list(zip(desired_data(), data_from_browser())), ids=list_of_ids())
def test_timedistance_v0(given, expected):
""" General docstring for the parametrized test. """
assert given[0] == expected[0] # title
dict_diff = DeepDiff(given[1], expected[1]) # value
assert len(dict_diff) == 0, 'The given value is not equal to the desired value.'
The input data are like this (same for both now):
{"DISTINCT IP": "1,722",
"TYPES": {"Error": "1", "Name": "14570"},
"FROM": [["AV", "7,738", "20.93%"], ["AA", "4,191", "11.34%"], ["AB", "4,160", "11.25%"]]}
I am generating the report as
> pytest -v -s test_my_param.py --html="reports/report_param.html"
which looks like this (you can see the IDs and the docstring) example of the html report
Can I somehow add maybe the IDs into the docstring (Description) section?
Thanks for any hints Michala
If you're following the guide to modify the results table from the pytest-html
plugin documentation, you can use the item
object to add the ID into the cell with item.callspec.id
:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
report.description = str(item.function.__doc__ + item.callspec.id)