Getting Started with Unit Testing in C#
to write a unit and integration test, you need a testing framework besides your Visual Studio IDE. there are several testing frameworks out there, but we are going to use the NUnit Framework.
the NUnit framework gives you a utility library to write your test and a Test Runner which runs your tests and gives you a report of passing and failing tests.
Source Code
You’re going to write your first unit test. so download the zip file from this Git Repository.
this is a visual studio solution that includes some code you’re going to test through this tutorial.
Writing your first Unit Test
Logic explained:
we have this folder called Fundamentals, let’s take a look at this “reservation” class
here you’re building an application for reserving a table at a restaurant.
our reservation class has currently one property that specifies the user who made this reservation.
- This is the Method we want to test “CanBeCancelledBy“ a given user.
- a simple business rule is implemented: if the user can cancel this reservation if he is “Admin” or “the User who made this reservation”, otherwise they can’t cancel it.
- a User Class, with one property, “IsAdmin”.
Scenarios to Test (execution paths)
here we have three scenarios:
- when the user is an admin (AdminCancelling).
- when the user is the same person who made this reservation (SameUserCancelling).
- when someone else tries to cancel this reservation (AnotherUserCancelling).
Start with Unit Testing
Now in order to test this Class, we check the Project under our solution Called “PSTesting.UnitTest“
we right-click on the project and add our first class and we rename it after the class that we want to test in this case “ReservationTest“.
So visual studio creates this new file “ReservationTest.cs“which is a simple C# class that has the [TestFixture] attribute and it has one method called “TestMethod1”. with the attribute [Test].
these two attributes you see here belong to the NUnit test framework. So the test Runner we have in VS looks at all the classes with the “test Fixture” attribute, then it looks at all the Methods in this class with the “Test“ attribute and it will run them. See more.
Now the first thing we want to do is change the name of the Method to CanBeCancelledBy_UserIsAdmin__ReturnsTrue so we can test our first scenario
the name should be meaningful, I suggest following this naming convention: ClassName_Scenario__ExpectedBehavior
- inside every test method we have three parts (arrange Act, Assert), this convention is called AAA (triple-A)
- the “Arrange” part is where we initialize our objects we want to test. in this case, we want to create an instance of the reservation class.
1//Arrange 2var reservation = new Reservation(); // we need to add refrence to the project
- the “Act” part is where we act on this object, and that basically means we’re gonna call a Methos that we’re going to test.
1//Act 2var result = reservation.CanBeCancelledBy(new User { IsAdmin = true });
- the “Assert” is a helper class in Nunit Framework that has a bunch of static methods that we use to make an assertion see more.
1//Assert 2Assert.That(result == true);
2. Now to run this test, we go on the top, from the “test” menu, and we select “Run All Tests”
this should open the “Text Explorer” window, so we can see the list of passing/failed tests, and the time it took to run this test.
Refactoring
once we have tests we can refactor our code with confidence, because our test is always verifying that our application is still working, and nothing is broken.
For example: let’s refactor the Method to the following single line of code and run our tests.
the result is: all tests passed.