GergelyPolonkai
GergelyPolonkai

Reputation: 6431

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?

Upvotes: 2

Views: 662

Answers (2)

user18337734
user18337734

Reputation: 31

Have you tried using the built-in pytestconfig fixture? You can use its getoption() method to get the verbosity as follows:

def test_config_get_option(pytestconfig):
    verbosity = pytestconfig.getoption('verbose')
    print('\n', f'{verbosity=}', '\n')

Upvotes: 0

Nic Szerman
Nic Szerman

Reputation: 1864

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.

Upvotes: 1

Related Questions