Mocking a standard library function
Level: Intermediate (score: 3)
We moved from mutpy
(<= 3.7) to mutatest
(3.12 📈) for test Bites.
First we check if you have 90% test coverage, then we inject some mutations into the code to see if your tests catch them. 🤯
Good luck 🔥 💪 (please be patient 🙏 - it can take a few seconds to run the tests 🕒)
In this Bite you will mock out a function of the standard library, more specifically random.sample.
We wrote a small generator that produces hex colors. We also provided a fixture to initialize the generator so you can just call next(gen)
to get the next hex value.
Use unittest.mock
's patch
to mock out the call to sample
. It might be a bit tricky, but once you get this one down you possess a valuable testing skill!
Have fun and keep calm and test with pytest
!