vendredi 12 janvier 2018

Why does unittest.mock patch locally?

In the python3 unittest.mock docs, on the subject of "Where to patch", say that:

The basic principle is that you patch where an object is looked up, which is not necessarily the same place as where it is defined.

This makes sense when doing BDD unit tests and mocking aggressively, because instead of coupling your tests to the dependency by patching where the functionality is defined (like you would with monkey patching), you're coupling your tests instead to how the dependency is used by the system under test. That makes tons of sense.

In acceptance testing however, you're testing a much broader system under test – in which you might be using a single dependency thousands of times. In this kind of test, my focus isn't really on the behavior of the system under test with relation to the mock, although that comes into it; it's more on the behavior of the system as a self-contained whole. My goal is to avoid making api calls to my email provider, etc. It seems to me much more reasonable to use monkey patching instead of local name-resolution patching.

So am I correct that unittest.mock.patch is designed with behavior-driven unit tests in mind, or does the implementation of patch make sense for acceptance testing too?

Aucun commentaire:

Enregistrer un commentaire