Python for decision makers and business leaders Transcripts
Lecture: Examples of a pytest test
0:00 In the theme of giving you
0:01 a slight little taste without going too deep
0:03 into the technology and the programming details
0:06 let's just quickly look at what a test
0:09 in Pytest and Python looks like.
0:12 All we have to do is create a little method
0:14 a little function like this.
0:15 Here this one's called test_electric_guitars.
0:18 And, as long as the name starts with test_
0:21 this test is going to be run when we say
0:23 Hey Pytest, go test my Python code.
0:26 Now, what are we going to do here?
0:27 This guitar's library that we're working with
0:30 on the next line, this comes from our website example.
0:34 Give me all the guitars that are electric.
0:36 And we want to test that this library is able to
0:38 do that correctly, that it makes sure it's electric
0:41 and not all the guitars or something like that, right?
0:44 So, we get the guitars back and then we can just do this
0:46 simple little assert statement saying
0:48 I would like to verify that all of the guitar's styles
0:52 are equal to electric for the guitar and guitars.
0:56 That's it. If what we get back are only electric guitars
0:59 this is going to pass.
1:00 If we get something else, like an acoustic guitar
1:03 well our test will discover that bug in the all guitars
1:07 function in that library and let us know somethin's off.
1:09 We need to go fix it. Pretty simple, right?
1:12 This is the test for what I would call the happy path.
1:15 If things are working in the right way
1:18 the right input is being given to the library
1:20 and it's behaving in the right way
1:22 we asked for a reasonable guitar type
1:24 we got some of those types back.
1:27 The other side of testing is, of course
1:30 testing for invalid input.
1:31 Now this is super important.
1:32 I've worked with lots of interns
1:34 and junior software developers
1:36 and they're very good at the first type of programming.
1:38 I wrote the program and it does what you said it should do.
1:41 Great! But soon as you type in something wrong
1:44 oh, you were supposed to type in a number.
1:45 You didn't type in a number there
1:46 and so the program crashed.
1:48 Or it turned out it couldn't get to the website
1:50 so the whole thing crashed.
1:53 Testing for when things go wrong is almost as important
1:56 for testing when they go right.
1:57 So testing that you have the right error handling
1:59 and failure cases and so on, is super important.
2:02 So with Pytest it's easy.
2:03 We just create this thing called a with block.
2:04 We say it raises this type of error.
2:06 Then we try to abuse the library.
2:09 We say lib.all_guitars(None).
2:11 And it's not supposed to take None.
2:13 It's supposed to take some kind of string.
2:15 Electric, acoustic, whatever.
2:17 It got none and it says no, no, no.
2:18 This value you gave me, this is an error.
2:21 So this test is like an opposite.
2:24 Normally what you want is, if it behaves good
2:27 the test will pass.
2:28 This one is if the library does not fail
2:31 if it doesn't raise that error, then the test fails.
2:34 Because we said we expect this error.
2:35 So it's like a double negative
2:36 but it's exactly the kind of test that we need
2:39 to check for these error conditions and error handling.
2:41 And with Pytest, not to bad.