Transcript#

This transcript was generated automatically and may contain errors.

And today we're joined by our lab manager, Rich Iannone. And you know Rich because he made the GT package and the point blank package and a bunch of other fun stuff like Blastula. And he also happens to make Python packages. And that means that he knows a little bit about testing in Python. Rich, would you like to say hello?

Hi, thanks for joining. And you're gonna learn a lot today.

I mean, I'm gonna learn a lot because I am not a great tester in general, as I have admitted over and over again. But I have no shame, I love to learn. So Rich is going to take us through just a little bit about testing in Python, what pytest is, because that's the package that he is going to use, the library that he's going to use. And this is a place to ask questions. So the Discord is a place to ask questions. If you are like, what did Rich just click? What does that mean? I don't understand this concept. This is the place to stop us and ask questions and we can have discussions about it. It's not a presentation where you cannot interrupt us at all. So I'm gonna have Rich share his screen and go ahead and take us away. Yeah, and we'll ask questions throughout.

Okay. And I don't know if anybody can hear my cat in the background. I'm gonna try to make sure that she's not screaming. I'm gonna go on mute for a second.

Introducing the example package

Great. Okay, so I'm gonna start just by showing you, well, a tab in my browser. I've got like a new, like, sort of like example package plus explainer. And part of it deals with tests. So I'm gonna make this a little bit larger so you can just sort of see it better. It's called example Python package. It's in my own, like, GitHub repo. And what's it about? Basically, it's a small package I'm going to demonstrate today. I call it tiny tab. It's a fully functional package. Very, very small, very minimal. The whole idea is just to, like, understand how, like, a package in Python is set up.

So I have my own opinions and I just, like, went with my own. And basically, this whole repo is, like, how I would set up a package and how I would do tests. So the key information is actually inside of tests. I'm gonna run through this inside of, like, VS code, by the way. And basically, these are the test files. I'm gonna run through these, like, basically, like, what you do with them, how you make them, how they're detected. I'll get into the nitty gritty as well. But the idea is just to get really comfortable with the very basics of PyTest. And I'm assuming a lot of people are coming from R and testthat. I'm gonna use that as, like, a bridge to sort of learn about PyTest. There's lots of similarities, but there's some differences as well.

So I'm gonna get through all that stuff. Oh, also, because as a side note, I make a Python package called great-docs. And this actually is a documentation site. The reason I mention this is because there's a tab above Python packaging. And within that, there's this testing tab. And it has all sorts of good information you can use that will be, like, good stuff for, like, after this DS lab. So tons of stuff on testing. Maybe not enough, but, like, I think it's just enough. And so let's get to the actual, like, package here.

Running tests with a Makefile

It's opened up here in my editor. And what I do sometimes is I just, like, run the little test inside of QMD because I love Quarto so much. And I run it instead of a notebook. And I should use notebook. But I don't. I just use Quarto. And I just run these cells, and I can sort of get an idea about how the package works. And I can just run little functions inside my package. This is great. Because, like, when you're testing, you'll probably want to, like, run stuff at the same time. See, you know, like, what arguments seem to test, whether it works. This is kind of, like, you know, like the ground truth, I guess you can say.

So I'm just running a few cells. This is all very nice. And this is kind of what you would do inside testing. You would actually just invoke the main functions, maybe some other stuff, like the private functions as well and things like that. But mostly it's this. And I'm going to show you how tests work. So we have this folder called tests. It's a bit big. Don't worry about this stuff for now. I'll get into that. The main thing is you'll see right away all the test files that we care about begin with test underscore.

And another thing is that it's under a folder called tests. That's pretty useful. This is, like, a default thing. You can change these, but it's good to align with the defaults.

Okay. Make files. So one thing I do all the time when I do any Python project is make a make file. I'm going to show that to you right now. So it's basically this right here.

Okay. Also tons and tons of comments. This will be useful for you. So what do we do here? So basically in Python, instead of using, like, things like key binds to run tests, I just use make and some sort of command. So this is like a target. So if you just type in make, because the way this is set up, it shows you all the available targets. And what that means is just, like, commands. So a useful one for us is make test. So that's how it works. Make, then target, and then it's pretty much make test. I'm going to run that. Make test.

So this is, like, aliasing a terminal command. Exactly. Like, I don't want to remember to type all of that every single time. I'm going to make a little file that's full of little aliases. And it can remember what all the typing that I need to do in the terminal is. Yeah. Because can you imagine just remembering typing all this stuff out? Like, this is, like, that's a lot.

And I never do. And I have, like, a whole list on my wall taped to my wall with git commands.

Totally get it. Look at that super long one down there for test coverage. It's a lot. So useful, though. But this is much better. I'm going to actually run that, because, you know, why not? Make test hyphen coverage.

Okay. And all this adds is, like, a little bit of, like, coverage information. I think this one has it, too. I don't know why it's different. But whatever.

But, yeah. So, basically, like, I'm going to clear my console as well. I'll run it one more time to show you what coverage is, just real fast. Basically, it's showing, like, different files. Okay. Let's just use make, because it seems to have, like, make test. Has the same sort of stuff.

Again, as you can see, it's actually really fast to run these tests. But still, I'm always surprised how fast PyTest really is. Each one of these dots is essentially a different test. And what is a test? Okay. Let's go into that right now.

Anatomy of a test

I'll go into test formats. And basically, a test is when you scroll down to it, it's basically a function. And you give it a name. It doesn't have to have arguments. Usually it's just like this. And then you put your test in here, like, inside. And they always have assert or assert not.

And basically, that is the key thing. If you're coming from something like testthat, there's all sorts of, like, test functions. Like, you know, like, they have, you know, greater than, less than. There's many varieties. But in PyTest, it's always just assert and then, like, you do something. And it just expects a true or false in the end. Okay. So, all we're saying here is, like, this format number, this is, like, an invocation of, like, this top level function. I can sort of see it right here. It's quite handy. It's going to end up being a format string that looks like this. And all the tests pass. So, this is actually true. So, this equals this side.

But in PyTest, it's always just assert and then, like, you do something. And it just expects a true or false in the end.

What we do also in tests is we sometimes give the test functions very long names. Like, you probably wouldn't do this inside your actual, like, package code. But for tests, it's fine. Because we're going to have lots and lots of tests. And being descriptive is actually super useful. Because if you ever get, like, a failing test, this is, like, the thing that appears. Like, this name. So, you want to have a good sort of, like, description. Because you probably don't remember writing the test at all. So, this is very useful. And also, another salient thing is that you can have multiple tests under each of these functions. So, a function is kind of like a group and an identifier. Right? It needs this. You can't just have an assert, like, up here, like, outside of a function. Got to have those functions.

Yeah. So, here's another one. Same test format currency defaults to USD. And there's no defaults. Like, basically, these are the defaults. And we sort of see that dollar sign. So, that's a very descriptive way to describe a test.

So, I'm going to run this one more time. All right. While you're running it, we have a detour. Because we are all very focused on what your theme is. Nora wants to know what your theme is. Several people did. Okay. I just, I think, oh, my god. Let me find out. Because I got it a long time ago. And it just looked Python. Because of the green. I feel like Isabella and I figured out a quick way to do it one time in the command palette. Yeah. I think it's monokai charcoal green. Perfect. Yeah. It's a great one. And one day I'm going to have, like, a way to, like, you know, like, our projects being blue. And then, like, Python ones being green. Haven't figured that one out yet. Just to have a sense of, like, place. But this is pretty good.

Another thing I do, just while we're getting into, like, themes and stuff, I always put, like, a bar here for, like, 100 lines. Sorry, 100 characters. Just a little guideline. Just a thing I do. I feel like I keep mine at 80 because of RStudio. I used to. Yeah, 80. But then I thought that was too constricting. Especially these tabs in Python, like, being quite wide. So, I was, like, it's got to be 100 now.

I also put a poll in Discord and Isabella put one in Zoom that asked, have you ever used an insert statement in Python? And we have about 75% saying yes, but very low voting in the Discord. We only have eight votes so far. So, we, yeah, we need to up it on Discord for sure. But there's more nos than yeses on the Zoom side of the poll.

Okay. So, I'm hoping that even if you haven't used it, like, this is very instructive. It's basically just, like, if this is false in the end, then this fails the test. If it's true, then you get nothing. Basically, it passes and there's no problem. So, that's kind of the same as and testthat. Is only a warning or stop if there's a problem or a report of some sort of failure.

Seeing a test failure

I'm going to show you when this fails. I'm going to hit save. And then, you know, that works. My test is right up here. I'm going to run it. I put my terminal on the side because it was weird having it below. I wanted to see. I know it's kind of small, but I don't know what to do about that. Okay. So, my test. Oh, okay, great. We see some red. We see, okay, a few things. We see right away that there's a failure inside of test formats. This is just like a little map. So, it's really hard to know what. You just know it's a failure in that file. And then, things start to turn red. And then, we see the actual failures. We only have one because we changed this to have no dollar sign.

Okay. So, this is actually kind of hard to read. Every time I look at this, especially this part, I'm confused which side is which. But it's more obvious when you know what the failure is. But here, you know, we put in like this, but it should be this. If you read it, it's not that bad. It asserts that it's this, but it actually equals this. It's kind of weird, but it's fine. But we know exactly where it is. It's like in this exact place. And it's this one right here. So, interesting, right?

Okay. So, I'm going to like backtrack on that and save. I'm going to run that again. Everything turns green.

So, another thing that happens is you have multiple tests inside of a function. Okay. What happens if one of these is wrong? Maybe remove a comma. These have to be very exact. They're just pieces of text. Okay. I'm going to run it now. Great. Okay. So, we intentionally, of course, introduced an error here in the test and stopped here in rounds to request the decimals. This one right here. Okay. Interesting. You might get a situation where, okay, that's fine, but you remove this one. Okay. One more time. Okay. Another failure. It's in here. See, it shows now. It stops at the first failure. This one, it shows that it's fine. And then it stops here. So, that can be kind of a problem. If you have too many things in here, it could get kind of unwieldy, right? If you have. Oh, if you stack too many asserts.

Yeah, exactly. Okay. Yeah. And sometimes, of course, the description may not fit all the cases. I mean, request the decimals makes sense. You're just changing this a few times. So, I guess my bit of advice is don't put too many things in each one. Otherwise, you're going to have to, like, if there's a problem, you're going to have to figure out which one. Usually this is pretty good, but sometimes it can get pretty big, and if you have lots of failures, it can get kind of hard to read.

So, my advice is maybe not. Don't put too many things inside each function.

Using parametrize for multiple cases

And there's also a thing for that. And this is actually kind of cool. It's called parameterize. So, it's basically what I wrote here. One test function, many cases. So, it's kind of, like, really confusing at first, but I accept that because it's so incredibly useful.

What you do is you actually put in, like, essentially a map of different values. In this case, I'm just assigning x to these tuples. The first value in each tuple. And there's a list of tuples here. And basically, this one is x. Like, the value that goes into the function. Expected is kind of, like, the output. You can sort of see that because it's a string. So, it's a little bit easier to see here. But when this becomes long, you have to make sure that each of these, you know, has the right number of, like, members in each tuple. But it's usually fine. This is a bit weird, where you have, like, this formatted as a string. I just accept it.

So, x maps to, you have to put them in your signature. Now, this is, like, a case where you have something inside your function. You have x, which is right here. And then you have expected, which is right here. And all of these. And then you use these to make a statement with

a number of different x's going down the list. And a number of expectation values down the list again. Well. So, you have a function that takes two arguments. One is x and one is expected.

Named arguments. And what you're doing is you're just creating a suite that it's going to go through and it's going to be, like, take these arguments and plug them in each time. Yes, exactly. And the key thing, I did mention this. You have to use this decorator, pytest mark. This is basically a submodule. Parametrize. Parametrize. The way they spell it is kind of a little strange. But it's fine. That's what you use. And that just says, like, use all these values. This, well, these IDs and then these values. And then you can, like, craft, like, a better expectation, which maps over a number of things. And it gets even wilder.

You can do it, like, twice to do, like, a cross mapping. Like, advanced stuff down here. But I'm going to show you the basics first. So, I'm going to run this. And what you see also when you run it, it's just fine. So, make test. Everything just runs. And you can't really see it in here. Let's introduce an error. So, I'm going to say, like, this one is missing a comma.

Save it. I have a question. Yeah, go. We have two questions. When you run make test. Yeah. And it's running a whole thing. Is it running this entire file, test underscore formats.py? It's running them all.

Every single test underscore file in the test folder. Exactly. It runs test formats, all the tests in these files as well. Okay. And I'll get to comp test, what that is. That's basically just, like, setup.

Okay. And Isabella had asked, when you run the individual test, are you using command enter to do that?

You can use, like, ID affordances like this. You don't really run them, like, separately, I don't think. I've never done it that way. But there's ways. I have a little cheat sheet that I'll provide at the end. You can run individual files. You can even search for, like, an ID, which is basically this, and run just that. And you can do things, like, there's so many things you can do. It's pretty wild. You can even run, like, last failing tests. So if you, like, want to correct something, and you just want the last thing that failed, you can do that as well. Okay. Yeah.

All right. And we did have a question about Jupyter Notebooks versus Quarto versus Marimo, but I'm going to hold it to the end. Because I think that we can hold on to that one. So much ground to cover. Don't worry, y'all. We got to save that. So I introduced an error here. We're missing a comma. I saved it, I believe. Yeah, I'm going to run make test once more.

And this is kind of cool, because we see the failure. It occurs somewhere in test formats, the file we're on. And we see here, yeah, from this input value, which is x, so it's repeating the x, and expected, that's, like, the variable we have here. We expected this. Of course, this is wrong.

But the cool thing is, what is the cool thing? If you look down here in this little printout, I know it's very small, you see that there's an ID here, like test formats, then colon colon, then the function name. But it also introduces, like, this little bit right here. This is basically, like, the values that failed right here. It's sort of, like, you know, formatted, you know, in the way that it does. But usually there's, like, value one, hyphen, then value two, which is basically this. So it's kind of cool. You can sort of see what went wrong. So you're not just looking at, like, oh, this whole thing failed. Which one of these? I don't know. We can sort of see it. It sort of created, like, this little, like, indicator of what that is. So that's nice.

But even this might be a little bit unwieldy, if you have, like, lots of these values, and it just gets formatted kind of strangely. So a thing you can do, I'm going to just undo this, is you can actually introduce, like, ID values. Like right here, IDs. Okay, so you can say, like, for this first one, it's basically a list. The argument here is IDs. And the first one is USD. The second one here is got an ID of year. This one here is lowercase code. Maybe not the grade, but whatever. It has an ID. It's descriptive, it's okay. It's enough, yeah. It's enough to get at the cases. Yeah, exactly.

So I'm going to run this, and I'm going to, like, make some sort of error right here. Maybe not that one, because this one right here. Okay. I just want one of them to fail, so it thinks that I've been printed out, right? Exactly, exactly. And we see here, right inside this part right here, the little summary. Test for my currency. Ah, we see the ID value that we set. Not, like, just a set of, like, values. Which is much more helpful. Yeah, it can help you narrow it down, especially if you have lots of these.

Okay, so that's super good. And, but there's, like, you know, a million ways to do things. So there's even more things you can do on this regard.

Using pytest.param for self-contained IDs

You can also, like, imagine you want to reorder it for some reason. Okay, now these are out of sync. So what's the solution to that? You provide the IDs with the tuples. The unfortunate bit is you have to introduce this, like, this sort of thing. Pytest param. But maybe it's worth it. Because now you can do this, and they're self-contained. And it's no problem to, like, do something like that.

So now if I put them like this, for instance, and run make test. I'm doing make test a lot. So that's, like, what you do. It's like, it's like command shift T in Python land. Yeah, see the problem here. And we see it's because of test format percent zero right here. Being the problem. So the idea is just right there to sort of embed it with the values.

Okay, now, quickly, I'm getting back to my mistakes. I just undid. That is great. All right, I'll pause you. Because I can tell we're going to get a question on this the moment you did it. Can you talk about the keyboard shortcuts that you use to highlight a row and reorder it? So where you, like, grab line 115. You're like, we're going to move this down to two under where it was. Just how I did it.

I use alt and then the arrows up and down. And you do the same thing in RStudio. And of course, in Positron. Yeah, it's really great. The other thing that Rich did was he grabbed pytest.param. And he grabbed it just those first, like, 10 characters all the way down without grabbing the rest of the lines. And I forget which shortcut that is. But it's also very useful. Yeah. Oh, do you mean this one here? Yes. Yeah. Again, with alt. Yeah. You just, like, start somewhere, hold down alt, and then do this. And then you can change it to whatever you want. And are you on Windows? No, I'm on Mac. Yeah. Okay. I'm like, alt. Where's my alt on Mac? Oh, yeah. It's because the reason is because I have a keyboard, an external keyboard, which has all the Windows names. It's actually option. Yeah. I think it's option. Okay. I wanted to make sure that we covered both Windows and Mac, but I'm, like, looking at my keyboard going, what is rich typing?

Okay. Good, good, good. Thank you. That is excellent. Yeah, yeah. And then let me see. There was another question that we could just hop in and answer right now, which is just about a naming convention or a sort of, like, setup convention.

Package source directory conventions

Trey had asked, is there a reason to put the package source code in a source directory, like src, which you often see in a R package? As well. For the example package, this would be, like, source slash tinytab instead of just tinytab. He says, I always create a source directory because I thought it was a convention, but I don't really know why. And, like, same. I do things all the time, and I'm like, I just see people do this. Is this the right thing? Yeah, I see so many different things. I see source. I see sometimes source has multiple things inside, not just, like, the main package, for reasons. Sometimes you have, like, some compiled code. But more often than not, I see, like, the actual package name as the first directory. So I just adopted that. And things changed a lot in the last few years. Like, there's been a lot of, like, movement towards just going to, like, one almost, like, an agreement on, like, pyproject.toml as being all the config. And then the other files you've seen before, like, requirements.txt, just kind of falling by the wayside.

So what I saw most was always, like, the main package name being first without source. So I just kind of went with that. But either way is good. Either way is good. Great. And I see Dan, Daniel Chen, just put in the chat that he made a repo to help explain all of the source slash stuff that might help people. Daniel is a lecturer in data science at UBC. And we are very lucky to have him in the community helping us out with documentation and stuff.

All right. We will put links to everything in the YouTube description as well. Okay. Let's keep going. So far, we have covered a tour of the package. We have covered a makefile, which is our aliasing for our sort of, like, terminal, long terminal strings that we don't really want to remember every single time. And we have also gone through individual test files, where they fail when we run make test, which is testing everything. We've looked at giving them IDs so that we can identify what's failing when it's failing. And we've also parametrized so that we can put multiple arguments into one test. So it can run through those. Where do we go next?

Running pytest from the command line

I just want to show you that pytest itself can be used on the command line. Of course, that's what you use in the makefile targets. But I want to show you some of the cool things you can do. Just one or two things. Like, for instance, I had mentioned before that you could run individual files. So you have some slow running tests. So you just, you know, like, it takes a while for all of them. Oh, I don't know why my focus always gets trapped right here. Tests, formats, dot py. So you can do that. But what I also like to do is, like, if you're just doing one file, v for verbose. And I'm going to run that.

Oh, tests, of course. Yeah, you have to type everything right, of course. It's a computer. There we are. And this is kind of cool, because this actually expands for one file. I'm sure you can do it for multiple files. But actually, absolutely, you can. But it's nice. It expands to, like, even, like, the parts in parameterize right here. So you see, like, format number mini. It's many different times. We see different instances of, like, the different maps. Like, the different, like, you know, like, values here. And that's how they're formatted. And we sort of see our, we have the ones with our names, our IDs. And this is the one where there's, like, a double map that basically parameterized twice right here. So USD, both 12.5 and minus 12.5, you see that right here.

So it looks like the mapping is, like, the 12.5 first and the minus 12.5 second with all the different currency codes, which is interesting. And yeah, basically, assert, you can use any sort of, like, Python statements you want. This is actually maybe a little bit tricky right here. Any result that starts with, does the result start with, like, a negative sign? And does also the value less than zero? Yeah, these are both true or both false, then the assertion is true. It's only slightly mind bending when you think about it, but it's not bad.

But yeah, the cool thing I want to show you here is, you know, like, this is a test for multiple different parameters going in. And you can totally do that. Currency, in this case, we have currency here, just one value. And then, like, this X value here coming in. So it's the same sort of setup as well. You got to make sure all the values here, whether they're in, like, you know, like a comma, sorry, like this, like, comma list, or separate like this, they are part of the signature, and they're, you know, used inside the functions.

Testing for errors with pytest.raises

Oh, yeah, another cool thing is you can set up things, of course, you can, like, you know, create intermediate results and then have an assert statement. That's pretty common. The ones above were just a straight assert statement. But you can do, you can library any code you want, and then, like, end off with one or more asserts.

Okay, the next big thing I want to show you is, like, errors. Because a code you create will definitely, you know, raise errors when there's, like, you know, like, improper inputs or, you know, lots of reasons.

So what we use here is essentially with, this is basically a context thing. And we use PyTest and raises. So when we're doing stuff like this, we have to make sure, usually you don't import PyTest, but when you're doing things like, you know, PyTest raises, or we had another case where we used PyTest param, then you do import PyTest at the top of the file. But you'll see all sorts of test files that even don't really have PyTest imported at the top. Because you can use an assert statement anytime you want to. Exactly, yeah. In Python, yeah, you don't have to be using PyTest. Exactly, yeah. And PyTest is, when you run it on the command line, there's no need for it to be available here. You're sort of running it outside of, like, the files.

Yeah, so this one's kind of cool. So basically we're saying here, we're expecting a value error, and we're expecting a certain message when we invoke this function with this, you know, currency, which is not the right currency. And basically this is why you would run, you know, like, you would, like, create these tests after, like, doing a little bit of, like, you know, either, like, looking at the function itself and just imagining what tests you need, or just running things and, you know, causing an error and seeing what kind of error message pops out, and then basically just, like, bringing it over to, like, this test.

And a key thing is, like, it's mentioned here, this match actually is like a regular expression, like a regular match. So you can do all sorts of things, like, you know, have regular expression characters, like dots or stars, you know, things like that. All the usual things. But this is basically just literal text the way it's written. There's no special characters here.

And you can, you know, a good tip is just to, you know, omit, you know, those special characters if they would collide with this sort of thing. So just have a fragment that, you know, is, like, you know, key in this error. And basically we're saying here, like, the error includes, like, the input, you know, like, we put this input, it's incorrect, but the message includes it. So we're testing for that as well. And it really does raise. And the reason you need this context manager here is because if you're just run from a currency, it would error. Yeah. So it would basically say, like, you know, give you a tester. But in this case, we're looking for an error. So and of course, we have that in testthat as well. There's, like, you know, expect error. So yeah, that's the same sort of thing. So I'm trying to test this, test this, tie this back to testthat in some way.

And this is like, I wanted to ask about your process a little bit, because you mentioned,

like, okay, you could either think of all the tests that you possibly might need, or you can just, like, hit them as they come up.

do you go through formal testing, where you're like, I'm going to try to break this in every way I can? Do you send it to someone and be like, hey, break this in every way that you can?

Yeah. We've done that. Before yes. Yeah. We've done that a little like a only we call it like a test party or it wasn't just when I called it. Basically, yeah, I would sometimes I would spend like hours. This is pre AI, right? We'll talk about AI later. But, you know, like in the before times, I would spend like, you know, like, sometimes like very long periods of time, just like, you know, like with one function, all the different inputs, bad inputs as well. Maybe it's too much, but I always kind of maximal with tests. Because I felt more secure, like, you know, that things were well tested. And if anything broke, even on some other side of the code base, you know, it would, it would catch things, you never know how things interact.

Yeah, or somebody using a different browser, like if I'm creating an app, I'm always like, hey, who uses like Linux or Windows or something? You're like, who uses a totally different browser? Can you can you test this and tell me what you get? Because there's almost always something that I wouldn't expect.

Oh, yeah, there's there's stuff like that, too. That that goes well beyond like browser specific testing is a thing that goes well beyond what we're going to show here. But you could test for that. Absolutely. And, and we're gonna talk a little bit about that later this month, by the way, everybody, because we're doing shiny testing. We're doing two weeks of shiny testing with Barrett Schlarke. So we will be covering shiny and R and shiny and Python. And he'll talk about like, testing UI stuff where you need like a headless browser to test that things are working correctly. So we'll hold all of that. But this is just basic testing, which I am loving because I'm already a little bit overwhelmed. But I'm really excited to see like an entire wall of passing tests in the terminal is like really satisfying. It's great. All green. It's wonderful. But yeah, I'm gonna show you a little more on raises. There's like many, many things here. Let me talk about value error. That's just an error you give you can there's all sorts of like, you know, clusters in Python compared to like, right, you have an R. This is something I chose in the code. So I have to choose the right error could be type error, things like that. I just have to know that I when I when I create this function, and having like a wrong currency value, it is a value error. So that just mirrors what I already have in the code. So yeah, there's that.

Also, there's this thing you can do, you can capture the exception. And basically, this is like exception info. And we know we know this raises an error. But now we have a variable is this right here, which is kind of cool. And you can grab values from it, for instance, like, you say, like, you know, message is like the value from exception info. And we just made a string to make sure it was a string. And we can say, like, inside, we know we're up here, like, you know, I know, currency, this gives the code, it's part of it, we can say, like, we want to make sure that, you know, for like many different cases, you know, we have these codes inside message. Yeah, and that's basically it. So we're doing like, assert that this code is in the message. And that's basically it.

Okay, we use parameterize for every cases as well. So we're saying here, like, these are like, not really good inputs at all. They're very various. But right here, for a number, we just want a number, we don't want a list, we don't want a Boolean, we don't want none, we don't want a string. And all these cases, we could just like, say, these are all bad, right here. And it's gonna be pretty literal in all these things, like bad, that's actually pretty good verbiage for such a thing. Then we know, obviously, like, these are like, you know, improper inputs, and these all raise an error, a type error. And again, it's not a value error. So we have to know what we're actually doing inside the code. So that's why we have that.

Skipping and marking expected failures

What if we want to skip something? What if we're like, I know this is gonna fail, and I'm not fixing it right now. So let's just come back to this later.

Yeah, that's a good one. Okay. We can talk a little bit about that and testthat as well. Because we can we can be like, skip this and like, we'll come back.

Yeah, okay. It's been a while, but testthat for me. And back back in the day, when I used to testthat a lot, I still do. But I'm not writing as many tests as I used to, for reasons of maintaining things in R. For the most part, that may change. But anyways, in testthat, what do we do sometimes if there was a failing test, and I still want to keep the test in? I would just like comment it out and say to do fix this test. I'm sure there's a better solution in testthat maybe there was always one, there was one now. But at any rate, let's suppose that there wasn't a solution for that. And my thing was good. It's actually way better in pytest, because we can use something called x fail. And what we do is we just mark. So mark, we can mark with any sort of thing. This is like a, like a thing that's accepted with arguments, we can say like, this is going to fail, and we call it x fail. And the reason it's x fail is because like, there's an x in like this report. So if you do like make test, maybe that's not the reason. But we see an x here, this is something that x fail, this is actually it right here, right here at the end of test formats. And another instructive thing is like this runs from begin to end. So at the end, this is like the last test, and it's actually right here.

So it fails, but we know the reason. We have a test your front number, we have this argument called desk mark, you'll know this, but if you just look here, there's no desk mark argument here, but we're trying it out as a test. We really want to do want to work like we expect this. But the functionality, it just isn't in there yet. Like it's not in the in the package. But we don't want to, you know, delete the test, you know, for a future where, you know, desk mark is implemented. So we just say, yeah, format number has no desk mark argument yet. And we just we just say mark of an x fail. That's what we call it.

Nice. We're like, we know why this is happening. Exactly. Chill out. One day, one day it might work. And the cool thing is like one day when it does work, it'll actually say like one x fail worked, like it actually passed.

Oh, nice. And it's like, It won't just skip it completely. It'll be like, hey, bonus, your x fail is working, you might want to go update it.

Yeah, because you may forget about the x fail. I mean, this shows it up front. Like we have some things we have to address, which is better than to do, I would say, because to do you just have like search for it. Like an ID may highlight it, but still, you have to search for it. This puts it right up there. Like, it shows you which, which parts of your tests are x fail tests.

Yeah, because you may forget about the x fail. I mean, this shows it up front. Like we have some things we have to address, which is better than to do, I would say, because to do you just have like search for it. Like an ID may highlight it, but still, you have to search for it. This puts it right up there. Like, it shows you which, which parts of your tests are x fail tests.

But yeah, this doesn't work. Let's just do something. Let's see if we can do this. Can we do this? Maybe this is not good. Oh, it's not gonna work. Okay. I was gonna make it work and show it to you. But I don't wanna do that.

Like implement desk mark on the fly right now. No, yeah. Or even just like, you know, like fake it with like format number. But I think I just want to tell you, take my word for it. If when it does pass, it'll actually tell you that, you know, x fails does this x fail thing does pass now.

Very cool. Yeah, but it's a great reminder for things that fail. And you might have some tests, and you change some functionality. Another use case is you may have some functionality changed, and it breaks your tests. And we could just mark, we just find the failing test, we just, we don't want to change it or, or remove it. We'll just say, okay, just mark it with x fail, we'll get to it later, that sort of thing. So it could not be for upcoming functionality, it could be just for broken functionality, you just want to like address a little bit later.

Yeah, cuz like, I usually do. I don't want to forget to do this. But I'm not fixing this right this minute. Because yeah, oh, yeah, one o'clock in the morning, I need to go to bed. Definitely want to sleep. So like, yeah, it is.

Fixtures and conftest

What about that conf test thing that you mentioned earlier? Can we go back to it? I want to go I want to go back to it. I'm curious. And it has the word conf in it. And conf just ended for me. And I'm like, so stricken. So much work.

Yeah, it's actually pretty good. I've been using it. And essentially, what you make are what we call fixtures. And fixtures are just the variables, I guess, or just like objects, which are available. You don't have to import them, they're just available for each of these files. So that means if you make a fixture, and you call it menu has to be in the form of a function, you can give it a doc string like this just to describe it. Basically, this is giving you a table. And the cool thing is, you don't have to like make this table multiple times in several tests. Let me show you that. So we got this to remember menu. Okay, that's a keyword, and it creates a table for you. Let's go to test table. So we're going to use that.

And I'm going to show you where fixtures shared setup. Okay, here we go. So in this case, I'm saying menu, and it has an attribute columns. And this is nice, because you don't have to make the menu like before in the test or up here. You know, above the tests, you can just call menu and change the fixture. If you want to, you can make multiple of these, you know, which have, you know, different things, different features of this sort of table. And, you know, test that sort of thing and name descriptively. So we're saying menu, let's go back to it again, it's good to go back and forth. It has these columns, and it has these values within these three columns, like basically three rows. So going back to test table, we're actually testing the bits of the table. So columns, it's going to be the ones we have in menu, way back in conftest, just available, you know, like menu, it doesn't appear until right there, nothing up here to do with menu, which is great. So it's like, here it is. So yeah, even separate here in this comment, which is very similar for us, menu comes from conftest.py, open that file, see how it's defined. There it is. So number of rows, that's another attribute it has, it's three. And these all pass, we know that, because we remind you, make test, these all the time.

Yeah. So I have a question. Yeah. Is this just a helper object that helps you build tests? Or is this something that is like, actually inside your package, that you have defined and is going to be usable by the user? I would say the first one. Yeah. Okay. So it's more like, at some point, we might expect a menu from our user. Yeah. So we want to test menu as an object. And we just want to create it and save it over here so that we have like, one place where we can define menu, and we use it in multiple different places.

Exactly. That's exactly it. So if I go back here, I don't really have any example of a table. Oh, right here. Yeah. So we have like, all sorts of like, methods that menu has, we want to test those too. So like, we're saying menu as text, that's a basically, so if you're just coming new to Python, basically a method is like a function that the object menu has, that you can invoke. So that's basically why you could just do that sort of thing. So cool things I want to show you. Okay, one more thing. Okay, so we made an object here, menu format, we use the function we have, we're basically invoking format currency on the price column. And we have this set of formatted values. And we're gonna say the table change. So formatted, this new instance of this table, it's not the same as the original. So basically, we're saying assert, you know, like something is not. Or we're saying assert, in this case, you know, this value is inside, like this new table, as text, basically, just like, essentially, it's going to be like a string. We have that, and then not in. So these are just some variations of like using asserts, and combined with like this fixture, which is menu. Another key thing is you do have to call it right here, you're not calling, you have to define it inside like the signature of these, of these, these test functions. That's the only time we really need it. Otherwise, it's not available, like sort of like, globally. I mean, it is, but you still have to pass it through. Because it's just the way it is. So let's say you don't, yeah, let's say you don't do it. It's going to like, automatically sort of like, you know, in the browser, not like it. And if you run the tests, I'm sure it's going to fail. Yeah, basically right here, name menu is not defined, it can't find it. So it does have to go here.

It's got to know what it's testing. Yeah, exactly. I mean, but that can trip you up. Because sometimes you're just used to like, creating all these like things without arguments. And then all of a sudden you need an argument. But it's, you just got to know when, of course. But it makes sense, like, when you use ID and also like, what's menu, it's like, it makes no sense to, to have it here if it doesn't exist somewhere. Plus, if you're going to reuse it over and over again, and need that type of object, like might as well.

Snapshot testing and test coverage

Okay, so we have 12 minutes left. Do we think we can cover the basics of snapshot testing?

And maybe test coverage, because we ran in the very beginning that like, test curve. And we saw our sort of like snapshot of our, why shouldn't use that word twice to mean two different things. But we saw kind of a picture of what our test coverage looks like. Let's try to do it. Yeah, what is that testing? So I'll do a quick snap, it's going to basically be, when do you use it? It's when you have like these longer outputs, which are hard to test. A good example is a table in text form, like HTML form, for instance. It's really hard to test, you have to use like, you know, like, see what you have to sometimes like, so you have this string somewhere inside this blob of text. But it's not pretty good. Sometimes you just want to say like, this, this function creates this output, it's not going to change, it should be this. So that's perfect for snapshots. It's kind of like if it does change, maybe it's intentional, and you just have to update snapshots. So that's like a decision point, pretty key one. But sometimes it does, your values that you get out do change compared to the snapshots, like a previously known good form of the output. And it's a problem. It's like, oh, these are incorrect. These new values, these new outputs. So that's why we use it for like big, bulky sort of like outputs, like a chunk of HTML is a great one for that. So for instance, okay, how to use it. So there's a few ways. I use typically Syrupy is one of the plugins I use.

But I also use another one. I was just looking at them like, oh, make me hungry.

Yeah, it's great. And it's got a great site. Super great. There's like one or two other ones that are available. They're pretty much the same. They behave a little bit differently. But the whole gist is basically like you, you capture these snapshots, they, at least in Syrupy, they become these amber files, I'm gonna click into it to show you. So basically, like, this is like the ID, or basically the function name. And this is the output you get. So basically, we're printing out an HTML, a table, obviously a tiny tab. And this is good, because like we're saying, like, for the invocation we used, it's going to be this type of output. Okay. And if things change, it's gonna be for good reason. Right? Okay, same here. This is like, just like a markdown table.

And like, you know, these shouldn't change this for good reason. So they're captured here in these like amber files. Okay, so we went back to test table. And the way it works is you always have, at least in Syrupy, snapshot is the first as one argument. And then like, this is just a fixture that we need. So it's like, you know, but at the very least, we have snapshot, because we're going to compare it against that. And I believe you don't need to have any other naming, just having snapshot in there will make it work. So like Syrupy, we'll just get in there and say, like, okay, we have a snapshot here. Let's create a snapshot if there's one doesn't exist. And if it does exist, we're gonna make sure menu as text looks like the snapshot that we have. If it doesn't, then you know, we got a problem, it's going to fail.

Another workflow, I mean, another piece of this is, okay, say, for instance, you change the functionality and the output does change. Say, like, you know, like, you change the formatting, for instance, just using a different type of formatting for the table. Then it would say, okay, it fails. But you have a, you have some recourse to update the snapshot. And in their make file, I created this. It's basically test update. And it would say, like, the snapshots don't work. But you would see the diff and say, like, okay, this is fine. We made this change. This is actually fine. Let's update the snapshot. So you should be really deliberate and careful with that. Look at all snapshots and see that, like, the update is what you intended. You would run this. Then you would run make test one more time. It would say something like warning. Your snapshots are, you know, updated. It would just give you a warning. Then the next time you run it, it would be just fine. Because in that case, the snapshots are pretty much accepted as truth. It just gives you, like, one round of warnings, just so you know, after an update. So these are good things to know. Because, like, you might get, like, oh, still warnings. What's going on? But it's fine. It's just, like, the snapshot update.

Yeah. Yeah. So that's basically my workflow. I use this a lot in gray tables, snapshots. Like, tons and tons of tables are snapshotted. And if there's any changes, sometimes it's, like, large scale changes. Sometimes, like, a few things. I just look at the diff and make sure it's good. And I accept, basically, like, the entire set of that.

That boggles my mind. Because GT tables can be so complex.

Yeah. The trick is not to make a too big snapshot. Try to, like, trim down. You have to do, like, really small versions of things. Yes. Yeah. For your own sanity. Yes.

The best idea. Don't go test this, like, massive table with a million different calculations and things in it. It needs to be, like, a three by three that just demonstrates that the thing works, right? Yeah. And it kind of goes down to, like, the philosophy of testing. Like, test very specific things that are different.

And try to, like, as much as possible, like, narrow it down. But some large scale tests are good, too. Just in case there's, like, weird interactions. You never know. Especially with tables. Like, things will change. One thing will change up here. But something on the bottom will change unexpectedly because of that thing. Because of something else.

And try to, like, as much as possible, like, narrow it down. But some large scale tests are good, too. Just in case there's, like, weird interactions. You never know. Especially with tables. Like, things will change. One thing will change up here. But something on the bottom will change unexpectedly because of that thing. Because of something else.

Test coverage

Yeah. Sometimes interactions. All right. Well, I say we talk a little bit about test coverage. Because we only have six minutes left. And then maybe I'd love to hear just a little bit about your thoughts on using AI for testing.

Yes. Okay. So, test coverage. It's kind of like people say you should get a pretty high value. What is test coverage? Basically, it means that in your invocations, like, in your tests, you access different code paths. You may have conditionals. You may use certain arguments. They may flow through conditional statements. And as long as you use, like, a variety of those things, you'll have a test that covers, like, a flow through these different, like, things. And there will be errors and things like that. So, the conventional wisdom, maybe it's not conventional wisdom, is that you should get a pretty high test coverage value. I kind of think a variety of tests is good. High coverage is great. 100% maybe too lofty of a goal. Sometimes unrealistic. So, like, yeah, I think you should just be comfortable with good amounts of testing, I guess you can say. Without having to cover every single line or, you know, like, branch in the conditional. Because some things are actually hard to test. Like, and could be, you know, possible. Speaking of which, you may want to if you're really into it, you may want to mark those lines which are impossible or impractical to test with something called pragma mark. Like, pragma ignore. I don't have an example of that here. But what it says to PyTest is just in the coverage component is that don't count this as part of the coverage. It's basically, you know, ignore this line. Make it pass or make it part of the coverage.

So, that's what that does. But also It's good to know. Sometimes there are things that you're just not going to be able to test adequately. Exactly, yeah. So, if you really want to aim for a high number, you might have to do some pragma stuff. But probably there's different, like, takes on that, whether that's good or bad. So, I'll leave that to you to decide. All right. I think test coverage is, like, a step above in topics. So, this is just to get us curious. And in the spirit of curiosity, I say we go back and we answer some questions. Yeah. Last little four minutes here. Let me see what they are.

Audience Q&A

Okay. He'll do his best. So, the first one was from Rory, and it was that one about Jupyter Notebooks. He says, are there situations where a Jupyter Notebook should be used over Quarto? Because I use Quarto for a lot of things, a lot of notebook things. Have I been doing Quarto wrong? And Sean had chimed in and said, or what about Marimo?

I like all these things. Like, Marimo is actually really, really good. So, I even have Marimo at the ready in my own IDE. I think it's great, especially for testing things. And it's actually good to use a variety of things, just in case there's, like, notebook specific things in your code that work or do not work. Like, certain representations. Like, for a great table, for instance, something's worked differently in the VS code, like Jupyter, compared to, like, say, Marimo or in Positron. So, it's actually, in my case, it was good to use a bunch of things. But you may just, for practical reasons, just stick to one that you like and just run with it, I would say. There's no right or wrong, I think.

Yeah, whatever works for your purpose, whatever you're comfortable in. Like, we, Isabel and I, we get questions a lot where people are like, do I have to switch to Positron? What if I love RStudio? And we're like, just use RStudio, then. No one's twisting your arm to, like, change to anything else. If your workflow doesn't contain Python at all, or any other language at all, there are still some cool things that happen in one that won't happen in the other. But, like, you gotta use what you like.

And we had another one from Garrett that said, this one can wait until the end. Is there a particular functionality in PyTest that makes it preferable to other libraries like UnitTest? Or is it just preference and familiarity for rich? For me, it's a latter preference. And also, like, aligning with, like, pretty much what everybody else uses. I think PyTest, I'm not sure of this, but I seem to see it everywhere. So I think it's like the leading thing. So I'm just like running with something that is like established. And that's what made my decision run the way I tend to do that, too. Also, I tend to like if I see a lot of people using something, and I can tell that there's a lot of documentation or community input around it, then I feel more comfortable using it. Because I'm like, I'm going to be able to find an answer. I'm going to be able to find someone else who's using this who can help me.

There was one from Isabella, too. Isabella, did you want to ask about the? Yes.

That you had shared this HTML file. And I just wanted to, like, hear what you what it is. I don't know how we should use it. Because it was very helpful. Yeah, exactly. So I had the site. Yeah, basically, I whipped this up, like, yesterday, like, I was working on this stuff. These are just, like, things that I think are cool with tests, or I use, I should say, in Python. I was just trying to, like, lay out, I got quite a few Python packages on the move. So I thought, like, a layout, basically what I use, and some of my preferences therein. You may find this useful, you may not. It's actually pretty good stuff, I would say. Like, basically, things I showed you, like, make test. And, of course, running things in CI. Just to remind myself, use parameterize more often than not. Prefer that over loops. Because then you can find out, like, what failed and what did not fail. And if I do use raises, always use match. You can actually, like, run, like, you can actually check for things not failing by, like, just, like, running, like, invoking a function inside of, like, for instance, here, you can just run the function. And don't print a search statement. And if it doesn't fail, you know, then it works. But there's actually a better solution to that. And I think this goes over some of that as well.

Very nice. We appreciate all of your thoughtful resources today, Rich. This is amazing. And, of course, as the creator of great-docs, like, you are the resource creation dude right now. I do have to stop us because we're at the top of the hour. We reached the top of the hour, but we didn't get to ask Rich about his thoughts on using AI in testing. Maybe Rich will join us in the Data Science Lab channel and give us a little glimpse of his thoughts. Awesome. Okay. Thank you so much, Rich, for joining us, giving us just a taste and a toe in the water of what testing in Python looks like. I am a baby Python tester myself. I only ever just learned a search statement in grad school, and then we moved on very quickly from that. So, we really appreciate sharing all of your wisdom. All of these links are in the Discord server in the Data Science Lab channel. If you're watching on YouTube, they're in the description. Everybody, round of applause for Rich. Rich, thank you. Oh, thank you for having me. It was great.