#100DaysOfCode in Python Transcripts
Chapter: Days 10-12: Testing your code with pytest
Lecture: Setup and a guessing game to test

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's make a virtual environment and install pytest and also pytest coverage, which I will use to show you how much of the code is covered by tests.
0:12 Let's head over to my terminal and make a virtual environment, and I'm using Anaconda, so I'm pointing virtualenv
0:20 to the Python binary in my Anaconda path. As by convention, I use venv for all my virtual environments, that I can just run this.
0:31 Then I have another alias, ae, which will source the venv activate script. So that's basically enabling my virtual environment.
0:44 And we have nothing installed, and let's now install pytest and pytest coverage. Let's look at that little guessing game we're going to write test for.
0:57 It lets me guess 5 times, a number between 1 and 20, and it will feedback if I'm too low or too high. That's funny. 9 is too high.
1:11 Wow, I'm a good guesser. Right. You see here, there are some validations we need to do. So a number I cannot guess again,
1:24 I need to have a valid number, I need to have a valid range and I cannot just do strings. So that is all stuff that's in
1:33 that program I will show you next and we have to write test for. And we have 5 guesses. Right. So here, I didn't guess it,
1:44 and it bailed out after 5 times. So that's the program. Now look at how that looks in code. So we have to max number of guesses,
1:51 the start and end of the range, we have a function to get a random number, using the random module. So we have to constructer
1:59 that sets the guess's internal set, sets the answer, and it sets when bullion to falls. Then we have the guess method which takes a user input
2:10 and see if there was actually a number, if it was an integer, if it was in the range, and if it was already guessed. And if nothing bails out,
2:18 then we can add it to the guesses set, and return it. Then we have an internal validate guess method, which just sees if the guess equals the answer,
2:27 otherwise, it gives feedback if it's too low or too high. If a property of number of guesses that's basically just the length
2:34 of the internal guesses variable, and there's a dunder call which makes that I can instantiate the class and call that object as if it was a function.
2:44 That will initialize a while loop, checking if the number of guesses is below the limit, try to get a guess, check for a failure error,
2:53 print that and continue, validate the guess. If there was a win or done, make sure that the print guess or guesses, reset the win to true and re-break.
3:03 If the while loop exits without breaking, then we know that you have guessed the maximum number of times and you didn't find it.
3:11 Although some people are against this, I do find this construct useful in this particular case. This gives us a nice playground to start using pytest.


Talk Python's Mastodon Michael Kennedy's Mastodon