Test a number guessing game
Level: Advanced (score: 4)
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 test a simple number guessing game (class). Ready to write some pytest
code? Read on ...
You again will need to mock out a standard library function, input
this time.
We tried to use random.randint
to get the secret number, but mutpy
became a bit too strict on us, so for now we just pass the secret number into the constructor. That said, there is some validation going on that you will need to test. Check for boundaries and to check exception strings, pytest
's raises
has a nice match
kwarg.
The constructor also takes an optional max_guesses
arg to make the game easier or harder. You will need to the use capfd/capsys
fixture again to test the standard output of the game (100% test coverage remember).
With all this testing goodness going on we label this an Advanced Bite, also due to the number of LOC we ended up with (> LOC of the game!). We also had to kill quite some mutants along the way (no worries, only mutpy
mutants!) so well deserved the 4 points towards your next Ninja belt upon solving this one!
Good luck and keep calm and code in Python / pytest
.