Latest / Talk Python To Me / #267: 15 amazing pytest plugins
Transcript
- 0:00Do you write tests for your code? You probably should.
- 0:02And most of the time, pytest is the industry standard these days.
- 0:06But pytest can be much more than what you just get from installing it as a tool.
- 0:11There are many amazing plugins that improve pytest in many aspects.
- 0:15That's why I've invited Brian Okken to the show to tell us about his favorites.
- 0:19Listen in and your Python testing will be faster, stronger, and more beautiful.
- 0:23This is Talk Python To Me, episode 267, recorded June 4th, 2020.
- 0:29Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 0:48This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy.
- 0:52Keep up with the show and listen to past episodes at talkpython.fm.
- 0:56And follow the show on Twitter via at Talk Python.
- 0:59This episode is brought to you by Linode and Sentry.
- 1:01Please check out what they're offering during their segments.
- 1:04It really helps support the show.
- 1:05Brian, welcome back to Talk Python To Me.
- 1:08Thanks. It's fun to be back.
- 1:09Yeah, it's good to have you back.
- 1:10You've been on a lot of episodes.
- 1:12You've been on five.
- 1:142017 Python Year in Review, 2018 Python Year in Review, the Python Testing Column is a Thing,
- 1:2030 Amazing Python Projects, which is one of the most popular episodes, actually.
- 1:25And the book author panel discussion.
- 1:29So happy to have one more on the list to have you here.
- 1:32Yeah, it's nice.
- 1:33I love your show.
- 1:33Listen to it all the time.
- 1:34Thanks.
- 1:34Yeah, and it's good to catch up with you.
- 1:36It's been a while.
- 1:36Yeah.
- 1:38Well, we, I mean, we talk to each other weekly.
- 1:41That's right.
- 1:42Of course.
- 1:43Of course.
- 1:44Yeah.
- 1:44So those of you listening who don't know about Python Bytes, that's our other podcast that
- 1:50Brian and I co-host.
- 1:51We each have separate podcasts, right?
- 1:53Talk Python and I do alone.
- 1:54You do Test and Code, which is really good.
- 1:57Yes.
- 1:57Where do people find Test and Code?
- 1:58At testandcode.com.
- 2:01Perfect.
- 2:01But we also do Python Bytes, which is like a newsletter for Python with analysis and audio
- 2:06form.
- 2:07It's a lot of fun.
- 2:08Yeah, it is.
- 2:09People should check that out as well.
- 2:10So I want to have you tell your story because you probably told it four times out of five there
- 2:15anyway.
- 2:15So let's just sort of check in and see what you've been up to in the last year or so.
- 2:22It's been actually pretty exciting.
- 2:24I think I've told the story before, but I work at Roden Schwartz.
- 2:29So I work at a test and measurement company.
- 2:30I'm an embedded software developer and a team lead and a lead engineer.
- 2:34But I've been focusing for the last 10 to 15.
- 2:39Half of my career has been on communication testing.
- 2:43Most of it's been cellular stuff and we're switching over the last year or so.
- 2:47I've been focusing mostly on Wi-Fi testing.
- 2:50So that's been a lot of fun.
- 2:52Okay, nice.
- 2:52Like Wi-Fi 6, that kind of stuff?
- 2:54Yes.
- 2:54One of the fun things lately is some of the work on 802.11x, which is just starting to roll
- 3:00out around the world in different places.
- 3:02Yeah, that's cool.
- 3:03I'm actually very excited about that.
- 3:05Yeah.
- 3:05Yeah.
- 3:05It's neat.
- 3:06Also, one of the reasons why I kind of focus on testing a lot is I am a proponent of developing
- 3:12software with testing hand in hand so that you develop tests and code at the same time.
- 3:17Yeah.
- 3:17It's always very error prone and unlikely to succeed to say, now that you've written the
- 3:24code, let's go back and add all the tests to it.
- 3:26Yeah, definitely.
- 3:27But people don't learn how to write tests when they're in.
- 3:30I mean, even college classes hardly ever talk about how to write good tests.
- 3:36Yeah, there's definitely that sort of splintering there.
- 3:39Like on one hand, the stuff you're building in college, it's probably not going to be long
- 3:44lived, right?
- 3:45It's an assignment or a short project.
- 3:48You just want to get it done because you also got that English paper and you didn't really
- 3:52even want to take English.
- 3:53But now you're in there and you got to do it.
- 3:54So let's just get it done.
- 3:55And it's a very different thing to think a team of people have to live with this for a
- 4:00period of time.
- 4:01Yeah, definitely.
- 4:02And with a more and more reliant on, it's a little bit easier of a thing for me to try
- 4:07to tell people the importance because they're used to working.
- 4:10A lot of people are used to working with open source projects now and you would never pull
- 4:14an open source project off the shelf and use it if it had zero tests around it.
- 4:18Yeah.
- 4:18And a lot of times you've got to have tests to accompany your contributions back if you're
- 4:23doing a PR, right?
- 4:24Yeah.
- 4:24Yeah, definitely.
- 4:25It's getting more and more attention.
- 4:27And I think that's a good thing.
- 4:28What's the work from home story for you these days?
- 4:30Do you got to go to the office?
- 4:32I mean, the world scrambled for everyone, but the only part of my world that hasn't changed
- 4:36is my work because I've already been fully distributed and whatnot, I guess, other than
- 4:42conferences.
- 4:42But how about you?
- 4:43Yeah.
- 4:43So it was a hard shift.
- 4:45I think it was early March, was it?
- 4:48Really early March.
- 4:50There was a day where we just got told, grab your stuff and go home.
- 4:54And we're working from home from now on for a while, at least.
- 4:57And we had just moved into a beautiful office in July.
- 5:01So it's...
- 5:02It is really nice.
- 5:03Like, it's a place you would want to go to because it's got cool decor and whatnot.
- 5:07Yeah.
- 5:07And these giant training rooms and stuff.
- 5:09It's a good building.
- 5:11So working from home, the entire...
- 5:13I had just hired a couple people for the team.
- 5:15And so training people remotely was...
- 5:18I thought it was going to be a trick, but it's actually been...
- 5:20It's gone really smoothly.
- 5:21Oh, that's good.
- 5:22And yeah, so we're a remote team.
- 5:24We are...
- 5:25So in our county...
- 5:27So I'm in the county, Washington County that we're in around Portland.
- 5:30We are supposedly entering phase one.
- 5:33We're doing this three-stage phase going back to work thing.
- 5:36We don't know.
- 5:37We haven't allowed people to go back yet, but hopefully there will be...
- 5:41There's some people that can more effectively work from the office.
- 5:45So we're going to try to get those people back as soon as we can, but do it safely.
- 5:49Do you think you'll have more work from home remote options?
- 5:53Generally, at the company because of this?
- 5:55Yeah, I think so.
- 5:56I think we've...
- 5:56One of the fears was...
- 5:57Then I think a lot of companies have seen this.
- 6:00The fear is that people working from home won't...
- 6:02They'll be more distracted.
- 6:04They won't be as productive.
- 6:06And I don't even know if this is a fair test because everybody's also...
- 6:10It's not just working from home, but their kids are home also.
- 6:13Yeah, exactly.
- 6:15There's also the stress of just what's going on in the world is a stressful situation.
- 6:21So even though it's not a fair comparison, we're comparing anyway.
- 6:25And things are going okay.
- 6:27We're still being productive, just about as productive or more so than we were before in
- 6:32a lot of cases.
- 6:33The lack of a commute is really easy.
- 6:35The ease of discussion.
- 6:37I mean, you can...
- 6:38I don't know.
- 6:39It just is...
- 6:39I get more time focused, but also I think I'm talking with my team more often now because
- 6:45we make it a point to talk every day as a team.
- 6:48Right, right.
- 6:48Focus type stuff, not just, oh, I passed them in the hall sort of thing.
- 6:51Yeah, that's cool.
- 6:52Now, one more thing, I guess while we're catching up on this stuff is you've got these really
- 6:57beautiful training rooms with nice giant screens.
- 7:00And one of the things that we were doing together is the Python Portland West meetup, which kind
- 7:06of got polished from all this as well, right?
- 7:08Yeah, and that's a big bummer because I think it was really serving a community in the west
- 7:13side of Portland that didn't have a meetup.
- 7:15And I would really like to...
- 7:17I have no idea when we're going to get back to being able to do that, but I'd like to be
- 7:21able to do something.
- 7:22Yeah, I don't even know when I can go to a restaurant.
- 7:24So, you know, things are all up in the air.
- 7:26The world is crazy.
- 7:27Yeah, definitely.
- 7:29Yeah.
- 7:30All right, cool.
- 7:31Well, good to catch up.
- 7:32Now, let's talk some pie tests.
- 7:35I'd like to do just a little bit of high-level stuff to just like set the stage, and then
- 7:38we're going to dive into the plugins.
- 7:40So, pytest is an external separate way to write and test Python code.
- 7:47Just not, say, part of Python like UnitTest.
- 7:49So, maybe let's just do like a quick survey of the modern testing tools that people might
- 7:55potentially use, and then like how we'd write tests in pytest for those who maybe have only
- 8:00done UnitTest type testing, or they're like, testing, maybe I should do that.
- 8:03Yeah, sure.
- 8:04Sure.
- 8:04Well, actually, you had a few on the list that I deleted, but...
- 8:07Because they're no longer modern, right?
- 8:08Well, I would say not.
- 8:11There still is UnitTest, and UnitTest is being supported, and I do think, and it's part of
- 8:16the Python distribution.
- 8:18That's the one winning point, I think, right?
- 8:21It's like, I don't have to have a requirements.txt or pip install anything, potentially, and I
- 8:27can still write tests.
- 8:28Yeah.
- 8:28One of the main reasons why it's there is to test Python and test the standard library.
- 8:34And I think that that's a good reason for it.
- 8:36There's a bunch of people that have built on tools around it, and I think that's fine, but
- 8:41it's just not my thing.
- 8:43One of the things, while we're talking about other testing app things, is it's a good thing
- 8:48to note that Nose is still available, but I don't recommend anybody use Nose or Nose2 or
- 8:54any of its variants, because, I don't know, Nose is not being supported at all anymore.
- 9:00And Nose2 has very little traction, and I don't know if it's, I don't currently know
- 9:06if it's active.
- 9:07So, if you're currently working on Nose, sorry about the disparaging remark, but maybe correct
- 9:12me if I'm wrong.
- 9:13Yeah, yeah.
- 9:14Well, I mean, the thing is, you've got all these choices, right?
- 9:16Like, if it's pytest or some of these other things like Nose or something, it's like, well,
- 9:21okay.
- 9:22Which one are you going to pick, right?
- 9:24First got into it, I really, I was looking for the easiest.
- 9:27And I thought that maybe the easiest and the most powerful would be two different things,
- 9:30but it turns out that pytest is both the easiest and the most powerful, which is nice.
- 9:35Nice.
- 9:35There's some other ones, though, that you might consider.
- 9:38Yeah.
- 9:38Well, in conjunction with pytest and other testing things, there's a lot of other tools that people
- 9:43use.
- 9:44Tox is used, it is not just a testing tool, but one of the neat things about it is you can
- 9:50do a test matrix or running different Python environments.
- 9:53It creates a, it tests a lot of your application.
- 9:56It's pretty, almost an episode in itself.
- 9:58Right.
- 9:58You can test on like various versions, like Python 3.5, 3.6, 3.7, and stuff with that, right?
- 10:04Yeah.
- 10:04And typically, if you're running, like running pytest, pytest doesn't automatically build your
- 10:10application for you, but Tox, part of its process is creating the wheel of your, like if you're
- 10:16doing a package, it'll create the package for you and then install it.
- 10:21And it does the whole round trip thing.
- 10:24So that's cool.
- 10:25Nice.
- 10:25Okay, cool.
- 10:26A lot of people are moving to, so continuous integration is definitely a thing that's important
- 10:31everywhere.
- 10:32And a couple years ago, it was probably Jenkins or Travis that people were using, but now
- 10:39I think more, a lot of people are moving to GitHub Actions because they're just right there
- 10:44with your code.
- 10:44So why not?
- 10:45Yeah, that's super easy.
- 10:47And it totally solves the problem that is, I think, one of the really big challenges of
- 10:51testing in teams is you get different levels of buy-in from different participants in the
- 10:57team.
- 10:57And some people are very, they just are reluctant to just run the test before they'll do a check-in.
- 11:03Yeah.
- 11:04They'll break the test, they'll do a check-in, and they don't even know they broke it, but
- 11:07they don't care that much.
- 11:08I mean, people care to differ in degrees, but it's like, well, they're kind of a nuisance,
- 11:11but I guess if you're going to make me write tests, I will.
- 11:14Right.
- 11:14And so the continuous integration is like, everyone gets checked before we accept your
- 11:19stuff, right?
- 11:20Well, right.
- 11:20So hooking it up within a GitHub Action, you could do this before, but it's really easy with
- 11:26these, is to just say, if you'd like, for instance, a merge request doesn't even go,
- 11:30doesn't even go very far if the test fails.
- 11:33So yeah.
- 11:35Listing coverage.py, because I'm, if you care about coverage, that's how you do that in Python.
- 11:40I don't do a lot of web testing, but I wanted to point out that Selenium and a wrapper around
- 11:45Selenium called Splinter are very commonly used.
- 11:49And then these are all packaged very well within a plugin called pytest Splinter so that you
- 11:54can easily control web applications.
- 11:56Nice.
- 11:57I hadn't heard of that one.
- 11:58I've heard of Selenium, but not Splinter.
- 11:59Yeah.
- 12:00A property-based testing is gaining a lot more traction in conjunction with functionality testing.
- 12:06And the solution within Python is a hypothesis for that.
- 12:10Nice.
- 12:10So everything you've listed here is pytest Plus, not instead of, right?
- 12:15Well, sure.
- 12:15It's pytest Plus these other things.
- 12:17However, they, I mean, none of them are pytest only.
- 12:20So you can use unit test with all of these things as well.
- 12:23Yeah.
- 12:23Or yeah, testing framework plus, right?
- 12:25They're not necessarily alternatives.
- 12:27Yeah.
- 12:27Right.
- 12:28All right.
- 12:28Cool.
- 12:28So those are definitely neat things to look into.
- 12:31They're separate projects.
- 12:33This portion of Talk Python To Me is brought to you by Linode.
- 12:37Whether you're working on a personal project or managing your enterprise's infrastructure,
- 12:41Linode has the pricing, support, and scale that you need to take your project to the next level.
- 12:46With 11 data centers worldwide, including their newest data center in Sydney, Australia,
- 12:51enterprise-grade hardware, S3-compatible storage, and the next-generation network,
- 12:57Linode delivers the performance that you expect at a price that you don't.
- 13:01Get started on Linode today with a $20 credit, and you get access to native SSD storage,
- 13:06a 40-gigabit network, industry-leading processors, their revamped cloud manager,
- 13:11cloud.linode.com, root access to your server, along with their newest API, and a Python CLI.
- 13:17Just visit talkpython.fm/Linode when creating a new Linode account,
- 13:22and you'll automatically get $20 credit for your next project.
- 13:25Oh, and one last thing.
- 13:26They're hiring.
- 13:27Go to linode.com slash careers to find out more.
- 13:30Let them know that we sent you.
- 13:34Now, like I said, just for people who maybe haven't written a test in pytest,
- 13:39like, what's the really quick flow?
- 13:41Like, why is it so simple?
- 13:42pytest has a really great discovery mechanism, but a function that just starts with,
- 13:47like, if you have a test file that starts with test underscore,
- 13:50there's some naming conventions.
- 13:52So the easiest thing is to have a test file that has test underscore in it,
- 13:56and then start your functions, test functions with test underscore,
- 13:59and then it'll all just get run.
- 14:01So pytest will find all of the files that start with test underscore and run all of the functions
- 14:07within them that start with test underscore, and then that's it, really.
- 14:11And so how does a test pass or fail?
- 14:13It fails if an exception is raised, and the easiest way to raise an exception is to,
- 14:18if you're intentionally checking something, is to use assert.
- 14:21So you, like, assert A equals B or something, and if the assertion fails, your test fails.
- 14:27It's that easy.
- 14:28Yeah, so you don't have to have decorators or derive from certain classes or anything like that.
- 14:33It's just the name involves test underscore, and you assert stuff.
- 14:36That's pretty good.
- 14:37Yeah, you don't even have to import pytest into your test files.
- 14:40They can be just functions.
- 14:42Yeah, that's right.
- 14:44So unless you're using some of the unique features that, like, extend pytest,
- 14:47like you talked about the way a test fails if there's an exception.
- 14:51Well, if you're testing for error handling,
- 14:53sometimes you want to trigger an exception and verify it has happened.
- 14:56So in that case, you'd have to include some pytest stuff to do, like,
- 14:59the width raises sort of stuff on it.
- 15:01But in general, for, like, the straight-ahead test,
- 15:06you don't even need to import pytest.
- 15:07Which is kind of weird.
- 15:08That freaked me out a little bit at first.
- 15:09I'm like, why am I not interacting with pytest?
- 15:11This is so weird at a code level.
- 15:13No, it's beautiful.
- 15:14It's one of the things I like is that it's just easy to start.
- 15:17Absolutely.
- 15:17Now, one other thing I want to cover before we dig into the list of plugins
- 15:22that we're going to cover is there's some non-obvious other ways to extend pytest, right?
- 15:29Like, it has this concept of fixtures that allow you to create and initialize stuff,
- 15:35and then automatically pytest has a way to, like, pass it along to your tests and whatnot.
- 15:39You'll see some of these plugins actually use fixtures, like some stuff about modifying time and whatnot that we'll talk about.
- 15:46What's the deal with those?
- 15:48Well, so the unit test model, and the unit test is similar to a lot of the X unit type, like J unit and PHP unit and stuff,
- 15:58have a similar model where you just, things are just named also and the tests get run.
- 16:03But there's often times where you need stuff to happen before your test gets run,
- 16:07like setting up a resource or generating data or something like that.
- 16:11And there's, so there's setup and teardown functions that you can, that are associated with these other test frameworks.
- 16:17pytest actually also supports those sorts of setup and teardown functions.
- 16:21If you want to use them, they are supported.
- 16:23But it's really not recommended because fixtures are so awesome.
- 16:27So a fixture is a way to combine, like, to write a function that is handling a resource or handling some data
- 16:35and be able to do the setup and teardown within one function and, if necessary.
- 16:42And then also just be able to scope those.
- 16:45And you can, you can have them have the resource or data shared between one test or many tests or even across the entire project.
- 16:53And you can control, you know, like if you're initializing a database or connecting to a web resource,
- 16:59you might not want to do that for every test.
- 17:01You can do it for one, once per session and then, and then do things like rollbacks and stuff per test.
- 17:07And that granularity of being able to shift your, the granularity of your setup and teardown is super powerful
- 17:14and really helps makes pytest shine.
- 17:16Yeah.
- 17:17And the way that you write it is super cool as well, right?
- 17:19Just have a test function that just takes a parameter.
- 17:21The parameter is named the same as the fixture and pytest just says, okay, I really call this function and pass it over.
- 17:27It could do really cool things.
- 17:29Like you could pass off your database data access layer and it could start a transaction before, hand it to you,
- 17:34and then roll back the transaction after the test is over.
- 17:38Right.
- 17:38And you don't have to worry about it.
- 17:40It could just take care of that for you.
- 17:41Yeah.
- 17:41Yep.
- 17:42And the fixtures are really what brought me to pytest.
- 17:44Some of the, there's some massively cool things you can do with that.
- 17:48Like one of the things that people don't think about is if you've got a system under test that has error logs somewhere,
- 17:55one of the things you can do within a fixture is check your error logs after the test runs to make sure there's no fatal errors that got saved somewhere.
- 18:02Yeah.
- 18:03Yeah.
- 18:03Very cool.
- 18:03Love it.
- 18:04Okay.
- 18:05So that's one way.
- 18:05The other one is this plugins in general, right?
- 18:08Like there's ways to extend pytest with plugins as a concept.
- 18:13The pytest has a whole set of hook functions as well.
- 18:16There's, there's a lot of things you can do.
- 18:18You can, you can write fixtures and package those fixtures for other people to use.
- 18:22So some plugins are just packaged fixtures.
- 18:25Other things you can do with hook functions.
- 18:27So with, I actually was intimidated when I first was reading about hook functions, but they're just, there's just a way for pytest to allow different parts of the way pytest runs to be hooked into.
- 18:40So you can, you know, alter a few things if you need to, like, like some of the test ordering ones.
- 18:45Once all the tests are collected, you can hook into that section and reorder them if you need to, or, or filter them.
- 18:52Choose which ones execute, right?
- 18:54Yeah.
- 18:54There's a whole bunch of ways you can hook into it and it's fairly complex, but a lot of people have come up with some really clever things and plugins allow you to allow you to do that.
- 19:05And I mean, not just third party plugins that you can pull off of PyPI, but you can also just, if you want to share some code that you have that works with your tests amongst different projects, you can package your own plugins to do that.
- 19:19Yeah.
- 19:19Oh, that's cool.
- 19:20You could even have them for like your company, right?
- 19:23Like we always do this in RCI and whatnot.
- 19:26You can just grab a plugin and do stuff.
- 19:28Yeah.
- 19:28Like I've got plugins that I've written that, that just are particular to one particular test piece of test equipment.
- 19:34So to control this piece of equipment, you can use this plugin and it helps you with some of the common things.
- 19:39Oh, that's cool.
- 19:40Makes it a little easier for new people.
- 19:41Yeah.
- 19:42Yeah.
- 19:42Lots easier.
- 19:43Nice.
- 19:43All right.
- 19:44I think it's time we go down the list.
- 19:47We've set the stage.
- 19:48We know what fixtures are.
- 19:49We know what plugins are, at least in general.
- 19:52Let's put some sugar on pytest to make it sweet.
- 19:55Yeah.
- 19:56So we're going to run through a whole bunch of really cool pytest plugins, right?
- 20:00Yeah, absolutely.
- 20:01All right.
- 20:02What's this first one here?
- 20:03So sugar is a fun one, actually.
- 20:04I ran into this right away with looking at pytest.
- 20:07So the default output of pytest is you, when you run it, you get a dot shows up for every, for every test that gets run.
- 20:16And it does list the files, but a little dot shows up.
- 20:19And maybe that's not enough for you.
- 20:22So pytest sugar shows you, like, green check marks instead.
- 20:26So, and then, you know, instead of, like, Fs for failure, I think there's, like, red Xs or something that show up.
- 20:33So it alters the output of pytest so that it looks a little nicer.
- 20:38Yeah, it also looks like it might be suppressing the module name, the folder names, and only, like, highlighting the actual file names.
- 20:46So it does, like, gray out a little part of the UI.
- 20:49So, like, the actual test file being run stands out.
- 20:51And there's a vertical progress bar going along that runs as well.
- 20:56That's pretty sweet.
- 20:57Yeah, that's pretty cool.
- 20:58And that was especially really helpful before pytest added the percentage done.
- 21:02So now, for each file, pytest lists the percent done now by default.
- 21:07But it didn't always have that, and pytest sugar had that.
- 21:10It's pretty cool.
- 21:11Yeah, it's cool.
- 21:11And there's a nice little animated GIF, as all things should have, on there that I'll link to in the show notes so people can check it out and see it going.
- 21:20So, yeah, pytest sugar.
- 21:21And this is cool because all you've got to do is install this, and it doesn't require effort, right?
- 21:26And, like, oh, and now I can do this thing in code.
- 21:28It's just, like, I install this, and it looks better.
- 21:31Yeah.
- 21:31How do you install plugins?
- 21:32Just do you pip install them?
- 21:34Like, do I need to register it with pytest, or is it sufficient to just pip install it?
- 21:37Just pip install it.
- 21:38It's about the design of the plugin as well.
- 21:41So some plugins, like Sugar, you just install it, and it starts working.
- 21:45You don't have to do anything.
- 21:46All right.
- 21:47But some of them intentionally don't start right away.
- 21:50So some plugins, they are installed, but you have to turn on the functionality with a flag or something like that.
- 21:56Got you.
- 21:56Okay.
- 21:56Cool.
- 21:57All right.
- 21:58What's the next one?
- 21:58Number two.
- 21:59So this one's definitely something, pytest cov, dash cov, it's a way to run coverage with pytest together.
- 22:08So, I mean, there's really two ways to run coverage on pytest tests.
- 22:13Coverage doesn't require pytest, right?
- 22:15So you can use any test framework.
- 22:18So you can run pytest from coverage, or you can run coverage from pytest.
- 22:22It's kind of a choice.
- 22:24But I usually use pytest cov because it's just really well supported.
- 22:29It does some really cool things.
- 22:31One of the things that it does really nice is, we'll talk about xDisk later, but xDisk allows
- 22:36parallel runs.
- 22:37And pytest cov cleanly collects all of the different parallel runs and pulls the report, coverage
- 22:44report together for you.
- 22:45Oh, that's cool.
- 22:46Which is really nice.
- 22:47Yeah, it has xDisk support, which we'll talk about what that means.
- 22:50But that's a pretty high-end piece of kit there to go grab a parallel executing stuff and bring
- 22:56it all together.
- 22:57Yeah, and there's also, it's really good about catching the coverage from right from the beginning.
- 23:01So you've got like some startup code that happens, like your fixtures and stuff that you've set
- 23:06up that run before your tests, making sure that those have coverage turned on as soon as
- 23:13possible so that you don't have startup code that doesn't get run and coverage.
- 23:17That's one of the issues with hooking up coverage often.
- 23:20But pytest cov handles that really cleanly.
- 23:23Yeah, awesome.
- 23:24It also has sub-process support, so you can fork things into a sub-process and it'll get
- 23:29covered automatically, which you might think, okay, who's going to do that?
- 23:33And like, do they really care about tracking the coverage of it?
- 23:37But, you know, multiprocessing, right?
- 23:39They're doing parallelism.
- 23:40Yeah, definitely.
- 23:41Yeah, that's good as well.
- 23:42Very nice.
- 23:44The other nice thing about it is just, so if you're, it makes it easy to have a text output.
- 23:49So normally when you run coverage outside of, like if you run pytest from coverage, once
- 23:55you're finished, you have to then tell coverage to give you the reports.
- 23:59You have to run coverage report to get your coverage report.
- 24:02But pytest cov just prints that at the end.
- 24:05So you get that automatically.
- 24:06Don't have to run it again.
- 24:08Cool.
- 24:08Very nice.
- 24:08Very nice.
- 24:09Would you say it's low stress?
- 24:11It is low stress, yes.
- 24:12Unless the stress is trying to get 100% coverage.
- 24:16Well, that's true.
- 24:17Actually, what are your thoughts on, I'll share my thoughts with you as well, but, you know,
- 24:22a lot of people feel like they're not testing well if they don't have 100% coverage or they
- 24:27don't have any coverage.
- 24:27So they don't even know like how much of their code is being run by tests.
- 24:31Like what are your thoughts there?
- 24:32I am a strong proponent for open source projects to have 100% coverage because it eases new
- 24:39people, new developers into the project.
- 24:41When they add code, it's easy for them to find the code that they added that isn't being covered
- 24:45yet.
- 24:45It doesn't necessarily mean that your tests are good.
- 24:48It just means that their code's covered.
- 24:51For open source stuff, it's good.
- 24:52And then if you take that argument, then it's good for internal projects as well because it's
- 25:00easier to run stuff.
- 25:01However, I'm not a zealot about it.
- 25:03I mean, if you're like jumping into a legacy project and adding software tests to a project
- 25:08and you're testing the new stuff and the things that you change, you're never going to hit
- 25:13100% coverage of like the rest of the legacy code.
- 25:16And, you know.
- 25:17Yeah, I agree with that.
- 25:19I feel like a lot of people get hung up on like the last mile for applications, like for
- 25:26open source projects where it's like a library or something.
- 25:28Sure.
- 25:29It should totally all be there.
- 25:30But there's like in certain apps, there's just little edge cases.
- 25:34Like if I'm running on Windows, I got to do this other thing, but I'm only testing this
- 25:38on macOS or on Linux.
- 25:40Right.
- 25:41So like how much should I stress about trying to like get that little edge piece of code to
- 25:46run or like there's this.
- 25:47This one utility that only I run and a cron job to do this thing.
- 25:51I don't know.
- 25:52There's just all these little edge cases that can be like a whole lot of work to get to.
- 25:56But I feel like, you know, most of the time, you know, you should probably not stress too
- 26:01much about it.
- 26:01Yeah.
- 26:01So all those edge cases are the reasons why I think you should test those.
- 26:04Because.
- 26:06Okay.
- 26:07So we have ways to combine coverage reports.
- 26:09So you can use something like GitHub Actions or Travis or something to run your, run your
- 26:16code on multiple platforms and combine the coverage from the test runs from multiple platforms.
- 26:21So why not run them on multiple platforms?
- 26:24If that, if you have platform specific code, if you have a platform specific code, I think
- 26:30there's a reason is because some of your customers use it.
- 26:33And if it's different than what the developers use, I think you should test that code.
- 26:38Yeah, that's probably true.
- 26:39That's a good point.
- 26:39That's the only place it gets run before it gets deployed.
- 26:43All right.
- 26:44Well, my transition to the stress one, we got kind of sidetracked.
- 26:48But this next one is all about stress.
- 26:50Yeah.
- 26:50So I think we should actually just cover a couple of them.
- 26:53So the next two.
- 26:54Yeah.
- 26:54They're kind of a good pair.
- 26:55Yeah.
- 26:56Yeah.
- 26:56And pytest repeat.
- 26:57So pytest stress is a nice thing.
- 27:00So let's say you've got some code that you just run it like overnight.
- 27:03I know that I'm going to leave it like a mid.
- 27:06I want to, I know everybody's going to be out of the building by midnight.
- 27:09And then I want to just beat on the code and run it as many times as I can for eight hours.
- 27:16So pytest stress allows you to be able to run a set of time to say, hey, run this for eight hours or run it for a half an hour, run it for a certain time period.
- 27:24And that's really cool if that's where you're thinking.
- 27:27And then repeat.
- 27:28pytest repeat allows you to run your test suite or test functions or whatever a certain number of times.
- 27:34So I want to set it up for a thousand times.
- 27:37I bring these up together because I found pytest repeat first when I needed to.
- 27:41I had some tests that, a test that was failing once in a while and I just wanted to run it all night long.
- 27:47And so I just set it for a large number like 2000.
- 27:51I don't know.
- 27:51Like I just guessed.
- 27:53But really what I wanted it to do was run all night.
- 27:56And so pytest stress would have been better for that case.
- 27:59Oh yeah, that's a cool one.
- 28:00You know where I would see these as being useful?
- 28:03Sometimes I'm trying to do profiling, like CPU profiling to figure out or maybe even memory profiling.
- 28:09And the startup of getting the pytest or whatever it is up and running and all the stuff in place and then running a little bit of code and then tearing it all down.
- 28:19It's like gets dwarfed.
- 28:20You know, the actual bit you're trying to understand gets dwarfed by all that startup.
- 28:25But here you could just let it run for 10 minutes and then it'll be really focused on what it was slow at.
- 28:30Then that's where actually with profiling, I think that would be a good place to use the repeat.
- 28:34So you could say do a nice round number like 100 times or a thousand times because it's easy to divide by that.
- 28:40Yeah, yeah, that's a good point.
- 28:42Exactly.
- 28:42Nice.
- 28:43What's this next one?
- 28:44InstaFail.
- 28:45So if you had a whole bunch of like print statements and whatever in your code, it doesn't get shown normally with pytest.
- 28:51So pytest will capture the output by default.
- 28:55I mean, you can turn it off.
- 28:56But by default, it captures the output and any errors and failures and stuff.
- 29:00Those get captured also, like tracebacks and stuff.
- 29:03And then afterwards, so after the test is done, you get your report and then you get a lot of this output that you can help debug your test failures with.
- 29:15With long running tests, sometimes it'd be really nice if you really could see those errors while it's happening.
- 29:24So you can start looking at the code and start debugging right away.
- 29:26And that's where InstaFail will report your failures and errors while the test run is happening, even with output being captured.
- 29:34Nice.
- 29:35Is there a way with plugins where if I've installed InstaFail, but I only want to see it some of the time to like encode, say, you know, don't run InstaFail right now.
- 29:44But sometimes when I want it, let it run without pip uninstalling it.
- 29:48I'm not sure, actually.
- 29:50That would be sweet.
- 29:51Yeah.
- 29:52I don't know.
- 29:53It could be up to them individually, but it would be nice if maybe a command line or something.
- 29:57I don't know.
- 29:58Some way to say, actually, I want this other one turned on now, but not always.
- 30:01Well, I'm just looking at it.
- 30:02So it looks like even if it has it installed, it doesn't.
- 30:05This is one of those that it doesn't run automatically.
- 30:07You have to pass in the flag.
- 30:09I see.
- 30:09Okay.
- 30:09Cool, cool.
- 30:10To turn it on.
- 30:10Nice.
- 30:11Speaking of passing flags and whatnot, pytest metadata is the next one.
- 30:15Number six.
- 30:15Yeah.
- 30:16This is a kind of an obscure one.
- 30:17And this is often used with pytest HTML, which actually we're not covering.
- 30:22Maybe we should.
- 30:23But pytest InstaFail, or not InstaFail.
- 30:26Metadata.
- 30:28Metadata.
- 30:29Metadata allows you to either through, it gives you a couple of things.
- 30:33It has a command line flag where you can pass in extra information to go into your test report.
- 30:39And then also you can within, so like for instance, if you're running against a particular set of servers,
- 30:46you could have the server name getting listed in the report.
- 30:50The other thing is maybe the data that you really want to log, the extra metadata about a test run, is generated or like in real time is developed or gotten from within a fixture or something.
- 31:03So a fixture or a test can set this extra metadata and it gets output with your test data.
- 31:09Oh, cool.
- 31:10Like maybe what GitHub branch you were running on or what environment like staging or production testing or whatever.
- 31:17Yeah.
- 31:18Yeah.
- 31:19Yeah, cool.
- 31:19Or maybe adding, you know, what operating system.
- 31:22I mean, your operating system's already being logged.
- 31:24But if there's other information like, yeah, what branch, what version, what some of the extra specifics.
- 31:30Yeah.
- 31:31Yeah.
- 31:31That's cool.
- 31:32Yeah.
- 31:33That sounds useful.
- 31:33Like if you're saving the report and you're going to look at it later, like maybe continuous integration or something like that.
- 31:40Yeah.
- 31:40So like for instance, when I'm testing against electronic test equipment, we add the instrument that we're testing against.
- 31:49So that's just part of the log.
- 31:51This portion of Talk Python To Me is brought to you by Sentry.
- 31:56How would you like to remove a little stress from your life?
- 31:58Do you worry that users may be having difficulties or are encountering errors with your app right now?
- 32:04Would you even know it until they send that support email?
- 32:07How much better would it be to have the error details immediately sent to you, including the call stack and values of local variables, as well as the active user stored in the report?
- 32:17With Sentry, this is not only possible, it's simple and free.
- 32:21In fact, we use Sentry on all the Talk Python web properties.
- 32:25We've actually fixed a bug triggered by our user and had the upgrade ready to roll out as we got the support email.
- 32:31That was a great email to write back.
- 32:33We saw your error and have already rolled out the fix.
- 32:35Imagine their surprise.
- 32:37Surprise and delight your users today.
- 32:39Create your free account at talkpython.fm/sentry and track up to 5,000 errors a month across multiple projects for free.
- 32:47And if you use the code talkpython, all one word, it's good for two free months of Sentry's team plan, which will give you up to 20 times as many monthly events and some other features.
- 32:58So create that free account today.
- 33:02Yeah, I don't want to just be random.
- 33:04Not just random.
- 33:05Yeah.
- 33:06But sometimes you do.
- 33:07So one of the things that pytest will actually, I think I've intentionally forgotten the test order.
- 33:12I think it's alphabetical or just it's not alphabetical.
- 33:15It's like the order that the tests are in in the file.
- 33:19That's how the default order that things get run in.
- 33:22But you really don't want your test to be order dependent.
- 33:26So you want to be able to run any test in any order.
- 33:30And sometimes a good way to test that is with the pytest Randomly plugin.
- 33:35So it does a couple of things.
- 33:37One of the things you can do is it can reorder your tests so that they're running in a random order.
- 33:43But also you might be using random as a seed for some randomized information.
- 33:48Like say, like Faker, for instance, and other fake data generators use random seeds.
- 33:54So pytest Randomly can help you randomize that stuff so that it's more random.
- 33:59Yeah, that's cool.
- 34:00It says that if Faker, which is a really cool library, if Faker is installed, it'll automatically update it.
- 34:07So that Faker is different this time than another time.
- 34:10Same thing for NumPy.
- 34:11Yeah, this is really cool.
- 34:13Anytime you have your test running reliably in the same order, you could unintentionally pick up some form of dependency on order.
- 34:23Like if you call this test, it like initializes the database connection string.
- 34:27And then you can call this other thing and it'll work.
- 34:29But if you call it in the other order, it would crash.
- 34:30I mean, maybe that's a bad example because you really want to avoid that dependency.
- 34:34But you know what I mean?
- 34:35Like it could set something where the next thing depends on it being set that way.
- 34:39And if you randomly order it, then you're going to discover those.
- 34:42Yeah, especially with your scoping your fixtures to try to speed up test time, you might inadvertently have dependencies like system dependencies.
- 34:52For instance, you get the system in a particular state in one test and then you're running another test.
- 34:57One of the problems often is you got a test that runs fine when you're running it from your editor.
- 35:04And then you go into on the CI system where it's running everything.
- 35:09It fails.
- 35:10And you can consistently fail it with the entire suite, but not by itself.
- 35:14Those are just a pain in the rear to find out where the problem really is.
- 35:18And I mean, randomly can help you.
- 35:20I'm not sure how well it can help you debug that problem, but it can help find dependency.
- 35:25Yeah, at least it won't leave it covered up, hidden.
- 35:29Yeah.
- 35:29Yeah.
- 35:29Well, you already mentioned exodist earlier when we talked about pytestCov.
- 35:36So what's this exodist?
- 35:38Well, exodist is super powerful.
- 35:40It's a little bit of a grab bag of a few features.
- 35:43One of the things that it does, and people usually grab it for this, is to run tests in parallel.
- 35:49So you can tell it how many CPUs to run on and it'll just run a whole bunch of tests in parallel.
- 35:54And that's pretty cool.
- 35:56It can speed up tests.
- 35:57It does have a little bit of startup time because it's communicating with the different, you know,
- 36:03it's doing multi-process communications, a little bit of startup time.
- 36:06So if you have independent tests that don't share a resource, this can be really cool.
- 36:11The other thing that you can also just offload it onto different processes or different computers,
- 36:16you can have it be running on different machines as well.
- 36:19So that's pretty cool.
- 36:20That's cool.
- 36:20That's really cool.
- 36:21Yeah, that's very fascinating.
- 36:22And then I don't know why this is bundled into it, but this is a really cool feature of pytest
- 36:27exodist is the loop on fail feature.
- 36:29So you can pass it loop on fail and it'll run your tests repeatedly in a sub process.
- 36:35And then you get to see what the failures are.
- 36:39But if it detects you've changed your project at all, it'll rerun the test, but it just reruns the failing tests.
- 36:46So it just keeps rerunning all of your failures while you're trying to fix it.
- 36:50And then once all of them are, when everything's running and fixed, it'll rerun everything.
- 36:57So when they all pass, it'll rerun the whole suite.
- 37:00Yeah, super cool.
- 37:00So it's a little bit like in PyCharm where you can say, run the test when anything changes,
- 37:07but this only does it for the failure.
- 37:08So you're like, you got to, you write a failing test or you have a failing test you're trying to fix and you can just work on it.
- 37:13And it only focuses on that thing.
- 37:15Yeah.
- 37:16I mean, it kind of is all the passing ones too, because if your suite is passing, it just keeps rerunning it every time it sees a change.
- 37:22I see.
- 37:22Okay.
- 37:22Yeah.
- 37:23Cool.
- 37:24All right.
- 37:24Well, one way your code can fail is it doesn't execute properly, makes a mistake.
- 37:31The other one is it could just not be formatted the way you like or break some of the idioms that you're supposed to write.
- 37:37Yeah.
- 37:38There's a whole bunch of extra plugins that are like this, but they're style guide stuff.
- 37:42So pytest Flakes is one where it can run Flake 8 and other things against your code as part of the pytest run.
- 37:51It can test both your source code and your test code if you wanted to.
- 37:54I actually think this is a pretty cool idea, but there is some controversy as to whether this should be really part of,
- 38:00your pytest run or it should be part of like, say, do a pre-commit or something.
- 38:06But I think having it in both places is fine.
- 38:09If your workflow is mostly people run pytest to make sure all their code's working and then they're ready to check it in.
- 38:17If you want to make sure you don't, it's just your workflow, whether you like to catch your Flake errors during commit time or during test time.
- 38:26Yeah.
- 38:26And I just noticed the one that I grabbed to throw into the list, there was pytest Flakes, I think.
- 38:32And they say you should use pytest dash Flake 8.
- 38:35So I'm going to link to that one.
- 38:37But same idea.
- 38:38Yeah.
- 38:38Yeah.
- 38:39There's also a pytest PEP 8, I think it is.
- 38:43There's like various different ones for this.
- 38:46Yeah.
- 38:46And I got to admit, I'm like a little, I guess, over the top.
- 38:48I use both pytest Flake 8 and hookup Flake 8 to pre-commit hook.
- 38:53So just because, I mean, pre-commit is so fast.
- 38:56Why not?
- 38:57Yeah, absolutely.
- 38:58Cool.
- 38:59Cool.
- 38:59Speaking of being fast, you know, your tests sometimes are slow because it's a lot of work to run your tests.
- 39:04Or they could just be stuck waiting on something.
- 39:08Yeah, I really like, so pytest Timeout is a plugin that is a fairly big hammer to a problem.
- 39:15But you've got tests that maybe it loops forever.
- 39:19You forgot to put a timer on something.
- 39:21Or it's stuck on a resource.
- 39:22Or for any reason, it's hung.
- 39:25And I suggest people use pytest Timeout for any long-running suites all the time.
- 39:30For really short, fast tests, there is a little bit of overhead.
- 39:35But for any sort of long-running stuff, the overhead is negligible and you don't see it.
- 39:40But what it does is it runs your tests in another thread and then is able to kill it.
- 39:46So it will just put a timer around it and kill the thread if it doesn't come back in time.
- 39:52Why I call it a big hammer is because if it fails, you don't get your test output.
- 39:57But it also doesn't hang forever.
- 39:59And these are especially good for CI systems.
- 40:03Because you really don't want to come back in the morning and notice that your test suite has been running for 12 hours.
- 40:08And it's only really supposed to run for 20 minutes.
- 40:11Yeah, absolutely.
- 40:12Yeah, if it's unattended or it's really long-running or you're talking to something that who knows how long it's going to take to respond.
- 40:19Yeah, maybe even have that with instruments that you're testing and whatnot.
- 40:23Yeah, we definitely use it.
- 40:24And the nice thing is you can have a global timeout to say, hey, no tests should run longer than this long.
- 40:31So whatever, 10 minutes.
- 40:33Whatever is in your system is a reasonable amount of time.
- 40:35And then you can override that within.
- 40:38We can put decorators on individual tests to say, well, this test is really fast.
- 40:43It really should only be this long.
- 40:44And whatever, you can fine-tune it if you need to.
- 40:47Nice.
- 40:47All right, another one.
- 40:48I think I threw this one on the list is spec.
- 40:50Yeah, I don't know anything about this.
- 40:53Yeah, so spec, it's a little bit like sugar.
- 40:55It allows you to sort of control the output.
- 40:58So you can group tests by classes or by files.
- 41:02The failed, passed, and skipped ones are marked and colored.
- 41:06You can remove like test underscore and underscore stuff from the test.
- 41:11So it'll like, you might have a name where it's like test user can log in with underscores.
- 41:17And I think it'll take those out so it looks more like English, like user can log in or something like that.
- 41:22So it's just a nice little one for customizing.
- 41:25There's some options you can pass to it and whatnot.
- 41:27Cool.
- 41:28Yeah, yeah.
- 41:28I've heard you talk about pytest Picked before.
- 41:31Yeah, that's a fun little plugin.
- 41:33So do you remember what this does?
- 41:36Yes.
- 41:36Yeah, so this is the one.
- 41:38And we weren't totally sure like how smart it was.
- 41:41We had a bit of a conversation about it.
- 41:43But this one will run tests that are not checked into GitHub or have changes that are not checked into GitHub.
- 41:51Oh, that's nice.
- 41:52Yeah, so it'll run tests related to unstaged files.
- 41:57And if it sees some that are, you know, there, it'll basically, if you're in a GitHub repo, you can run tests that are modified, but not checked in, not committed.
- 42:07Yeah, and that's usually often what you want to be doing, right?
- 42:09Exactly.
- 42:10What I would really like it to do, and I don't think it does, and people can add a comment at the bottom of the show page if that's changed or still misunderstood.
- 42:21But I would love it to somehow use coverage to see what are the files that need to be retested.
- 42:29Like what tests touch, like I've got a non-test file I change.
- 42:34What tests need to be rerun?
- 42:35I think it only just reruns the tests that are changed.
- 42:39I don't think it uses coverage, but I could be wrong about that.
- 42:41But nonetheless, it's still pretty cool.
- 42:43Yeah, I mean, all of the pieces are together for somebody to do something like this.
- 42:47Yeah.
- 42:47Because coverage has a context feature now, so you can tell it.
- 42:52And with pytestCov, it automatically sets it up so that you can tell which tests touched which parts of your code.
- 43:00And the idea seems like great.
- 43:01One of the issues with that is there's a whole bunch of your code that gets hit by every test.
- 43:07Yeah.
- 43:07Because it's like startup code or something like that.
- 43:10Right, right.
- 43:11Well, you just don't change the startup code.
- 43:12You can't edit that part.
- 43:13Yeah.
- 43:14No, but still, it is a really cool idea to say like just run the stuff that might have had some influence on.
- 43:24That's really cool.
- 43:24Yeah.
- 43:24So pytestPick doesn't do that, but it has this.
- 43:28It does a really cool, it's a nice addition to be able to run the tests that you're working on.
- 43:33Yeah, I could totally see just running this and then using like a pre-commit hook to run everything or just, you know, right before you check in, just run everything.
- 43:40Or even just check it in and it runs in CI and doesn't get merged if it fails, right?
- 43:44Like if there's some other gated mechanism further down, then this seems pretty cool.
- 43:48Yeah, definitely.
- 43:49Yeah.
- 43:49So this next one, FreezeGun, is all about time.
- 43:53And this one I feel like is probably packaged as a fixture.
- 43:56At least it has a fixture concept.
- 43:59Definitely as a fixture.
- 44:00It doesn't run by default, but you have to kind of turn it on when you know it.
- 44:04Yeah, this one, people know it once they hear what it does.
- 44:07You don't want this to run by default.
- 44:08I like to list this one, but just, it's such a great name, FreezeGun.
- 44:12Oh, yeah.
- 44:13It's for changing time.
- 44:15You can easily, there's fixtures around it so that you can easily either freeze time so that you're, anytime you grab the time of day or the date time stuff, it's always going to be the same.
- 44:24Or you can, you know, fast forward or change the date or do things like that.
- 44:29So one of the hard things is around testing code that deals with time because you have to know what the answer is going to be.
- 44:36And it's going to be different every time.
- 44:38So FreezeGun allows you to test time-related tests really easily.
- 44:42Plus, it's just such a great name.
- 44:44It is.
- 44:45And you have these functions, they can take a freezer object and then basically that, what is that probably behind the scenes patch, like the time functions, date time.now and whatnot.
- 44:54And then you can also do like freezer, move to some other time.
- 44:58And then like instantly you are transformed either into the future or the past.
- 45:02Yeah.
- 45:02And I think some people might be using mock for that sort of thing in the past, but this is cleaner.
- 45:08Use this.
- 45:09Look, if you could say, let's mock out date time.date time.now, or you could use FreezeGun.
- 45:13Like, which do you want to do?
- 45:14Come on.
- 45:14Exactly.
- 45:15Yeah.
- 45:15FreezeGun.
- 45:16All right.
- 45:17We've got one more that's on your list and then an extra one I threw in at the end.
- 45:20pytest-check.
- 45:21Yeah.
- 45:21I wrote a plugin called pytest-check and solved a problem I had.
- 45:26So the problem is the normal way to stop or to fail a test is to use assert or raise an exception.
- 45:34Both of those stop the execution.
- 45:36I wanted to be able to do lots of things.
- 45:39Like if I want to test lots of attributes of some object, I want to be able to have a lot of things.
- 45:44I want to have lots of checks in there.
- 45:45And I want to see all of the failures, not just the first one.
- 45:48So pytest-check solves this by allowing multiple failures per test with these little check functions.
- 45:54But you can also, it also provides a context manager so that you can say within this context, you know, you can use normal asserts, but within a context that will continue after that context.
- 46:06So a similar solution is available called subtests and with the pytest SubTest plugin.
- 46:12But I still like pytest-check better because it works with stop on fail and X fail, whereas subtests do not.
- 46:18Okay.
- 46:18Yeah, this is really cool.
- 46:19So you can pass in a check fixture to your test and you can say check greater AB less than or equal to this.
- 46:28This is not in that.
- 46:30This is not in that.
- 46:30And then it'll tell you like it was not greater and it's not in there as the error.
- 46:35It's just something like that, right?
- 46:36It's a fairly simple thing.
- 46:38It's a fixture that just collects failure.
- 46:40It like wraps things right with an assert context manager or a try except block.
- 46:45And if a failure happens, it just, instead of stopping the test, it records it into a list.
- 46:50And then after the test is complete, it'll like kind of hack with the pytest guts to make it fail.
- 46:57Okay.
- 46:57That's really cool.
- 46:58I definitely like this.
- 46:59I'm thinking like if I get just the text printout of the build failed or the CI failed, you know, even, you know, that I just see it.
- 47:08And then when I'm running it and like PyCharm or something, I'd like to just look at it and go, oh, it failed because if this is true and that's true and this,
- 47:15like I see, right, you don't have to go and actually debug through it or be, it might be that there's enough information here to actually know what's going on and just fix it.
- 47:24Yep.
- 47:24And also just more information is good sometimes dealing with, I mean, I definitely use it a lot with test equipment, with signal processing and stuff.
- 47:31A signal might be wrong, but why is the signal wrong?
- 47:34It might be the wrong frequency.
- 47:36It might be the wrong bandwidth.
- 47:37It might be, I mean, lots of different things.
- 47:39And if I'm checking four or five attributes of the signal, I really want all of that information for a failure.
- 47:46Yeah, I agree.
- 47:46There might be different aspects that all come together to make it successful.
- 47:50So report all the aspects.
- 47:51Yeah.
- 47:51That's cool.
- 47:52All right.
- 47:53I want to throw one more in that's not really part of pytest per se, but absolutely can be used with pytest because, hey, it raises exceptions.
- 48:01And that's called fluent check.
- 48:03So this one allows you to use a, well, a fluent API, you know, where you say operation one dot operation two dot operation three to have a more English language thing.
- 48:14And to me, it feels a little bit like your check as well, but it's not a pytest plugin or fixture.
- 48:19So it has a couple of APIs.
- 48:22I like the is API.
- 48:23So you can say like is in dot not none dot float dot between zero and one, and it'll actually check all those things and report like nice messages.
- 48:33Like if it's not a float, it would say, you know, whatever it is, here's its value is not a float and you expected it to be right.
- 48:41So a little more readable.
- 48:43Some people like it.
- 48:44Some people just want to assert stuff and either one's fine.
- 48:47But if you like the style, this is a pretty cool library.
- 48:49It's not mine, but I did do some PRs.
- 48:52I did basically create the is API for it.
- 48:54Cool.
- 48:54Does it have not a hot dog?
- 48:56It should also have is a teapot or something like that from the for web testing, you know, the four for 18.
- 49:02Yeah, we should throw some of those in there.
- 49:05Like, why not?
- 49:05We do it.
- 49:06Anyway, those are kind of fun for just like inside your test expressing what you're testing a little bit better.
- 49:12I think check is really cool as well.
- 49:14And it's a bit similar.
- 49:15Yeah, but this one doesn't stop.
- 49:17It just throws the first one.
- 49:18All right.
- 49:19Well, that's a great, a great list.
- 49:21You know, maybe just really quick, we could check in.
- 49:23How's your book doing?
- 49:24You wrote a book about some of the stuff.
- 49:26Yeah.
- 49:26So I wrote Python testing with pytest.
- 49:28That was gosh, it was it's been a couple of years now that it's been out and it's still going going strong.
- 49:36The of course, the big chunk of sales right after the book was released and quite a few during beta too.
- 49:43And that was neat.
- 49:44But it's still selling really well.
- 49:47And I'm really happy with it.
- 49:48And it's still valid, even though it's it's a couple of years old now.
- 49:52And I wrote it in conjunction with talking with a lot of the pytest core maintainers to make sure I wasn't putting anything in there that would be deprecated soon.
- 50:01Yeah.
- 50:02So it's still valid.
- 50:04It's good.
- 50:05Awesome.
- 50:05Yeah.
- 50:05People can take that up.
- 50:06They want to read, you know, dive into this much more.
- 50:08And then also you did an episode that also talked about pytest plugins over on testing code.
- 50:13Yeah.
- 50:14It was episode 104 with Anthony Sotil.
- 50:17The episode that you and I just did, we kind of picked our own, some of our favorites, some of the things that we think people should know about.
- 50:23What we did with episode 104, we took download counts and took the top some of the top 28 downloads.
- 50:30I see.
- 50:30Popular by usage, not by curation.
- 50:33Yeah.
- 50:33Yep.
- 50:34Exactly.
- 50:34Awesome.
- 50:35All right.
- 50:36Well, this is really fun, Brian.
- 50:37But before we get out of here, two final questions as per usual.
- 50:40If you're going to write some Python code, what editor do you use?
- 50:43PyCharm.
- 50:43All right.
- 50:44Right on.
- 50:44It's got some sweet built-in test runners.
- 50:46Yeah.
- 50:46It's really clean with testing.
- 50:47Yep.
- 50:48And then notable PyPI package.
- 50:50I mean, I guess many of these are.
- 50:52Anything in particular comes to mind you want to throw out there?
- 50:55It's something different.
- 50:56I'm kind of a huge fan of Flit for packaging.
- 51:00So that's the easiest way to package right now, I think.
- 51:04All right.
- 51:04As opposed to some of the other alternatives like pip and PMP and setup tools and all those
- 51:12things, right?
- 51:13Yeah.
- 51:14Yeah.
- 51:14Sweet.
- 51:14All right.
- 51:15Yeah.
- 51:15What is that?
- 51:16There's poetry and...
- 51:17Yeah.
- 51:17Poetry as well.
- 51:18That's right.
- 51:18All right.
- 51:20Well, final call to action.
- 51:21People are excited about pytest and pytest plugins.
- 51:23What should they do?
- 51:24They should buy my book, actually.
- 51:26So one of the things I made sure to do is to tell people how to create their own plugins
- 51:31within the book.
- 51:32So I think it's a really good introduction to that.
- 51:36Also, I guess, final call to action, go subscribe to Testing Code and podcast also and test your
- 51:43code.
- 51:43Go out and test something.
- 51:44Do you guys talk about pytests over there sometimes?
- 51:46Sometimes.
- 51:47Yeah.
- 51:47A lot of times we do.
- 51:48Yeah.
- 51:49That's great.
- 51:50Awesome.
- 51:51Cool.
- 51:52Well, thanks for being on the show.
- 51:53Nice to have you back as always.
- 51:55Yeah.
- 51:55Thank you.
- 51:55You bet.
- 51:55Bye.
- 51:56Bye.
- 51:56This has been another episode of Talk Python To Me.
- 51:59Our guest on this episode was Brian Okken, and it's been brought to you by Linode and Sentry.
- 52:05Start your next Python project on Linode's state-of-the-art cloud service.
- 52:10Just visit talkpython.fm/Linode, L-I-N-O-D-E.
- 52:14You'll automatically get a $20 credit when you create a new account.
- 52:17Take some stress out of your life.
- 52:20Get notified immediately about errors in your web applications with Sentry.
- 52:25Just visit talkpython.fm/sentry and get started for free.
- 52:29Want to level up your Python?
- 52:31If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 52:36Or if you're looking for something more advanced, check out our new async course that digs into
- 52:41all the different types of async programming you can do in Python.
- 52:44And of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
- 52:48It's like a subscription that never expires.
- 52:50Be sure to subscribe to the show.
- 52:52Open your favorite podcatcher and search for Python.
- 52:55We should be right at the top.
- 52:56You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 53:01and the direct RSS feed at /rss on talkpython.fm.
- 53:06This is your host, Michael Kennedy.
- 53:07Thanks so much for listening.
- 53:09I really appreciate it.
- 53:10Now get out there and write some Python code.
- 53:12I'll see you next time.