Python for the .NET developer Transcripts
Chapter: Course conclusion
Lecture: Unit testing review
0:00 We all like software that works
0:01 and one of the great ways to do that, verify that it's
0:04 going to work, and keep it working, is to write unit tests.
0:07 We saw Python has a built in testing module named unittest
0:11 although we didn't do much with it because we said, pytest.
0:14 That's probably where they action is these days
0:16 so let's write some tests with pytest.
0:19 The way it works is we just define some methods
0:21 test_ whatever you want to call it
0:24 here we said test electric guitars.
0:26 We also used two fixtures, and these are ways
0:29 to inject data or functionality into our test method.
0:34 Really, really cool. We just have to create those
0:36 in a separate location and put
0:38 the @pytest fixture decorator on it.
0:41 Then we say the name, here we have guitar data
0:43 and this is our generated test data
0:45 and we have mocker, this is our mock object library.
0:49 We had two functions to remove the dependencies
0:52 from our tests. Get guitars from the db
0:54 that was our database one.
0:56 And that one returns our test guitar data.
0:58 And our logging was another dependency
1:00 but we didn't really care what it did
1:01 so we just said, autospec = True.
1:03 Just go away log, go away.
1:06 And then from then on we're able to just
1:07 do our regular testing
1:09 and recall that pytest overrides or takes control over
1:12 Python's regular assert statement.
1:14 So you just say assert, and then some truthy thing
1:17 that you might want to test for, like
1:19 all the guitar styles are electric.
1:22 Boom. That's our test.