Python for the .NET Developer Transcripts
Chapter: Course conclusion
Lecture: Unit testing review

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We all like software that works and one of the great ways to do that, verify that it's going to work, and keep it working, is to write unit tests.
0:08 We saw Python has a built in testing module named unittest although we didn't do much with it because we said, pytest.
0:15 That's probably where they action is these days so let's write some tests with pytest. The way it works is we just define some methods
0:22 test_ whatever you want to call it here we said test electric guitars. We also used two fixtures, and these are ways
0:30 to inject data or functionality into our test method. Really, really cool. We just have to create those in a separate location and put
0:39 the @pytest fixture decorator on it. Then we say the name, here we have guitar data and this is our generated test data
0:46 and we have mocker, this is our mock object library. We had two functions to remove the dependencies from our tests. Get guitars from the db
0:55 that was our database one. And that one returns our test guitar data. And our logging was another dependency but we didn't really care what it did
1:02 so we just said, autospec = True. Just go away log, go away. And then from then on we're able to just do our regular testing
1:10 and recall that pytest overrides or takes control over Python's regular assert statement. So you just say assert, and then some truthy thing
1:18 that you might want to test for, like all the guitar styles are electric. Boom. That's our test.

Talk Python's Mastodon Michael Kennedy's Mastodon