#100DaysOfCode in Python Transcripts
Chapter: Days 10-12: Testing your code with pytest
Lecture: Setup and a guessing game to test
0:00 Let's make a virtual environment
0:02 and install pytest and also pytest coverage,
0:06 which I will use to show you how much
0:08 of the code is covered by tests.
0:11 Let's head over to my terminal
0:13 and make a virtual environment, and I'm using Anaconda,
0:17 so I'm pointing virtualenv
0:19 to the python binary in my Anaconda path.
0:23 As by convention,
0:24 I use venv for all my virtual environments,
0:27 that I can just run this.
0:30 Then I have another alias, ae,
0:35 which will source the venv activate script.
0:39 So that's basically enabling my virtual environment.
0:43 And we have nothing installed,
0:44 and let's now install pytest and pytest coverage.
0:52 Let's look at that little guessing game
0:55 we're going to write test for.
0:56 It lets me guess 5 times, a number between 1 and 20,
1:00 and it will feedback if I'm too low or too high.
1:04 That's funny.
1:07 9 is too high.
1:10 Wow, I'm a good guesser.
1:17 Right. You see here,
1:19 there are some validations we need to do.
1:21 So a number I cannot guess again,
1:23 I need to have a valid number,
1:26 I need to have a valid range and I cannot just do strings.
1:30 So that is all stuff that's in
1:32 that program I will show you next
1:33 and we have to write test for. And we have 5 guesses.
1:40 Right. So here, I didn't guess it,
1:43 and it bailed out after 5 times.
1:45 So that's the program.
1:46 Now look at how that looks in code.
1:48 So we have to max number of guesses,
1:50 the start and end of the range,
1:52 we have a function to get a random number,
1:55 using the random module.
1:56 So we have to constructer
1:58 that sets the guess's internal set, sets the answer,
2:02 and it sets when bullion to falls.
2:05 Then we have the guess method which takes a user input
2:09 and see if there was actually a number,
2:11 if it was an integer, if it was in the range,
2:14 and if it was already guessed.
2:16 And if nothing bails out,
2:17 then we can add it to the guesses set, and return it.
2:21 Then we have an internal validate guess method,
2:23 which just sees if the guess equals the answer,
2:26 otherwise, it gives feedback if it's too low or too high.
2:29 If a property of number of guesses
2:31 that's basically just the length
2:33 of the internal guesses variable,
2:35 and there's a dunder call which makes
2:37 that I can instantiate the class
2:40 and call that object as if it was a function.
2:43 That will initialize a while loop,
2:46 checking if the number of guesses is below the limit,
2:49 try to get a guess, check for a failure error,
2:52 print that and continue, validate the guess.
2:55 If there was a win or done,
2:57 make sure that the print guess or guesses,
3:00 reset the win to true and re-break.
3:02 If the while loop exits without breaking,
3:05 then we know that you have guessed the maximum number
3:08 of times and you didn't find it.
3:10 Although some people are against this,
3:11 I do find this construct useful in this particular case.
3:16 This gives us a nice playground to start using pytest.