Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Skipping Tests

Resources mentioned in this lesson:

00:00 In the previous lesson, I dove deep on the assertion methods. In this lesson, I’ll show you the test skipping decorators. The unittest module has a series of decorators you can use to control what tests get run.

00:13 You can skip a test altogether if you want to temporarily disable it, or you can run a test conditionally based on a truthy value. Let’s go look at some examples.

00:24 This is test_skip.py. Once more, I’ve inherited from TestCase, this time with three test methods. The @unittest.skip() decorator says that this test should be skipped. You can provide a message so that when the test gets run, it explains why it got skipped.

00:42 The @unittest.skipIf() decorator is a conditional skip. It evaluates the first argument, and if it’s true, the test gets skipped. The example here is a pretty common use case, skipping version-specific tests.

00:54 The calendar constants got added in Python 3.12, so if you tested with something earlier, it would fail. In the condition here, I’m accessing the version_info tuple and checking if it’s less than 3.12.

01:06 If so, this test gets skipped. That’s beyond the scope of this course, but there are libraries out there that allow you to run your code across multiple versions of Python.

01:15 This is particularly helpful if you’re writing a library yourself. Other coders might not be using the same version of Python that you are. The most popular library for doing this kind of testing is tox, but my favorite is a competitor, nox. If I ran nox across 3.11 and 3.12, this test would get skipped in 3.11, but run in 3.12.

01:37 Like with the previous decorator, you can provide a message explaining why the test got skipped. The last decorator is the opposite of the previous one. Instead of skipping when a condition is met, it skips when the condition is not met.

01:50 When testing Windows-specific code, I want to skip the test if I’m not testing on Windows. Here, I’m checking the sys.platform value to see where things are being run.

02:02 Importing this stuff on a non-Windows box raises an exception. I’m running Python 3.14 on a Mac, so only the middle test should run. Let’s see this in action.

02:18 Note that I used the -v argument to make it verbose. Without it, I wouldn’t see the skipped messages. As expected, the first test got skipped, the second ran and passed, and since I’m not on Windows, the third got skipped as well.

02:33 Down at the bottom, our summary tells you that nothing failed, but two tests got skipped. This would be another case where you might turn on -v if you hadn’t already.

02:42 If the summary said something got skipped, and you wanted to see what, -v would be your friend.

02:48 So far, I’ve been just running code and asserting a single value. Oftentimes, you want to test a bunch of values. You can do that in a loop, but if you do, it can be problematic to know which instance failed.

03:00 The answer to that situation is in the next lesson.

Become a Member to join the conversation.