Modern Python Projects Transcripts
Chapter: Testing code
Lecture: Useful pytest plugins
0:01 Let's take a look at some of the useful pytest plugins. First, we have pytest-xdist, which is a plugin that lets you run your tests in parallel.
0:15 Once you install it, you can run pytest-n auto and pytest will automatically split all test between all the CPU cores that you have and this can
0:26 significantly speed up your tests. And if you have some crazy large amount of test
0:34 that still take too long to run even when using all the CPU's of your computer you can even use this plugin to.
0:40 Execute your tests on a remote machines. Next, we have pytest-cov. You can use it to generate a test coverage report at the end of each test
0:50 run. When you run pytest --cov parameter, you will see a summary of How much code is covered with tests in general. Plus, you get a nice,
0:58 detailed information for each file so you can see which ones don't have enough tests
1:05 pytest-clarity is a very useful plugin that improves the readability of your pytest output By default, If you have,
1:12 for example, a large dictionary and some keys are different than expected. The output from pytest can be quite confusing,
1:20 but with pytest clarity, it's much more readable by default, When you run pytest, you will see the errors only after all the
1:30 tests have been run. But if you want to see them immediately, you can install this pytest-instafail, plugin, and when you run pytest --instafail,
1:40 you will see the errors and failed test immediately in the output. pytest-sugar is, ah, pytest plugin that. Criticize the output of pytest a bit.
1:51 You can see a nice progress bar and, just like pytest in stuff fail.
1:55 You will also immediately display the errors and failures instead of waiting until the end. So if you're tired of watching those dots,
2:03 you can install pytest plugin, quite a sugar plugin, and you will have a slightly different output.
2:11 If you notice that your tests are starting to get slow, you can install pytest benchmark.
2:16 With this plugin, you can run some benchmarks on your test and easily find which
2:21 one are the slowest ones. Do you remember when we talked about monkey patching a
2:27 few lessons ago, I showed you how to mock stripe, so it doesn't charge a real credit card when you're on your test.
2:34 But what happens when you manually test something? Use a real credit card, and then you forget to remove this code.
2:41 Well, that credit card will be charged each time you run your test, and that can be an expensive mistake.
2:48 Or maybe you forgot to change the database settings for your test, and you accidentally dropped the production database.
2:54 I mean, those are extreme examples, but they can happen, so you can install pytest-socket plugin,
3:00 and it will disable all network calls in your tests, so you won't be able to access anything outside of your local host machine.
3:07 So no access to stripe API or to your production database. Last but not least, we have our lovely pytest-emoji plugin.
3:18 If you like a emoji and you find the default output from pytest boring,
3:22 you can check it out, as we already saw it replaces the output of pytest with some emojis.