Pybites Logo

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 mutation testing 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).

Good luck and keep calm and code in Python / pytest.