0

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

Michala
  • 3
  • 2
  • Have you tried formatting the docstring with the id, such as `f""" General docstring for the parametrized test. {id} """`? – Marco.S Nov 25 '22 at 00:50
  • Hi Marco, So I have tried these directly in the docstrng, but none of these work as desired: `""" General docstring for the parametrized test."""` - create this exact same doctsting for each test `f""" General docstring for the parametrized test. {id}` """ - result in "None" in the report `f""" General docstring for the parametrized test. {ids}` """ - throw an error: `test_vals[id1] - NameError: name 'ids' is not defined` `f""" General docstring for the parametrized test. {x[0]}` """ - result in "None" in the report – Michala Nov 26 '22 at 09:19

1 Answers1

0

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)

enter image description here

Marco.S
  • 383
  • 2
  • 10
  • Thanks, that looks good. Even thought, I have to rewrite it like `report.description = f'{str(item.function.__doc__)}: {str(item.callspec.id)}' ` – Michala Nov 27 '22 at 18:50