Getting started with Python's pytest
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 training course 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.
- 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)
Getting started with Python 3 (live online training course with Matt Harrison)
About your instructor
Matt runs MetaSnake, a Python and Data Science training and consulting company. He has over 15 years of experience using Python across a breadth of domains: Data Science, BI, Storage, Testing and Automation, Open Source Stack Management, and Search.
The timeframes 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