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.
Course info
Rating
(236)
Level
Intermediate
Updated
Nov 1, 2013
Duration
2h 58m
Table of contents
Unit Testing with Python - Basic Example Using unittest
Why and When Should You 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
Description
Course info
Rating
(236)
Level
Intermediate
Updated
Nov 1, 2013
Duration
2h 58m
Description

This course follows on from the Pluralsight "Python Fundamentals" course, and has more detail about unit testing with Python. 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.

About the author
About the author

Emily is an experienced software developer specializing in Test Driven Development, agile methods, and automated testing in general. She is a well known speaker, coding dojo facilitator, blogger, and author of "The Coding Dojo Handbook".

More from the author
Coding Dojo: Test Driven Development
Intermediate
2h 4m
25 Jun 2013
Section Introduction Transcripts
Section Introduction Transcripts

Unit Testing with Python - Basic Example Using unittest
Hi, this is Emily Bache for Pluralsight. This course is about unit testing with Python. In general the term unit test refers to any kind of test for a small piece of a larger system. In this course we're specifically talking about automated unit tests. In this course I'm also assuming you're already familiar with the python programming language. So if you're new to python I do recommend you watch the Pluralsight course python fundamentals before you watch this one, that course includes an overview of the unit test framework. And in this course, we'll be looking at that library in more detail, and some other useful tools for unit testing. This course is divided into modules. In this first module, we're really looking through the basic example using unittest and explaining the vocabulary around unit testing. In the next module we'll step back a little and look at the context of unit testing. Why are we doing it and when should we do it? The next few modules are all very concrete. We'll look at pytest, an alternative to unittest, and doctest, which allows you to write testable documentation, test doubles, mock stocks and fakes and how you can use test coverage or code coverage to assess how good your test suite is. The last module is really full of advice I've gained from experience about how to write maintainable unit tests. So in case you're wondering. That picture there is of the Python testing goat. Now goats are very agile and this particular goat encourages you to write tests for your code. From his vantage point in that tree he looks over your shoulder and chooses opportune moments to say things like, Have you got a test for that?

Why and When Should You Write Unit Tests?
In the first module, we went through an example of writing unit tests for a simple program. In this module, we'll be looking at some more background to the topic. We'll be discussing why we're doing unit testing. What benefits can it bring? We'll also talk about how you can incorporate writing unit tests into your personal development process. And I'll be characterizing three different approaches, as test first, test last and test driven. In the last part of this module, we'll talk about how unit testing can help when you're collaborating with others in a wider development process.

Using Pytest for Unit Testing in Python
This is Emily Bache for Pluralsight. This is the third module of this course. In the first module, we went through the basics of unit testing using Unit Test. In this module, we'll be looking at the same worked example using a different tool, py. test. First, we'll look at how you define test cases with py. test. Then we'll talk a little more about interpreting the test failure information you get. Py. test has extended information compared with the Unit Test. Then we'll look at test fixtures with py. test. These are handled a little differently than with Unit Test.

Testable Documentation with Doctest
This is Emily Bache for Pluralsight. This is the fourth module of this course, and we'll be looking more closely at doctest. So, first I'll talk about situations where you might use doctest, and the main used case for it is to make documentation comments more truthful. So we'll look at an example of that. With doctest it's very important to handle output that changes from run to run or changes on different platforms. So we'll look at that too, and then we'll look at another way to use doctest which is all about regression testing.

Test Doubles: Mocks, Fakes and Stubs
This is Emily Bache for Pluralsight. This is the fifth module of this course and we'll be looking more closely at test doubles. Test doubles are sometimes generally referred to as mock objects and they can be used to make your unit tests more readable, reliable, and quick to execute. So we're going to talk about, well what is a test double, different kinds of test doubles, why you should use them, and then a little bit about how you can use Monkeypatching to insert test doubles.

Test Coverage and Parameterized Tests
Hi. This is Emily Bache for Pluralsight. This is the 6th module of this course, and we'll be looking more closely at test coverage and parameterized tests. In the previous module of this course, we talked about how you can use unit tests as part of a development process, and how a team might set up a continuous integration environment to measure the code coverage of their tests. In this module, we will be looking more at how to use code coverage metrics. But before we do that, I'd like to show you a useful technique for writing compact test cases with high coverage called parameterized tests. So we'll look at doing that with both unittest and pytest. Then I'll show you about coverage and how you can measure that. And then I will talk more about how to use those coverage metrics when you're adding test cases to your code.