From the course: Testing Python Data Science Code

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Mocking

Mocking

- [Instructor] Mocking is taking an object and replacing it with something else that behaves the same. One of the reasons is that it's hard for us to actually work with a real object. Let's say, start a real authentication server. But you need to remember, every time you mock, you're actually cheating. Your code is not running against the actual authentication server. It is going to run against your own mock, which you think is the way the server is going to behave. I try to avoid mocking as much as possible. But in some cases, especially simulating errors, I find mocking a highly effective tool. Let's have a look. We are going to use two libraries. One is the built-in monkeypatch fixture from pytest. It lets us set attributes and delete attributes temporarily within the test. The second one is the built-in unittest.mock library that comes in with Python. Here we have a database client which does a database…

Contents