Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
pytest Quick Start Guide

You're reading from   pytest Quick Start Guide Write better Python code with simple and maintainable tests

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789347562
Length 160 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bruno Oliveira Bruno Oliveira
Author Profile Icon Bruno Oliveira
Bruno Oliveira
Arrow right icon
View More author details
Toc

Using marks from fixtures

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, loc)
yield
locale.setlocale(locale.LC_ALL, None)

@pytest.mark.change_locale...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime