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