Search code examples
pythonpytest

pytest - collect information about test together with parameters


I am using pytest. I would like to query the framework to get a list of all the tests that would be scheduled to run, together with the parameters for each test.

By query, I mean an action to acquire that info.

When I execute pytest -s --co, I get some info but not all the info. The issue is that I want to be able to get the explicit parameters used in each test, not a label like pytest_parameters0.

<Module test_name.py>
<Function test_do[pytest_parameters0]
<Function test_do[pytest_parameters1]

I have also tried to implement hook functions like "pytest_collection_modify_items", however I a have not been able to retrieve the parameter information by this means either. Is there a way to do what I am seeking?

Any help will be appreciated!


Solution

  • Implement a pytest_collection hook is a viable solution. You can enumerate test items via session.items, and access parameters of each test item via keywords.

    def pytest_collection_finish(session: pytest.Session):
        for item in session.items:
            print(f"Test item: {item.nodeid}")
            for k, v in item.keywords.items():
                print(f"  {k}: {v}")