Getting started with pytest Transcripts
Chapter: pytest Plugins
Lecture: Repeating tests with pytest-repeat

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's say I've got a test that's a little slow. So this test is actually just slow because I'm sleeping inside of it.
0:08 Not a good thing to do within test. Try not to have to sleeps. However it's useful for demonstrating a couple of things that we're going to look at.
0:17 I wanna let's now let's also pretend it's got a bug in it that it's you know it's failing once in a while but not all the time.
0:25 And I'd like to run it like 10 times or something like that to see if I can repeat the problem. I could maybe parameterize it I could do
0:33 pytest mark parameterize and give it maybe X. And maybe range 10. But then I have to include X.
0:46 That I'm not really using and I have to import pytest This will run it 10 times I think. Let's try it By test test slow 123 sure ran
0:59 at 10 times about 2.5 seconds. Now the problem with this is I'm modifying the test and that's not terrible but it is what it is.
1:10 And also we're using this parameter to don't really need it. So instead of doing that let's take that out and let's use the repeat plugin
1:19 So pip, install pytest repeat. Now we can run the same pytest test slow we'll do -V.
1:31 So we can see it in action and we'll give it count equals 10 and now it's running it about the same time as it did before about 2.5 seconds.
1:43 And it gives us this nice little one of 10. 2 of 10. 3 of 10. 4 of 10. 10 of 10. Really cool. 2.5 seconds to run 10 tests.
1:52 But I can repeat it. So now I don't have to modify the test. If I say, oh well I'm not finding the problem with 10. Let's go to 20 or 100.
2:00 I'm not going to make you wait for this, but let's do something shorter. So like five now. I can just do it five times. Sweet. I like that. It's nice,
2:08 nice and clean. It realizes that it doesn't need to count. If you just do one, it just pretend you didn't call it and there you have it.


Talk Python's Mastodon Michael Kennedy's Mastodon