Unit Testing with Python
This course will help you to write good unit tests for your Python code, using tools such as unittest, doctest and py.test. Unit tests should improve code quality, and also support future development.
What you'll learn
This course follows on from the Pluralsight "Python Fundamentals" course, and has more detail about unit testing with Python.
In this Python tutorial we will cover libraries and frameworks such as:
- Unittest
- Doctest
- and Py.test
The aim is to help you to write unit tests that improve code quality, and also support future development. Good unit tests should provide immediate value in terms of reduced debugging and better design, and the investment writing them should pay back over the whole lifetime of your software.
Table of contents
- Course Overview 2m
- Module Overview 3m
- A First Test Case 4m
- Another Test Case, Explanation of 'Test Runner' 4m
- A Test Case Using assertRaises, Explanation of 'Test Suite' 3m
- Skip a Test Case, Marking it Work In Progress 2m
- Using setUp and tearDown - Explanation of 'Test Fixture' 4m
- Re-Introduce the Skipped Test Case, Get it to Pass. 1m
- Cest Case Design - Test Case Names as Specification. 5m
- Arrange - Act - Assert - Cleanup 4m
- unittest Documentation. 2m
- Module Summary. 1m
- Module Outline 1m
- Four Reasons for Unit Testing 1m
- Understanding What to Build 2m
- Documenting the Units 1m
- Designing the Units 3m
- Detecting Regression 3m
- Limitations of Unit Testing 1m
- Testing as Part of Your Personal Development Process 1m
- Test Last 2m
- Test First 2m
- Test Driven 1m
- Continuous Integration 4m
- Module Review 1m
- Module Outline 1m
- Motivation for Looking at pytest 2m
- Defining and Running a Simple Test Case 1m
- Interpreting Failure Information 2m
- Asserting the Contents of Collections 3m
- Built-In Helper Functions - 'raises' and 'skip' 2m
- Adding a Test Fixture by Using '@pytest.fixture' 5m
- Using Built-In Test Fixture Resources - 'tmpdir' 3m
- Using pytest to Run 'unittest' Tests 2m
- Module Review 1m
- Module Outline 1m
- What doctest is for, how it Relates to Unit Testing 2m
- Documenting a Simple Method With docstring Examples 4m
- Using Different Test Runners to Execute doctests 2m
- Handling Failing doctests 2m
- Handling Output That Changes - Dictionaries and Floats 4m
- Testing for Exceptions: Including Tracebacks in doctests 2m
- The ELLIPSIS Directive: a Wildcard for Matching Varying Output 5m
- Putting doctest Regression Tests in a Separate File 3m
- When to Put doctests in a File, Using Approval Testing 3m
- Doctest for Checking Tutorial Documentation 1m
- Module Review 1m
- Module Outline 1m
- What is a Test Double? 2m
- Example Using a Stub 4m
- Using unittest.mock to Create a Stub 2m
- Stub Example Summary 1m
- Example Using a Fake 7m
- Example Using a Mock 9m
- Example Using a Spy 3m
- Using unittest.mock to Create a Spy and a Mock 2m
- Example Using a Dummy Object 2m
- Choosing to Use a Particular Kind of Test Double 3m
- Using Monkeypatching to Insert a Test Double 5m
- Module Review 0m
- Module Outline 1m
- Using a Custom Assert to Reduce Duplication 4m
- Defining Parameterized Tests With unittest 5m
- Defining Parameterized Tests With pytest 3m
- Measuring Coverage With pytest-cov 4m
- Measuring Coverage of unittest Tests 1m
- Using Coverage Data to Add Tests to Legacy Code 6m
- Good and Bad Uses for Coverage Metrics 4m
- Module Review 1m
Course FAQ
Unit testing is when the smallest parts of a software are tested. It is testing that each unit of the software performs the way it is meant to. Python uses the unittest test framework.
Some benefits include:
- Improved code quality
- Support for future development
- Reduced debugging
- Better design
- Reduced costs
- Provides documentation
Ultimately you will learn how to write good unit tests for your Python code. Some of topics you will learn include:
- Python unittest - what it is and how to use it
- When and why you should write unit tests
- Using Pytest for unit testing in Python
- Testable documentation with Doctest
- Test doubles: Mocks, Fakes, and Stubs
- Test coverage and parameterized tests
- Much more
This course is for anyone who wants to learn how to test their Python code through unit tests to improve code quality and support future development. Software developers will especially find this course beneficial.
In order to understand and fully appreciate how to unit test your Python code, you should understand how to write Python code in the first place and have some experience with software development.