#100DaysOfCode in Python Transcripts
Chapter: Days 10-12: Testing your code with pytest
Lecture: Mocking randomness / pytest-cov

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright. Back to our guessing game. So, how do we want to test this program? Ideally, you want to test one function or functionality
0:09 in one pytest function. So let's start with the Get Random Number. I'm going to open a file test on the scoreguess.py.
0:21 Again, using pytest I have the convenience to not having to import any module to run the test. What I do need is to import the actual program.
0:30 So from guess import get random number and the game class. Now, one thing I want to show you in this video is how to
0:39 mock an object. Because Get Random Number, as you can see at the right, uses a random integer from start to end.
0:46 And random returns to something randomly every time. So how do you actually test that? And the way to do that in testing land is to mock an object.
0:55 And for this I'm just going to use the unittest patch method on the mock module because it's a perfect fit for this scenario.
1:06 So from unittest.mock, import patch. I actually need to import to random module because that's the one we're going to mock.
1:16 And you can use it as patch object and that's to random module. Just specify the function or method you want to patch. And then in your test function
1:36 you can pass in an argument and you can give that argument a fixed return value. And that's key because
1:43 instead of having random return something else every time you can give it a fixed value. So it's kind of an override of what randint() normally does
1:54 which is randomness. Now we're saying every time random gets called it gives us 17, and it makes that at least we can call our function,
2:03 which is get random number, and we know that it returns 17. Yeah, and this is pretty basic, but it should show you
2:12 how you can override certain things in your program you cannot really control and I have another example later
2:18 about the input function where we ask for user input, which is another area that can be anything, so you want to mock that out.
2:28 So with this code written, let's go back to the command line and run this test. And I'm using Control Z on a Mac with foreground to
2:36 toggle back and forth between my file editor and the terminal. And here I can just run pytest, and that's funny
2:45 because the previous example I put in a hello subdirectory and it's actually cool that we see this because
2:50 pytest is smart enough to look recursively for test files. So in this case it found two and ran them both.
2:58 So I'm going to move this out to somewhere else, because we now want to focus on the guessing game. And yeah it runs fine, and what I also want to see
3:09 from now on is how much coverage we have of our tests. So we installed pytest-cov and to use it was a bit of a
3:22 trial-and-error for me, but I found this syntax to work well for me. So I want a coverage report term missing coverage dot current directory.
3:32 And the term missing is cool because it starts to index all the-- okay, I actually have to give it something more specific
3:40 because it starts to look in my virtual environment. Alright, what I did was in the end moving the files into a subdirectory so we got our venv
3:48 and we got our guess new subdirectory with the script and the test file in there. And when I specify a subdirectory in the minus minus scuff
3:58 argument then it just rounds on our code, and what's cool about the missing argument is that it shows the lines in the code that are not having
4:06 test yet, which is of course is a lot because we just got started. But even so, we have 24 percent coverage so
4:12 we are up for a good start. And you can then map those lines back to the actual program so this is not tested 29. This is not tested, et cetera.
4:26 One final thing, as I use Vim I'm going to use the coverage command quite a lot so in my Vim RC, which I mapped to VRC to edit it,
4:36 I have a comma T which maps to save the file or run a command with the exclamation mark and then I run this command and I'm going to--
4:44 yeah, I think that's fine because we're going to work in the guess directory from now on and the venv is not sitting there
4:51 so the dot should work there. And we can confirm that by going into guess, run a test, run a coverage report with a dot. Yeah that's fine.
5:06 So when I'm writing my tests, I can just hit comma T, it saves the file and it runs my coverage. So that's a bit of Vim trick or shortcut
5:19 for Pycharm or another editor. There must be a similar way to do this but this is my way of running coverage with one keystroke in Vim.


Talk Python's Mastodon Michael Kennedy's Mastodon