Python Testing with Green
4.8 (46 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
355 students enrolled

Python Testing with Green

Concepts, best practices, explanations & instructions on how to effectively test your Python code.
4.8 (46 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
355 students enrolled
Created by Nathan Stocks
Last updated 7/2018
English [Auto]
Current price: $13.99 Original price: $19.99 Discount: 30% off
5 hours left at this price!
30-Day Money-Back Guarantee
This course includes
  • 3.5 hours on-demand video
  • 35 downloadable resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
Training 5 or more people?

Get your team access to 4,000+ top Udemy courses anytime, anywhere.

Try Udemy for Business
What you'll learn
  • Improve code quality.
  • Refactor code without fear.
  • Have a better coding experience.
  • Be able to better help others.
Course content
Expand all 46 lectures 03:25:33
+ Introduction
2 lectures 04:01

Course introduction

  • About the Instructor (Nathan Stocks)
  • Motivations behind creating this course
  • Benefits of this course
  • What You Will learn
  • Course Overview
  • Target Audience
Preview 01:56
  • Invitation to join our community on the PythonLearning Discord server.
  • Python documentation website
Community & Help
+ Beginner: Fundamental Python Testing Concepts
9 lectures 44:58

How to run your first test in Python.

Preview 05:25
  • Test package (directory) naming & layout
  • Test module (file) naming & layout
Preview 07:52
  • Docstring - what it is and how to make one.
  • How docstrings are used in tests
Preview 03:02

unittest.TestCase methods:

  • self.assertTrue(expression)
  • self.assertFalse(expression)
Preview 01:52

unittest.TestCase methods:


Regarding Errors & Failures

  • Where an error comes from
  • How an error is presented in a test report
  • Where a failure comes from
  • How a failure is presented in a test report
  • The difference between failures and errors

Skipped Tests

  • What a skipped test looks like
  • Some ways to skip a test

Please see the "Skipping Tests" lecture in the Intermediate section for more in-depth information on skipping tests.

unittest.skip() decorator and skipTest()

Expected Failure

  • What it means to have an expected failure
  • How to mark a test as an expected failure
unittest.expectedFailure decorator

Quick review of ALL the concepts in the beginner section.

Fundamentals Review

Did you learn the fundamentals?

Fundamentals Quiz
10 questions
+ Intermediate: All The Things You Need to Know
23 lectures 01:40:59
  • Target audience for this section.  If you are comfortable with the content in the Fundamentals Review, then you are ready for this section!
  • Overview of lecture format for this section.
Preview 01:17

How to specify specific and/or multiple test targets using Green.

Green: Target Specification

How to get code coverage reports using Green.

Green: Coverage

Quick introduction to what makes a meaningful test.

Preview 01:06

How using your test as a design tool makes your test meaningful.

Meaningful Tests: 1. Design Tool

How writing a readable test makes your test meaningful.

Meaningful Tests: 2. Readable Tests

How testing for one outcome makes your test meaningful.

Meaningful Tests: 3. Test One Outcome

How testing boundaries makes your test meaningful.

Meaningful Tests: 4. Test Boundaries

How testing the results of your code makes your test meaningful.

Meaningful Tests: 5. Test Results

How isolating the unit you are testing makes your test meaningful.

Meaningful Tests: 6. Isolate the Unit

How identifying the reason for your test makes your test meaningful.

Meaninfgul Tests: 7. Identify the Reason

Suggested ways to organize your unit test files, classes, and methods.

Unit Test Organization

How (and when) to use test fixtures, class fixtures, and module fixtures.


unittest.TestCase methods:

  • self.assertEqual(arg1, arg2, [message])
  • self.assertNotEqual(arg1, arg2, [message])
assertEqual, assertNotEqual

unittest.TestCase methods:

  • self.assertGreater(left, right, [message])
  • self.assertGreaterEqual(left, right, [message])
  • self.assertLess(left, right, [message])
  • self.assertLessEqual(left, right, [message])
assertGreater, assertGreaterEqual, assertLess, assertLessEqual

unittest.TestCase methods:

  • self.assertIs(arg1, arg2, [message])
  • self.assertIsNot(arg1, arg2, [message])
  • self.assertIsNone(arg1, [message])
  • self.assertIsNotNone(arg1, [message])
assertIs, assertIsNot, assertIsNone, assertIsNotNone

unittest.TestCase methods:

  • self.assertIn(item, collection, [message])
  • self.assertNotIn(item, collection, [message])
assertIn, assertNotIn

unittest.TestCase methods 

  • self.assertIsInstance(obj, class, [message])
  • self.assertNotIsInstance(obj, class, [message])
assertIsInstance, assertNotIsInstance

unittest.TestCase methods:

  • self.assertRaises(exception, callable, [arg1, arg2, ...], [kwarg1=x, kwarg2=y, ...])
  • self.assertRaisesRegexp(exception, pattern, callable, [arg1, arg2, ...], [kwarg1=x, kwarg2=y, ...])

assertRaises, assertRaisesRegexp

unittest.TestCase methods:

  • self.assertItemsEqual(collection1, collection2, [message])
assertItemsEqual (assertCountEqual in Python 3)

unittest.TestCase methods:

  • self.assertAlmostEqual(arg1, arg2, [places=7], [message], [delta])
  • self.assertNotAlmostEqual(arg1, arg2, [places=7], [message], [delta])
assertAlmostEqual, assertNotAlmostEqual

unittest.TestCase methods:

  • self.assertRegexpMatches(text, pattern, [message])
  • self.assertNotRegexpMatches(text, pattern, [message])
assertRegexpMatches, assertNotRegexpMatches

A more in-depth look at ways to skip tests than the lecture in the beginner section:

  • @unittest.skip(message)
  • @unittest.skipIf(condition, message)
  • @unittest.skipUnless(condition, message)
  • self.skipTest(message)
  • raise unittest.SkipTest(message)
Skipping Tests
+ Advanced: Power Tools
12 lectures 55:35

In this section:

  • Mock objects for unit tests
  • Green's powerful features
  • Integration Testing & Continuous Integration
  • Tips & Tricks
Advanced Section Overview

Overview of mock module:

  • Portable imports
  • MagicMock: Introduction to calls, attributes, methods, return values, side effects, and patching.
Mock Overview

The MagicMock object is vital to testing, unit testing especially.  In part 1 we begin our journey covering all the most commonly-used parts of the versatile MagicMock object.

Magic Mock - Part 1

PART TWO!!!  We bring it all in and learn all the rest that we need to know about the MagicMock...and a few other mock tidbits.

Magic Mock - Part 2

Learn how to patch your code to automatically replace real object with mocks and isolate the units you are testing.

Mock Patchers

Learn to use Green's powerful output options to customize your test reports.

Green - Output Options

How to initialize & finalize external resources (like databases) so that each worker process can have its own dedicated external resource and simultaneous tests don't interfere with each other.

Green - Initializer & Finalizer

Green uses multiple processes to run your tests in parallel by default.  Learn exactly how that works, and what you can do to customize it.  Run your tests FAST!

Green - Number of Processes

Green supports multiple config files to customize Green's behavior for your projects.

Green - Config Files

Green provides tab-completion of test targets for both bash and zsh!  Setup is quick and easy.

Green - bash & zsh tab-completion

Sometimes you want a test run to stop after the first failure.

Green - Fail Fast

Learn the correct way to create and clean up temporary files & directories, plus tips & information about how Green's worker processes use temporary directories and how you can identify worker processes.

Temporary Files & Directories
  • Familiarity with Python

Ever wonder why some projects seem able to make huge changes and still ship promptly, while others collapse under the weight of their own code? Testing is the difference! Effective tests enable you to add features quickly, refactor your code without fear, and ship new releases without big new bugs.

Make Your Project Successful by Writing Meaningful Tests

  • Layout your test packages and modules correctly
  • Organize your tests effectively
  • Learn the tools in the unittest module
  • Run your test with Green, a powerful new test runner
  • Write meaningful tests that enable quick refactoring
  • Learn the difference between unit and integration tests
  • Use advanced tips and tricks to get the most out of your tests.

Python Testing with Green

Tests are just a way of writing code that uses your project and makes sure that it really does what you think it does!  We'll learn the best way to write tests and some common problems and pitfalls that you should avoid.  

This course is designed as a practical reference for Python programmers who have real code that they would like to start testing, or test more effectively. I provide real runnable examples that you can type in yourself or download from the resources section.

The beginner section requires zero prior testing knowledge.  I teach the fundamental basics of testing Python code including how to run your tests the "traditional" way as well as with the high-performance Green test runner.  After completing this section, you will be able to write tests and run them.

The intermediate section teaches you how to write meaningful tests, and covers every aspect of the Python "unittest" module that you will use in daily life.  Upon completing this section, you will know more than most Python programmers about how to test your code.

In the advanced section I go over how to mock unit tests, integration testing, advanced usage of the Green test runner, and some tips and tricks for getting the most out of your testing.  Only an elite few gain this level of knowledge on their own.  After completing this section, you will be a master of Python testing.

Who this course is for:
  • Likes pretty colors
  • Believes that testing can improve their code
  • Prefers interfaces carefully crafted for usability AND aesthetics
  • Believes fast tests are better than slow tests
  • Understands that a good continuous integration solution improves quality of life.