August 2018
Beginner
160 pages
4h 8m
English
We can use the request fixture to access marks that are applied to test functions.
Suppose we have an autouse fixture that always initializes the current locale to English:
@pytest.fixture(autouse=True)def setup_locale(): locale.setlocale(locale.LC_ALL, "en_US") yield locale.setlocale(locale.LC_ALL, None)def test_currency_us(): assert locale.currency(10.5) == "$10.50"
But what if we want to use a different locale for just a few tests?
One way to do that is to use a custom mark, and access the mark object from within our fixture:
@pytest.fixture(autouse=True)def setup_locale(request): mark = request.node.get_closest_marker("change_locale") loc = mark.args[0] if mark is not None else "en_US" locale.setlocale(locale.LC_ALL, ...Read now
Unlock full access