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.
0:04 Don't get me wrong. Unit test is not a bad library.
0:08 If you prefer to write test in object oriented way,
0:11 it's perfectly fine to use it.
0:13 But pytest has much more features,
0:14 and in the long run, it's a much better frame-work to use.
0:19 If you're starting your product from scratch,
0:21 then it's not a problem. You can start writing your test in pytest and
0:25 everything works fine. But what if you have an existing project full of test written
0:29 in the unit test? Well,
0:31 as we already saw, pytest has a backward compatibility with unit test so out of
0:36 the box it will run your unit tests.
0:39 Now you can either slowly migrate them from unit test to pytest.
0:43 Or, if you don't have time,
0:45 you can leave those all the unit test as they are and write new test in
0:49 pytest. Let's take a look at the file that we used to start this chapter
0:53 This is a unit test that tests some string methods,
0:58 so we have a class. We have three tests inside.
1:01 If we now go to the terminal and try to run pytest on it.
1:04 It will work. Now let's say we want to add another test,
1:11 but this time with pytest. The best idea is to.
1:14 Just create a new file and add pytest to there.
1:17 But you can also make pytest and unit test here if you want.
1:22 Let's just let's say we want to write a test for lower matter this time
1:27 Let's just take this out.
1:31 Remove parameter. And replaced the assertion, back to the terminal.
1:44 We run again, and now we have four test passing.