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