Getting started with pytest Transcripts
Chapter: Test Functions
Lecture: Structuring test functions

Login or purchase this course to watch this video and the rest of the course contents.
0:01 I want to take a break and talk about test structure for a minute. All the tests that we're writing so far kind of fall into a similar structure with
0:09 the asserts at the bottom. And that's not on accident. So let's grab one of these test to dict and talk about it specifically.
0:19 We'll stick it over here and just so that we can run it if we want So here's our test. And the structure comes in several forms and it's been
0:36 it's been talked about by Kent, Beck and Dan north and many others. as either Arrange, Act, Assert or Given, When, Then.
0:46 So let's talk about Arrange Act Assert at first. So at the top of the test we're gonna arrange whatever we need to arrange and
0:54 then we're going to do an action and then then we'll assert some stuff. How does that look like in our tests? So in our test we've got c1.
1:03 we created a card. This isn't this is just kind of getting ready to do an action. So this would be the arrange.
1:15 And then the action we're testing is 'to dict'. We want to see what 'to Dict' does so to dict is the action.
1:24 And then the assertion at the end is this bottom bit where we have I think the expected value creation is part of the assertion.
1:34 So we've got the assert here and a lot of people really really kind of resonate with the arrange act assert idea.
1:45 So if arrange act assert works for you to keep these in kind of a good order. That's great. The main thing we wanna do is we want to
1:52 keep the arranging stuff at the top. With no asserts. Hopefully the act obvious and the act should be kind of obvious
2:02 from the test name as well hopefully. And then the assertions at the bottom. What we don't want to do is inter leave these.
2:09 We don't want to start doing multiple actions interleague with asserts.
2:14 Because those are difficult. What resonates more with me is given when then so let's take a look at what that would be like.
2:22 So given a card with known value when when we call to dict on the card
2:41 then we get the expected dictionary. The reason why I really like the given when then model of it. It's the same as Arrange Act Assert.
2:59 It's just I like thinking about a given state. So given a certain state when I do some action then some expected outcome happens.
3:09 I like this because then I can come up with different given states. So for a particular action, what are the all the different states that I need
3:18 to act on? And then for a given action, what sort of maybe what sort of expected outcomes could I get even if I come
3:27 up with different states? And this sort of thinking helps me. And so for me I usually think and given when then and not Arrange Act Assert
3:34 but really either one if it works for you then awesome.


Talk Python's Mastodon Michael Kennedy's Mastodon