Getting started with Python’s pytest
Published by O'Reilly Media, Inc.
Using pytest to test and debug your Python code
Python already comes with powerful built-in testing utilities, such as unittest. However, with the pytest library, you can create tests that are even simpler and easier to write and debug. Join Matt Harrison for a hands-on introduction to pytest and related tools. You'll learn the theory behind testing, implement a basic test harness in Python, and write tests using the library. You'll also get hands-on experience debugging tests and discover how pytest compares with unittest and doctest. You'll walk away with a sound knowledge of testing and how to use it in Python.
What you’ll learn and how you can apply it
By the end of this live online course, you’ll understand:
- The pytest module and related tools
- The difference between pytest and unittest
And you’ll be able to:
- Write tests using pytest
- Run code coverage on your code
This live event is for you because...
- You're a Python programmer with experience using the unittest module, and you want to learn about pytest.
- You're a Python programmer who wants to learn about testing.
Prerequisites
- Programming experience with Python
Required materials and setup:
- A machine with Python 3.6 (or above) installed and the ability to run IDLE. IDLE is a Python editor that ships with Python. (Run python3 -m idlelib.idle. Note that on Windows you need to open the Command Prompt and run: python -m idlelib.idle)
Recommended preparation:
Getting started with Python 3 (live online training course with Matt Harrison)
Recommended follow-up:
- Mastering Python’s pytest (live online training course with Matt Harrison)
- Test-Driven Web Development with Python (book)
- Python Testing with pytest (book)
Schedule
The time frames are only estimates and may vary according to how the class is progressing.
Introduction to the blockchain (10 minutes)
Environment and installation (10 minutes)
- Lecture and hands-on walkthrough: Creating a virtual environment to sandbox your code and install pytest into the sandbox
Blockchain implementation (15 minutes)
- Lecture and hands-on walkthrough: Creating a basic blockchain to test against.
Test-driven development with pytest (15 minutes)
- Lecture and hands-on exercises: Code coverage; testing your implementation
Break (10 minutes)
Test-driven development with pytest continued (20 minutes)
- Lecture and hands-on exercises: Running pytest from the command line; assertion rewriting; conventions for test discovery
Marking tests (15 minutes)
- Lecture and hands-on exercises: Using the mark decorator syntax to skip tests or mark particular ones to run
Catching exceptions (15 minutes)
- Lecture and hands-on exercises: Using the pytest.raises context manager to deal with exceptions in pytest
Break (10 minutes)
Debugging tests (10 minutes)
- Lecture and hands-on exercises: Using pdb, assertions, and command-line options to debug tests
Controlling test execution (20 minutes)
- Lecture and hands-on exercises: Exploring command-line options to determine which tests are run (using string expressions, node IDs, and markers)
Doctest (30 minutes)
- Lecture and hands-on exercise: Using doctest; integrating it with pytest
Your Instructor
Matt Harrison
Matt Harrison has a CS degree from Stanford University. He is a best-selling author on Python and data subjects. His books—Effective Pandas, Illustrated Guide to Learning Python 3, Intermediate Python, Learning the Pandas Library, and Effective PyCharm—have all been best-selling books on Amazon. Matt's newest publications are Machine Learning Pocket Reference and Pandas Cookbook, Second Edition. He has taught courses at large companies (Netflix, NASA, Verizon, Adobe, HP, Exxon, and more), universities (Stanford, University of Utah, BYU), as well as small companies. Matt has been using Python since 2000 and has taught thousands through live training, both online and in person.