Modern Python Projects Transcripts
Chapter: Testing code
Lecture: Moving from unittest to pytest

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I hope that by now I convince you that pytest is a great testing framework. Don't get me wrong. Unit test is not a bad library.
0:09 If you prefer to write test in object oriented way, it's perfectly fine to use it. But pytest has much more features,
0:15 and in the long run, it's a much better frame-work to use. If you're starting your product from scratch,
0:22 then it's not a problem. You can start writing your test in pytest and
0:26 everything works fine. But what if you have an existing project full of test written in the unit test? Well,
0:32 as we already saw, pytest has a backward compatibility with unit test so out of the box it will run your unit tests.
0:40 Now you can either slowly migrate them from unit test to pytest. Or, if you don't have time,
0:46 you can leave those all the unit test as they are and write new test in pytest. Let's take a look at the file that we used to start this chapter
0:54 This is a unit test that tests some string methods, so we have a class. We have three tests inside.
1:02 If we now go to the terminal and try to run pytest on it. It will work. Now let's say we want to add another test,
1:12 but this time with pytest. The best idea is to. Just create a new file and add pytest to there.
1:18 But you can also make pytest and unit test here if you want. Let's just let's say we want to write a test for lower matter this time
1:28 Let's just take this out. Remove parameter. And replaced the assertion, back to the terminal.
1:45 We run again, and now we have four test passing.


Talk Python's Mastodon Michael Kennedy's Mastodon