Modern Python Projects Transcripts
Chapter: Testing code
Lecture: Converting unittest test to pytest

Login or purchase this course to watch this video and the rest of the course contents.
0:00 If we go to the unit test documentation,
0:02 we can see a basic example.
0:06 Let's try to write the same test,
0:08 but this time using pytest.
0:10 So, let me copy this for the reference. Let's name a test_string_methods.
0:23 And now let's create a second file.
0:24 And let's name it pytest_example,
0:31 Let's move it to the right side, so we can see both files on the same
0:34 time. Perfect. So first,
0:37 instead of importing unit test, we're going to import pytest.
0:42 Since this is a third party library,
0:44 we have to install it with pip, before we can actually use it.
0:48 So let's actually do this before we forget.
0:55 First, let's activate the virtualenvwrapper and let's create a virtual environment that we're
1:00 going to use. Next, let's install pytest and let's tell VSCode to
1:14 use this virtual environment with pytest.
1:16 Otherwise, we're going to get this problem with the import,
1:21 not this thing. So here,
1:24 that's search for pytest. Well,
1:26 there is none. so let's reload the window that way.
1:30 VSCode will pick up any new virtual environments that we have created.
1:36 And as you can see, here we have the pytest chapter,
1:38 virtual environment that we just created.
1:41 We select this and even though we still get this unresolved import,
1:46 it comes from the Python language server. so we can ignore it for now.
1:50 Okay, so we have pytest,
1:52 and now we could create a class to group all our tests together.
1:57 But with pytest, it's not necessary.
2:00 In case of a unit test,
2:01 you always have to create a class that inherits from this unittest.TestCase.
2:06 And then you have to create functions inside, with pytest,
2:10 All you have to do to create a test, is to write a function that starts
2:14 with a word test inside the file that starts with a test prefix,
2:19 and pytest will automatically detect all those as best cases.
2:23 And if you don't like this convention,
2:25 you can use whatever name you like and then you just change the pytest configuration
2:30 to tell pytest how you're naming your tests,
2:33 so I'm not going to create a class.
2:35 Let's start with the first function.
2:41 And now we have next difference between unit test and pytest.
2:44 Unit test, has a lot of different assertions.
2:47 If you want to check that something is true,
2:49 you have to use assertTrue.
2:51 If you want to check that something is false,
2:53 you have to use assertFalse.
2:54 If you want to check that two values are equal,
2:56 you have asserEqual and so on.
2:59 You can go to the unit test documentation to see the list of all the available
3:03 assertions. On the other hand,
3:06 with pytest, we only have a simple assert,
3:09 assert takes an expression, evaluates it and checks if the return value is true.
3:15 So if pytest, if you want to compare that something is equal to something
3:19 else, we just right assert 'foo'.upper() == 'FOO',
3:23 Next, we have a test for its upper.
3:31 If you want to assert that something is true or false,
3:34 all you have to do is to,
3:36 run assert something is true or assert something is false.
3:44 We're getting those warnings because Flake 8 is expecting two blank lines.
3:48 So, let's actually format our file with black,
3:52 and that is fine. We still get this warning from Flake 8 because we imported
3:57 pytest, but we're not using it.
3:59 Don't worry, we'll actually use it in the next test,
4:02 and then the Python language server is still complaining that the import is unresolved,
4:07 but well this we can ignore.
4:09 And now we have the final test.
4:20 One thing that we can't do with the simple assert statement is to assert.
4:24 That exception was raised. We can't do aseert type error because this is going
4:30 to give us a syntax error.
4:32 So, instead we have to call pytest.raises().
4:37 And that's how we can check that,
4:38 An exception was raised and again with pytest,
4:47 We don't have to write this, because pytest will automatically detect that those are our test
4:53 functions and it will call them.