O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

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

Testing code with mocks

Mocks are objects created as substitutes for real objects with the purpose of testing part of the behavior of the real objects. If you have seen the movie Body Snatchers, you might already have an understanding of the basic idea. Generally speaking, mocking is useful only when the real objects under test are expensive to create, such as a database connection, or when testing could have undesirable side effects; for instance, we might not want to write to the file system or a database.

In this recipe, we will test a nuclear reactor—not a real one, of course! This nuclear reactor class performs a factorial calculation that could, in theory, cause a chain reaction with a nuclear disaster as consequence. We will mock the factorial ...

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