Apparently TDD is the way to to modern software development. I say that facetiously, TDD is the ONLY way to do software development!! However, I often find that writing the tests end up taking way more work and is way more complicated than writing the code itself, but mostly that’s because I am a newbie to PyTest and unit testing in general.
So today I’ve been writing a little test for a decorator that determines whether a particular route is allowed or not via a feature switch stored in Redis. Now obviously I need to isolate the decorator function from all the other stuff like the Redis cache and some other bits about permissions. The easy way to do this is to monkeypatch those functions. I’ve done this SO many times, and every time I forget how to do it, hence this post.
So the very basic mocking I did to replace the
get_feature_switches is as follows:
First, create a copy of the return you’d expect from Redis, in this case it’s just a list of switches. I want then all to be off so in my case it’s just this:
feature_switches_all_off = 
Then the actual test:
So I’m using PyTest monkeypatch to replace
get_feature_switches with my very simple
Literally the simplest thing ever, and now I will never forget :)