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