4 ms·
How do you add a side effect and still call the original in pytest? This is easy in rspec but everywhere I google people say there's no good way. For example,
by drchickensalad 5y ago
How do you add a side effect and still call the original in pytest? This is easy in rspec but everywhere I google people say there's no good way.
For example, you're calling code which internally does Too().bar() and you want to advance your time mock after it's called.
- masklinn 5y agoThat… has nothing to do with pytest? You’d probably use something like `wraps` or `side_effect` in `unittest.mock` to delegate to the original with some extra behaviours. And / or use freezegun’s tick features, when you’re specifically dealing with time.
- simonw 5y agoThere are lots of good mocking fixtures available for pytest. I've used this one for clock stuff in the past: https://github.com/adamchainz/time-machine#pytest-plugin https://github.com/adamchainz/time-machine#pytest-plugin
- bb88 5y agoReplace the original function with a wrapper that calls the function then does the side effect. This could effectively be something like a decorator. As far as mocking time objects, look into pytest.freezegun. You should be able to control the date and time as you move forward. I was able to mock out the datetime.now() at some point in the past. It wasn't with freezegun...