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