avatar Bite 248. Test a number guessing game


Note this is a Test Bite and uses Python 3.7 and MutPy 0.6.1

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.

Login and get coding
go back Advanced level
Bitecoin 4X

40 out of 44 users completed this Bite.
Will you be Pythonista #41 to crack this Bite?
Resolution time: ~93 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 7.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

We use Python 3.8