O'Reilly logo

Modern Python Cookbook by Steven F. Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unit test

In order to unit test this, we'll need to mock two external objects. We need a mock for the run() function in the subprocess module. We don't want to actually run the other process, but we want to be sure that the run() function is called appropriately from the make_files() function.

We also need to mock the Path class and the resulting Path object. These provide the filenames, and will have the unlink() method called. We need to create mocks for this so that we can be sure only the appropriate files will be unlinked by the real application.

Testing with mock objects means that we never run the risk of accidentally deleting useful files when testing. This is a significant benefit of using Python for this kind of automation.

Here's ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required