Search code examples
pythonpytest

Access verbosity level in a pytest helper function


I have a helper function that retrieves a web page from the tested app and converts it to a dict using json.loads(). If the page is not a JSON, though, it raises an assertion error.

Now i want my test to print the page data if verbosity is set to 1 or more (ie. i run the tests with pytest -vv). I know i can access the config object in tests by using the config property of the request fixture:

def test_use_verbosity(request):
    verbose_level = request.config.getoption('verbose')

But how do i access the request in a helper function?


Solution

  • As pytest.config is deprecated, store the config data in a global variable (you can e.g. attach it to a module if it should be importable). You can write a pytest_configure hook for that.