Python for .NET Developers Transcripts
Chapter: Testing
Lecture: C# tests

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we saw our C# app ran well and it seemed like it was working. Are you sure it's working? We know every edge case works?
0:09 And what if I type in something incorrect I only type the right thing acoustic or electric, when it asks. Of course, we should have tests for this.
0:16 So let's go look at our unit test over here. We have our lib and we have our lib_test. Probably I should have broken this into a testing project
0:24 and a lib project to be proper but this is already so cluttered. I'm trying to keep it simple here. So here's some tests that we can write in C#
0:32 And first of all we could do this the wrong way. Let's go and comment these out for a sec. Show you what happens. So here we're going to go and test
0:45 and it says test run me wrong. Why are we doing it wrong? Well we're just creating the library and then we're calling functions on it
0:52 to run those tests, see what happens. Over here on the right well it looks like our tests passed. That's pretty cool actually
1:00 so our tests run me wrong didn't work but if you look at the output down here. Here we go, you got to check the right things off.
1:08 If we look at the output down here in the test results, here's the test run for run me wrong into bug and then check this out.
1:16 Logging this to a file you shouldn't see the center test. We're getting the guitars from the database. Shouldn't see the center test
1:22 Oooh well maybe this line seemed nice and easy but maybe we shouldn't be doing that. So let's switch these back here.
1:31 There we go, I comment out that one. Then we've written it the proper way. So what did we do? Well over here we're now
1:38 I'll show you this test data in a second but we're going over here and we're creating a mock database implementation and a mock logger.
1:46 Now logger just says, You try to log to me I'm going to do nothing. But the database has to have fake test data here.
1:54 So we're going to mock out the database implementation and we call this, it should be going to the database and getting it, right?
2:02 But doing its validation and its filtering, and so on. It's a little bit contrived, but hopefully it's good enough for you to get an idea
2:08 of what you might really do, right? Then we're going to go over here and we're going to create a hash of these things.
2:14 I'm going to create a hash of the styles. And the idea is the HashSet is a unique collection of items, right? So we're going to just say all the styles
2:21 that would be electric there should only be one item when we pass in the style electric and that set should contain style.
2:31 That let's us test the electric guitars. It could also test all the guitars and do the same HashSet and see that there are exactly two
2:37 an electric and an acoustic guitar. And then finally, it's not usually enough to test just the good happy path. We also want to test the error case.
2:46 What happens if you do something wrong? For example, if I ask for all guitars passing null I would expect some kind of argument exception.
2:55 The way this works is if you don't throw an exception this test fails, right? So let's just run it one more time.
3:02 Get everything working. Here are our tests over there and if you look at all the output, check it out. We're running this one, this one, and this one
3:10 but we are not seeing any of that logging information. Good we're using our mock data. So the last thing to check out
3:16 is how are we creating this mock data? And I've put this over into it's little own class to create this, so we want a mock logger.
3:23 We use an external package mock that we installed from NuGet and we're going to go over here and create a mock of ILogger and just return it
3:30 return the object. If we don't tell it to do anything it basically just does nothing but doesn't crash when you call things like log.
3:37 But for the mock DB we're going to say we're going to get some of this test data down here and this should look familiar.
3:42 All right we have a limited set of test data. And then we're going to create a mock IDB and then we're going to tell it
3:49 Hey if somebody goes to the mock and they call get guitars from DB return that test data. And then we're going to return that object.
3:56 So that's how we avoid going into the database but have it return some data down here when we ask for guitars. There's not as much data
4:07 but it's plenty to test with. And that's it, so we wrote these well we were at four tests, and we realized one of 'em we shouldn't write
4:13 so we really wrote three correct tests here using our mock data, our Dependency Injection Open Close Principle, all that kind of stuff
4:20 here with our lib class and our test data and then we just did the test and it turned out everything works just like it should.
4:28 I guess we could do one more thing down here, maybe if we for some reason forget our filter, right? Let's just see that some tests fail.
4:37 Here you go the electric guitar test failed. No surprise, right? 'Cause we're getting acoustic guitars as well. Here we go, I'll put it back
4:46 and leave it in a happy state. So this project and this idea of testing in this way we're going to recreate this in Python using pytest.


Talk Python's Mastodon Michael Kennedy's Mastodon