Loading video player…

Testing Your Code With Python's unittest (Overview)

The Python standard library ships with a testing framework named unittest, which you can use to write automated tests for your code. The unittest package has an object-oriented approach where test cases derive from a base class, which has several useful methods.

The framework supports many features that will help you write consistent unit tests for your code. These features include test cases, fixtures, test suites, and test discovery capabilities.

In this video course, you’ll learn how to:

  • Write unittest tests with the TestCase class
  • Explore the assert methods that TestCase provides
  • Use unittest from the command line
  • Group test cases using the TestSuite class
  • Create fixtures to handle setup and teardown logic

To get the most out of this video course, you should be familiar with some important Python concepts, such as object-oriented programming, inheritance, and assertions. Having a good understanding of code testing is a plus.

Download

Course Slides (.pdf)

899.2 KB
Download

Sample Code (.zip)

4.3 KB

00:00 Welcome to Testing Your Code With Python’s unittest. My name is Christopher, and I’ll be your guide. This course is an introduction to doing automated testing on your Python code.

00:11 You’ll learn about constructing tests with the standard library’s TestCase class, running tests, using the assert methods of TestCase to validate your code results, skipping tests, grouping values together in subtests, and setting your tests up with fixtures.

00:28 The code in this course was tested with Python 3.14. Everything you’ll see here is part of the standard library and has been around for quite some time, so as long as you’re using a supported version, you’ll be fine.

00:40 When you develop software, you want to make sure it works, and so you test it. If you’re just building short one-off scripts, that usually just means running it.

00:49 But for bigger pieces of software, especially those used by others, you need to be more comprehensive. Ideally, each time you make a change, you want to test your code to make sure you didn’t introduce a new bug.

01:01 Automating this process is considered a best practice in the industry. Just what does that mean, though? It means writing code that tests your code. In fact, a good rule of thumb is about one line of test code for each line of program code in your software.

01:15 A unit test is a small program that calls your code and checks that with certain inputs, expected output is returned.

01:24 Automating this means you can run it more frequently, the tests themselves are faster than manual, and less error-prone to boot. Most major languages now include unit testing libraries, and Python’s is called the unittest module.

01:37 There are also third-party libraries as well, but I’ll be concentrating on unittest in this course. To write a unit test in Python using the unittest module, you define a class that inherits from a TestCase.

01:50 Within that class, you declare methods that are your tests. Then when you run the test framework, it’ll find these classes for you and run your code.

01:59 In the next lesson, I’ll show you how to write those TestCase subclasses and how to run your test suite.

Become a Member to join the conversation.