Python for Decision Makers and Business Leaders Transcripts
Chapter: Testing
Lecture: Examples of a pytest test

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

Talk Python's Mastodon Michael Kennedy's Mastodon