Effective PyCharm 2025 Transcripts
Chapter: Testing
Lecture: Writing a Couple of Tests

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Just for completeness sake, let's actually write a couple of tests. Okay, so here's this test that a table can be booked.
0:08 If you're new to how pytest works, the way that it understands what to do is
0:13 if a function starts with test underscore, it will be discovered by pytest and run,
0:19 and tests being in the name of the file also lets it get discovered. Okay, so let's go over here, and I put some notes just to remind myself
0:28 what the heck we're up to here. So I'm going to say a table, and notice there's this core layer that we can test.
0:35 I'm always a fan of having nice separation. So we can say all tables, bracket zero, that'll give us a table.
0:43 We're going to book that table, passing its table ID, and what we get back is a booked table. So this actually returns a booked table.
0:54 Is it the same object? Did it get set in the database and returned as a query?
0:59 Doesn't really matter, but we're going to want to work with both of those.
1:02 So we're going to get a table, we booked it, and now we can verify two things.
1:08 First, we want to verify that the table itself, the one that we got, is booked. So we'll check here. We'll say assert, this is how pytest works.
1:17 We put some Boolean expression. Assert table is booked. Right? That's the first thing. And assert table ID is the same as the book table dot table ID.
1:30 Okay. So we can run this again, make sure all of our tests are passing.
1:34 And if we actually went over here and just made a change, like what would happen if we didn't book the table? Would our tests still pass?
1:41 You want to make sure you're testing something for real.
1:44 Indeed, if we don't set that value and we're not actually getting this set the way you would expect. So put it back. Works.
1:52 A little bit of flavor of test-driven development there, but not too much.
1:59 And then for the final one here, not that side, the final one here, what we want to know is we cannot book a booked table.
2:07 We're going to find a table by category 2. That's like Thai or burgers or whatever. And then we're going to book that table.
2:15 I'm going to get the first one. And we're going to book this one. That should work. That's basically the same thing we did in our test before.
2:23 But now we want to assert that if we try to book it again, that it's an error. Well, let's try to run that. Hmm, we've got an exception.
2:34 Okay, not ideal. The exception is that the table is unavailable error because it's already booked. But that's what we want.
2:44 We want it to be an error if we try to do the wrong thing. We're testing the edge cases here.
2:48 So what we can actually do is we can say with pytest.raises, we give it this exception, which I guess we could probably put as core.
2:57 And then we run it. It's testing for this error's existence. It says if you do not throw this exception and you go through this context block,
3:08 this with block, that itself is an error. But if you do, that's what you're looking for. So we're trying to test the error handling here.
3:15 We run this again. Sure enough, it throws the error. And if for some reason we don't run the code that throws the error, it says error.
3:23 It did not raise the exception that you expected it to raise. So perfect.
3:29 There we have a couple of tests written and running them and testing them with the pytest runner.
3:35 You can do things like show the past ones, only show the failed ones or the ignored ones, all those kinds of things.
3:42 So there's a lot of controls here and we'll dig into them a little bit more soon.


Talk Python's Mastodon Michael Kennedy's Mastodon