Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying pytest Quick Start Guide
  • Table Of Contents Toc
  • Feedback & Rating feedback
pytest Quick Start Guide

pytest Quick Start Guide

By : Oliveira
3.9 (7)
close
close
pytest Quick Start Guide

pytest Quick Start Guide

3.9 (7)
By: Oliveira

Overview of this book

Python's standard unittest module is based on the xUnit family of frameworks, which has its origins in Smalltalk and Java, and tends to be verbose to use and not easily extensible.The pytest framework on the other hand is very simple to get started, but powerful enough to cover complex testing integration scenarios, being considered by many the true Pythonic approach to testing in Python. In this book, you will learn how to get started right away and get the most out of pytest in your daily work?ow, exploring powerful mechanisms and plugins to facilitate many common testing tasks. You will also see how to use pytest in existing unittest-based test suites and will learn some tricks to make the jump to a pytest-style test suite quickly and easily.
Table of Contents (9 chapters)
close
close

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...
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
pytest Quick Start Guide
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon