Modern Python Projects Transcripts
Chapter: Testing code
0:00 In this chapter, I showed you how to use pytest, the most popular testing framework for Python. Since we saw a lot of different features,
0:08 let me give you a short summary. The most important features of pytest are simple assertions,
0:15 so you don't have to remember different types of assertions, and everything can be achieved with a simple assert statement.
0:21 Also, Discovery of tests, Writing your first test is easy. Just add a test_ prefix to the name of the function and put that
0:31 function inside the file, that is also prefix with test _. That way, pytest will automatically recognize that it's a test function.
0:39 So no need to create a test class if you don't really need it. Plenty of CLI options, pytest has a very extensive set of parameters that you can pass
0:48 when you run in the terminal. Easy to use fixtures, Using fixtures in pytest is quite easy.
0:54 All you have to do is to decorate the function with pytest .fixture, and that's it. Now you can use this fixture in your tests,
1:03 and also pytest comes with some building fixtures. You can easily mark tests across all the files, marks are a great way to split
1:12 your tests into different groups. For example, if some of your end to end tests are slow and you want to run them only on the CI server,
1:20 you can mark them as slow and exclude them from running on your computer. Plugins are a true power of pytest.
1:28 If some feature didn't make it to the main pytest module, there is a huge chance that there is a plugin that you can use.
1:35 There are plugins to run tests in parallel, using multiple code, to generate the test coverage for your code or even to print the
1:42 test reproducing emoji. And if you want, you can always write your own plugin to further extend the functionality of pytest.
1:50 And finally, pytest is compatible with unit test, so you can switch and your
1:55 old test will still work. Then you can gradually replace the unit test with the pytest version.