#100DaysOfCode in Python Transcripts
Chapter: Days 10-12: Testing your code with pytest
Lecture: Concepts: what did we learn
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review what we've learned so far. In this lesson we used a guessing game to write pytest. And the game went like this.
0:10
There were a maximum of 5 attempts to guess a number and you got feedback if the number was too low or too high. We compared pytest to unittest
0:20
and saw that it's pretty succinct. For unittest you need a class and self-serve equal syntax. pytest just lets you write functions.
0:31
And this is a very simple example, but you're already see that pytest requires far less code. pytest has nicely formatted outputs.
0:42
For example, if a fail, then we pass. We saw pytest code to show how much of our code was on our test and where tests were missing
0:55
it showed the lines in our script. We saw mocking in action, by mocking out the building input. Because we wanted to control what the input function
1:08
returned when playing a game. We also saw an example of mocking out the randint() function. We also saw how to test exceptions
1:20
with pytest, which is pretty easy. And we saw how to get your outputs from your program using the capfd. And that's nice because then we can check
1:30
if the output was as expected. And here we have a complete game, wrapped into a test. And then we did some TDD on Fizz Buzz,
1:47
writing tests, writing a bit of code, writing for more tests, some more code, some more tests and some more code.
1:58
And notice here we used the parameterize feature to prevent duplicating a lot of test code. So it takes a list of typals and in the body
2:07
of the test function, I can just do one assert. So, that's how we kept it dry. And again, the output is very nice.
2:18
Alright, that was quite some material to take in. I hope you are more comfortable with pytest now. And now, it's your turn.
2:25
Keep calm and code in Python.