Thursday, 7 July 2016

[Visual Studio] Write Unit Test to Test your Application in C# & VB [Part 1]

This post is separated into 2 parts. If you are familiar with Unit Test, skip this post and proceed to second part. http://jaryl-lan.blogspot.com/2016/07/visual-studio-write-unit-test-part-2.html

Today's post is to guide you on how to write a unit test to test the functionality of your application. In case you are unsure what is unit test all about, you can check it out at msdn here https://msdn.microsoft.com/en-us/library/hh694602.aspx.

Before you can start writing a unit test method, you need to have a few functional methods. So as an example, we will write one simple dummy method. This method will treat the user "John" as an existing customer.

[C#]
public bool VerifyUser(string name)
{
    return name == "John";
}

[VB]
Public Function VerifyUser(ByVal name As String) As Boolean
    Return name = "John"
End Function

Once you have a testable methods, create a "unit test project" and add a new "test class" to your unit test project. You are required to add reference of the project that contain the testable methods.

Steps to create a unit test project:
1) On the Solution Explorer in your visual studio, right click on your solution, hover to "Add" and click "New Project...". A window named "Add New Project" will be prompted.
Add new Project
2) On the left pane under Installed, expand Visual C# or Visual Basic based on your desired programming language and click Test.

3) On the middle pane, click "Unit Test Project".

4) Fill in your desired name and location. Once you are done. Click "OK" button.
Add a new Unit Test Project
5) Back to Solution Explorer, right click on your newly created Unit Test project, hover to "Add" and click "New Item...". A window named "Add New Item - [Project Name]" will be prompted.
Add new Item
6) On the left pane under "Installed", expand until you see "Test", click on it.

7) On the middle pane, click "Unit Test".

8) Fill in your desired name and click "Add" button.
Add a new Unit Test Class
9) Back to Solution Explorer, right click on your Unit Test project, hover to "Add" and click "Reference...". A window named "Reference Manager - [Project Name]" will be prompted.
Add Reference
10) On the left pane, expand Solution and click Projects.

11) On the middle pane, tick the project that contain the testable methods and click "OK" button.
Select the desired reference to be added
Once you are done, open up the test class that you have added earlier. The different in this test class is the class is being decorated with "TestClass" attribute and every test method are decorated with "TestMethod" attribute. Both of these attributes are required for Unit Test and the "TestMethod" attribute is necessary for the test method to appear in "Test Explorer" window. Test method must not contain parameter and the return type must be void.

Assuming you want to test whether the user "John" is exist by executing the method named VerifyUser. So create a method and decorate it with "TestMethod" attribute. From this method, invoke the method VerifyUser and pass in the string value "John" as parameter value. Obtain the result from the method and verify it with Assert. The Assert.AreEqual is used to verify whether the result returned from VerifyUser matches the expected result. In this case, the expected result is true.

[C#]
[TestMethod]
public void IsJohnExistTest()
{
    var component = new RedemptionComponent();
    var result = component.VerifyUser("John");

    Assert.AreEqual(true, result, "User 'John' does not exist.");

}

[VB]
<TestMethod()>
Public Sub IsJohnExistTest()
    Dim component As RedemptionComponent = New RedemptionComponent()
    Dim result As User = component.VerifyUser("John")

    Assert.AreEqual(true, result, "User 'John' does not exist.")

End Sub

To run your test method, there are two ways.
[Option 1]
1) At the top menu in Visual Studio, click Test, hover to Windows and click Test Explorer. "Test Explorer" window will be shown in visual studio.
Visual Studio - TEST - Windows - Test Explorer
Open Test Explorer window
2) On the Test Explorer window, right click on the desired test method and click "Run Selected Tests". To debug the test, click "Debug Selected Tests". If the test method did not appear, Make sure that your solution is compiled.
Visual Studio - Test Explorer - Run Selected Tests
Run Selected Tests

[Option 2]
1) In your class file, locate the desired test method that you want to test.

2) Left click on the method and on keyboard, press CTRL+R and T. To debug the method, press "CTRL+R and CTRL+T".

Once the test method execution is completed, you will either see
1) A green tick icon if the test is successful, or
Visual Studio - Test Explorer - Successful Test
Successful Test
2) A red cross icon if the test failed.
Visual Studio - Test Explorer - Fail Test
Failed Test
This is how unit testing works. Simple and straightforward. But of course Unit Test are not limited to only this. If you want to find out more on what else can be done with Unit Test, check it out at the second part of this post. http://jaryl-lan.blogspot.com/2016/07/visual-studio-write-unit-test-part-2.html

Refer to the following for the sample project.
C#: https://1drv.ms/u/s!Aj2AA7hoIWHmgm-KRQQBuL3ZCCf7
VB: https://1drv.ms/u/s!Aj2AA7hoIWHmgm44Muju_EO1tGl9




No comments:

Post a Comment