Skip to Content
Modern Python Standard Library Cookbook
book

Modern Python Standard Library Cookbook

by Alessandro Molina
August 2018
Intermediate to advanced
366 pages
10h 14m
English
Packt Publishing
Content preview from Modern Python Standard Library Cookbook

How it works...

You need to perform the following steps for this recipe:

  1. The unittest package provides a mock module that allows us to create Mock objects and to patch existing objects, so we can rely on it to replace the behavior of print:
from unittest import mock

with mock.patch('builtins.print') as mprint:
    print_division(4, 2)

mprint.assert_called_with(2)
  1. Once we know that the mocked print was actually called with 2, which is the value we expected, we can go even further and print all the arguments that it received:
mock_args, mock_kwargs = mprint.call_args
>>> print(mock_args)
(2, )

In this case, it's not very helpful as there was a single argument, but in cases where you only want to check some arguments instead of the whole call, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Matthew Wilkes

Publisher Resources

ISBN: 9781788830829Supplemental Content