Movatterモバイル変換


[0]ホーム

URL:


Packt
Search iconClose icon
Search icon CANCEL
Subscription
0
Cart icon
Your Cart(0 item)
Close icon
You have no products in your basket yet
Save more on your purchases!discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Profile icon
Account
Close icon

Change country

Modal Close icon
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timerSALE ENDS IN
0Days
:
00Hours
:
00Minutes
:
00Seconds
Home> Programming> Application Testing> pytest Quick Start Guide
pytest Quick Start Guide
pytest Quick Start Guide

pytest Quick Start Guide: Write better Python code with simple and maintainable tests

Arrow left icon
Profile Icon Oliveira
Arrow right icon
€17.98€19.99
Full star iconFull star iconFull star iconHalf star iconEmpty star icon3.9(7 Ratings)
eBookAug 2018160 pages1st Edition
eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Table of content iconView table of contentsPreview book icon Preview Book

pytest Quick Start Guide

Writing and Running Tests

In the previous chapter, we discussed why testing is so important and looked at a brief overview of theunittest module. We also took a cursory look at pytest's features, but barely got a taste of them.

In this chapter, we will start our journey with pytest.We will be pragmatic, so this means that we will not take an exhaustive look at all of the things it's possible to do with pytest, but instead provide you with a quick overview of the basics to make you productive quickly.We will take a look at how to write tests, how to organize them into files and directories, and how to use pytest's command line effectively.

Here's what is covered in this chapter:

  • Installing pytest
  • Writing and running tests
  • Organizing files and packages
  • Useful command-line options
  • Configuration:pytest.ini file
In the chapter, there are a lot of examples...

Installing pytest

Installing pytest is really simple, but first, let's take a moment to review good practices for Python development.

All of the examples are for Python 3. They should be easy to adapt to Python 2 if necessary.

pip and virtualenv

The recommended practice for installing dependencies is to create avirtualenv. Avirtualenv (https://packaging.python.org/guides/installing-using-pip-and-virtualenv/) acts like a complete separate Python installation from the one that comes with your operating system, making it safe to install the packages required by your application without risk of breaking your system Python or tools.

Now we will learn how to create a virtual environment and install pytest using pip. If...

Writing and running tests

Using pytest, all you need to do to start writing tests is to create a new file namedtest_*.py and write test functions that start withtest:

    # contents of test_player_mechanics.py
def test_player_hit():
player = create_player()
assert player.health == 100
undead = create_undead()
undead.hit(player)
assert player.health == 80

To execute this test, simply executepytest, passing the name of the file:

λ pytest test_player_mechanics.py

If you don't pass anything, pytest will look for all of the test files from the current directory recursively and execute them automatically.

You might encounter examples on the internet that usepy.test in the command line instead ofpytest. The reason for that is historical: pytest used to be part of thepy package, which provided several general purpose utilities, including...

Configuration: pytest.ini

Users can customize some pytest behavior using a configuration file calledpytest.ini. This file is usually placed at the root of the repository and contains a number of configuration values that are applied to all test runs for that project. It is meant to be kept under version control and committed with the rest of the code.

The format follows a simple ini-style formatwith all pytest-related options under a[pytest] section. For more details, go to:https://docs.python.org/3/library/configparser.html.

[pytest]

The location of this file also defines what pytest calls theroot directory (rootdir): if present, the directory that contains the configuration file is considered the root directory.

The root directory is used for the following:

  • To create the tests node IDs
  • As a stable location to store information about the project (by pytest plugins and features...

Summary

In this chapter, we covered how to usevirtualenv andpip to install pytest. After that, we jumped into how to write tests, and the different ways to run them so that we can execute just the tests we are interested in. We had an overview of how pytest can provide rich output information for failing tests for different built-in data types. We learned how to usepytest.raises andpytest.warns to check exceptions and warnings, andpytest.approx to avoid common pitfalls when comparing floating point numbers. Then, we briefly discussed how to organize test files and modules in your projects. We also took a look at some of the more useful command-line options so that we can get productive right away. Finally, we covered howpytest.ini files are used for persistent command-line options and other configuration.

In the next chapter, we will learn how to use marks to help us skip...

Download code iconDownload Code

Key benefits

  • Become proficient with pytest from day one by solving real-world testing problems
  • Use pytest to write tests more efficiently
  • Scale from simple to complex and functional testing

Description

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.

Who is this book for?

This book is for Python programmers that want to learn more about testing. This book is also for QA testers, and those who already benefit from programming with tests daily but want to improve their existing testing tools.

What you will learn

  • Write and run simple and complex tests
  • Organize tests in files and directories
  • Find out how to be more productive on the command line
  • Markers and how to skip, xfail and parametrize tests
  • Explore fxtures and techniques to use them effectively, such as tmpdir, pytestconfg, and monkeypatch
  • Convert unittest suites to pytest using little-known techniques
  • Use third-party plugins

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date :Aug 29, 2018
Length:160 pages
Edition :1st
Language :English
ISBN-13 :9781789343823
Category :
Languages :
Tools :

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Product Details

Publication date :Aug 29, 2018
Length:160 pages
Edition :1st
Language :English
ISBN-13 :9781789343823
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99billed monthly
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconSimple pricing, no contract
€189.99billed annually
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick iconExclusive print discounts
€264.99billed in 18 months
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick iconExclusive print discounts

Frequently bought together


Modern Python Standard Library Cookbook
Modern Python Standard Library Cookbook
Read more
Aug 2018366 pages
Full star icon4.4 (7)
eBook
eBook
€28.99€32.99
€41.99
pytest Quick Start Guide
pytest Quick Start Guide
Read more
Aug 2018160 pages
Full star icon3.9 (7)
eBook
eBook
€17.98€19.99
€24.99
Clean Code in Python
Clean Code in Python
Read more
Aug 2018332 pages
Full star icon3.7 (3)
eBook
eBook
€26.98€29.99
€36.99
Stars icon
Total103.97
Modern Python Standard Library Cookbook
€41.99
pytest Quick Start Guide
€24.99
Clean Code in Python
€36.99
Total103.97Stars icon

Table of Contents

8 Chapters
Introducing pytestChevron down iconChevron up icon
Introducing pytest
Why spend time writing tests?
A quick look at the unittest module
Why pytest?
Summary
Writing and Running TestsChevron down iconChevron up icon
Writing and Running Tests
Installing pytest
Writing and running tests
Organizing files and packages
Useful command-line options
Configuration: pytest.ini
Summary
Markers and ParametrizationChevron down iconChevron up icon
Markers and Parametrization
Mark basics
Built-in marks
Parametrization
Summary
FixturesChevron down iconChevron up icon
Fixtures
Introducing fixtures
Sharing fixtures with conftest.py files
Scopes
Autouse
Parametrizing fixtures
Using marks from fixtures
An overview of built-in fixtures
Tips/discussion
Summary
PluginsChevron down iconChevron up icon
Plugins
Finding and installing plugins
An overview of assorted plugins
Honorable mentions
Summary
Converting unittest suites to pytestChevron down iconChevron up icon
Converting unittest suites to pytest
Using pytest as a test runner
Converting asserts with unitest2pytest
Handling setup/teardown
Managing test hierarchies
Refactoring test utilities
Migration strategy
Summary
Wrapping UpChevron down iconChevron up icon
Wrapping Up
Overview of what we have learned
The pytest community
Next steps
Final summary
Other Books You May EnjoyChevron down iconChevron up icon
Other Books You May Enjoy
Leave a review - let other readers know what you think

Recommendations for you

Left arrow icon
Debunking C++ Myths
Debunking C++ Myths
Read more
Dec 2024226 pages
Full star icon5 (1)
eBook
eBook
€20.99€23.99
€29.99
Go Recipes for Developers
Go Recipes for Developers
Read more
Dec 2024350 pages
eBook
eBook
€20.99€23.99
€29.99
50 Algorithms Every Programmer Should Know
50 Algorithms Every Programmer Should Know
Read more
Sep 2023538 pages
Full star icon4.5 (68)
eBook
eBook
€26.98€29.99
€37.99
€37.99
Asynchronous Programming with C++
Asynchronous Programming with C++
Read more
Nov 2024424 pages
Full star icon5 (1)
eBook
eBook
€22.99€25.99
€31.99
Modern CMake for C++
Modern CMake for C++
Read more
May 2024504 pages
Full star icon4.7 (12)
eBook
eBook
€26.98€29.99
€37.99
Learn Python Programming
Learn Python Programming
Read more
Nov 2024616 pages
Full star icon5 (1)
eBook
eBook
€20.99€23.99
€29.99
Learn to Code with Rust
Learn to Code with Rust
Read more
Nov 202457hrs 40mins
Video
Video
€56.99
Modern Python Cookbook
Modern Python Cookbook
Read more
Jul 2024818 pages
Full star icon4.9 (21)
eBook
eBook
€28.99€32.99
€41.99
Right arrow icon

Customer reviews

Top Reviews
Rating distribution
Full star iconFull star iconFull star iconHalf star iconEmpty star icon3.9
(7 Ratings)
5 star57.1%
4 star14.3%
3 star0%
2 star14.3%
1 star14.3%
Filter icon Filter
Top Reviews

Filter reviews by




Denivy Braiam RuckNov 21, 2018
Full star iconFull star iconFull star iconFull star iconFull star icon5
A very rounded book with lots of tips and tricks both for python development and a healthy testing environment. It offers real examples and solutions for problems you may face in production.The book itself has a very near-the-user language and is very enjoyable to read! No issues found in the Kindle version.
Amazon Verified reviewAmazon
William JamirJun 17, 2020
Full star iconFull star iconFull star iconFull star iconFull star icon5
This is a well-written book, and it's pretty easy to get going. The highlight for me is the way the book not only explains how to use pytest but also why its features are useful, with practical examples and tips.I'm sure that even readers that have been using pytest for a few years will benefit and gain some insights.
Amazon Verified reviewAmazon
Winston Churchill-JoellApr 20, 2019
Full star iconFull star iconFull star iconFull star iconFull star icon5
As advertised, this book is a quick start guide to get you familiar with the core capabilities of the pytest framework and best practices for using it in production. Collaboration and version control are always kept in mind; the idea of why you're testing feels like it's always present, so the topics and tips have context with what you're likely going to encounter, rather than simply covered in isolation. Throughout the book, I felt like I'm getting an insider's perspective and not only an overview, but recommendations on the best way to proceed. In a relatively brief span of pages the author delivers a solid foundation for the reader in a humorous and conversational tone that makes the content an easy read. I highly recommend.
Amazon Verified reviewAmazon
Anderson FerreiraDec 19, 2022
Full star iconFull star iconFull star iconFull star iconFull star icon5
O livro é excelente, com detalhes e ótimas dicas de uso, não é muito aprofundado no pytest mas é pra quem quer e precisa usar rápido.
Amazon Verified reviewAmazon
D. BhaduriOct 30, 2021
Full star iconFull star iconFull star iconFull star iconEmpty star icon4
Self taught Python programmer, so, testing and test driven development are new to me. I'm careful about introducing too many packages that overlap with the standard library, but, I wanted to learn more about pytest as an alternative to unittest. Written by a contributor to the project, this book was an excellent and succinct introduction to (what i assume) is the 20% of pytest that is used 80% of the time.
Amazon Verified reviewAmazon
  • Arrow left icon Previous
  • 1
  • 2
  • Arrow right icon Next

People who bought this also bought

Left arrow icon
50 Algorithms Every Programmer Should Know
50 Algorithms Every Programmer Should Know
Read more
Sep 2023538 pages
Full star icon4.5 (68)
eBook
eBook
€26.98€29.99
€37.99
€37.99
Event-Driven Architecture in Golang
Event-Driven Architecture in Golang
Read more
Nov 2022384 pages
Full star icon4.9 (11)
eBook
eBook
€26.98€29.99
€37.99
The Python Workshop Second Edition
The Python Workshop Second Edition
Read more
Nov 2022600 pages
Full star icon4.6 (22)
eBook
eBook
€27.99€31.99
€38.99
Template Metaprogramming with C++
Template Metaprogramming with C++
Read more
Aug 2022480 pages
Full star icon4.6 (14)
eBook
eBook
€25.99€28.99
€35.99
Domain-Driven Design with Golang
Domain-Driven Design with Golang
Read more
Dec 2022204 pages
Full star icon4.4 (19)
eBook
eBook
€23.99€26.99
€33.99
Right arrow icon

About the author

Profile icon Oliveira
Oliveira
Github icon
Bruno Oliveira is a software developer with 18 years experience working at ESSS, developing desktop and web applications for simulation and numerical analysis for several industry sectors including oil and gas, aerospace, automotive, and chemical processes. Having taken part in the development of an internal testing framework to attend to the various needs of the applications he worked with, and having always been interested in testing and software quality, in 2012, Bruno took note of pytest and immediately fell in love with the project. He started contributing whenever he could and has been a pytest core contributor since 2014.
Read more
See other products by Oliveira
Getfree access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook?Chevron down iconChevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website?Chevron down iconChevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook?Chevron down iconChevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support?Chevron down iconChevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks?Chevron down iconChevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook?Chevron down iconChevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.


[8]ページ先頭

©2009-2025 Movatter.jp