O'Reilly logo

Test-Driven Development with Django by Kevin Harvey

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

Mocking – when and why

To get the search result that we're asking for in the new functional test without adding it to the database, we're going to extend the search view to do a search_artists() at MB. That's exactly the kind of external integration that we should mock out in our test.

A mock is simply a replacement for an object or function in the context of a single test. There are several reasons you might want to mock out external (or even internal) code, particularly in unit tests:

  • Mocks improve test isolation

    If our unit test is dependent on an external system working, doesn't that mean our test is not testing a single unit? And if the external system fails, is it really fair to fail the test of our code? A mock guarantees that we get the response ...

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