Live Unit Testing with an example

Let's open the UnitTest1.cs file and add the following two test methods to it. The first method will create two instances of the Person class and check to ensure that both instances are different. The second method will create one instance of the Person class, assign it to another variable, and check to ensure that both instances are equal. The following screenshot shows this:

When you start writing the code line by line, you will see that the Live Unit Testing process will run in the background and provide the status of the code coverage and the test result in the left side bar. In the previous screenshot, every line generates a green tick mark (), indicating that the written test cases passed and all the lines have been covered.

If you are unable to see the live unit test working, refer to the Configuring Visual Studio 2019 for Live Unit Testing section of this chapter.

When any unit test method has failed, the icon will change to a red cross mark () up to the line where it failed. Refer to the Overview to Live Unit Testing in Visual Studio 2019 | Understanding the coverage information shown in editor section of this chapter for more details about the various icons shown in the editor.

Now, let's create another two test methods that will call the Clone() method of the class. Here, the Live Unit Testing process will break at the same line where the method has been called and notify us with a red cross mark, as you can see in the following screenshot:

In the preceding example, the Clone() method call breaks as it throws NotImplementedException. The rest of the test method lines will be decorated with a blue dash mark (), as those are all unreachable code.

Let's revisit the Person class, where you will see the following status notification from the Live Unit Testing framework:

Now, move ahead and implement the body of the Clone() method, which will now return an object by calling the MemberwiseClone() method. Momentarily, the Live Unit Testing process will execute automatically and show you the status in the left side bar of the editor. This time, everything will be represented with green tick marks:

Let's navigate to the test class. You will see that the code coverage is now 100% for those two methods. Also, all the test methods passed. This way, the framework ensures that the cases execute while you write the code, and the framework gives you live feedback on the changes that you made, thereby reducing the extra effort of executing the test cases manually:

Now that you are familiar with Live Unit Testing, let's jump into the next section where we will discuss how easy it is to quickly navigate failed tests in Visual Studio 2019.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.160.61