0

How to patch my custom decorator with monkeypatch or pytest.mock? i manage to mock it by doing (answer to this question):

package.decorator = mytestdecorator

The problem is that it breaks some other tests where i actually need that decorator to work.

Quba
  • 4,776
  • 7
  • 34
  • 60

1 Answers1

2

You have to control complete lifecycle of your mocked decorator and revert the decorator back to original state.

It can be done in few different ways:

  • context manager which builds the mocked decorator, and reverts it back by __exit__.
  • setup and teardown functions for your test, teardown must revert the decorator.
  • pytest fixture with finalizer
  • pytest fixture with yield expression.

Personally I like the @pytest.yield_fixture as it keeps the code short and as soon as you realize, that all what comes after yield statement in the fixture code is cleanup code, things are very clear to do.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98