{"id":231577,"date":"2021-01-13T13:18:36","date_gmt":"2021-01-13T21:18:36","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=231577"},"modified":"2021-01-13T13:45:05","modified_gmt":"2021-01-13T21:45:05","slug":"visual-studio-for-mac-helps-you-write-tests","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/visual-studio-for-mac-helps-you-write-tests\/","title":{"rendered":"[Guest Post] Visual Studio for Mac Helps You Write Tests"},"content":{"rendered":"<p>You\u2019re writing tests for your code, right? No? Just say \u2018yes\u2019. It\u2019ll make this blog post go a lot easier. So anyway, I\u2019m happy to hear that you\u2019re writing tests for all your code. Whether you\u2019re following the \u201ctest first\u201d \/ Test-Driven Development (TDD) approach or whether you\u2019re just writing some unit tests or integration tests, Visual Studio for Mac has some nice features to make your life as a developer a lot easier.<\/p>\n<p>In this article, I\u2019ll walk you through the process of writing and running unit tests using Visual Studio for Mac.<\/p>\n<h2>Some Terms<\/h2>\n<p>Before I jump in on the features, let\u2019s get a few terms out of the way. (For more information on the different types of tests and why you might choose one over the other, <a href=\"https:\/\/www.benday.com\/2021\/01\/05\/tdd-vs-unit-testing-whats-the-difference\/\">check out this article<\/a>.)<\/p>\n<p>What\u2019s a \u201cunit test\u201d? A unit test is a piece of code that exercises and verifies the execution of a piece of code in your application. That application is referred to as the \u201csystem under test\u201d or SUT. Let\u2019s say I\u2019m writing a .NET Core application using C#. When I\u2019m writing code, if I have a C# class in my application, I\u2019ll have at least one test class in my test project. For every public method in my application\u2019s class, I\u2019ll have at least one test method in my test class that tests the corresponding application method.<\/p>\n<p>If you\u2019re new to automated testing, you might want to check out this article on <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/testing\/unit-testing-best-practices\">unit testing best practices<\/a>.<\/p>\n<p>Test-driven development (TDD) isn\u2019t a type of test \u2013 it\u2019s a way of writing features &amp; tests. TDD is commonly referred to as \u201ctest first\u201d and the idea is that you\u2019re going to write your tests before you write your application code. Starting with your tests gets you a better overall design and cleaner code.<\/p>\n<p>Writing your tests first can be a little cumbersome because you\u2019re starting with almost nothing. Thankfully, Visual Studio for Mac has some features that help to make it easier. In this example, I\u2019ll show how we can generate code as part of our test-first development workflow.<\/p>\n<h2>Tour of the Sample Code<\/h2>\n<p>Source code is available at <a href=\"https:\/\/github.com\/benday-inc\/visual-studio-mac-tdd-demo\">https:\/\/github.com\/benday-inc\/visual-studio-mac-tdd-demo<\/a>. This repo contains both \u201cbefore\u201d code so that you can code along and also an \u201cafter\u201d folder that has the completed sample. If you\u2019re interested in starting from scratch, you might want to check out <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/testing?view=vsmac-2019\">this guide to getting started with the Testing tools in Visual Studio for Mac<\/a>.<\/p>\n<p>Let\u2019s say that you\u2019re going to write a calculator using test-driven development (TDD). The first step is to start writing your tests. To save time, I\u2019ve created a .NET Core solution in Visual Studio for Mac that consists of an <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/testing\/unit-testing-with-mstest\">MSTest<\/a>-based unit project, an class library for my application, and an ASP.NET Core MVC web application.<\/p>\n<p><figure id=\"attachment_231578\" aria-labelledby=\"figcaption_attachment_231578\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231578\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-application-teams-desc.png\" alt=\"Visual Studio for Mac - Solution Explorer showing UnitTest1.cs selected.\" width=\"644\" height=\"484\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-application-teams-desc.png 644w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-application-teams-desc-300x225.png 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><figcaption id=\"figcaption_attachment_231578\" class=\"wp-caption-text\">Figure 1 &#8211; The empty calculator solution in Solution Explorer<\/figcaption><\/figure><\/p>\n<p>The class that contains the calculator logic is eventually going to go into the <code>Benday.CalculatorDemo.Api<\/code> class library project but at the moment that class doesn\u2019t exist. We\u2019re going to start writing our test code in a class called <code>CalculatorFixture<\/code> in the <code>Benday.CalculatorDemo.UnitTests<\/code> project.<\/p>\n<h2>Rename the Unit Test Class<\/h2>\n<p>Let\u2019s start by editing UnitTest1.cs. Right now that class just has the default structure for an MSTest-based unit test in it and the class is named <code>UnitTest1<\/code>.<\/p>\n<p><figure id=\"attachment_231579\" aria-labelledby=\"figcaption_attachment_231579\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231579\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated.png\" alt=\"New empty unit test method shown in editor.\" width=\"966\" height=\"544\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated.png 966w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-300x169.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-768x432.png 768w\" sizes=\"(max-width: 966px) 100vw, 966px\" \/><figcaption id=\"figcaption_attachment_231579\" class=\"wp-caption-text\">Figure 2 &#8211; The empty calculator fixture test class<\/figcaption><\/figure><\/p>\n<p>Let\u2019s change the class name to be <code>CalculatorFixture<\/code>.<\/p>\n<p><figure id=\"attachment_231580\" aria-labelledby=\"figcaption_attachment_231580\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231580\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-description-automa.png\" alt=\"Unit test shown renamed to CalculatorFixture\" width=\"958\" height=\"546\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-description-automa.png 958w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-description-automa-300x171.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-description-automa-768x438.png 768w\" sizes=\"(max-width: 958px) 100vw, 958px\" \/><figcaption id=\"figcaption_attachment_231580\" class=\"wp-caption-text\">Figure 3 &#8211; Rename class to be CalculatorFixture<\/figcaption><\/figure><\/p>\n<p>Right now the file name doesn\u2019t match the class name \u2013 the file is named <code>UnitTest1.cs<\/code> and the class name is <code>CalculatorFixture<\/code>. There\u2019s an easy way to fix that. Click on the \u201c<code>CalculatorFixture<\/code>\u201d word of the class declaration and press Option-Enter to bring up the <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/refactoring?view=vsmac-2019#quick-actions-and-refactorings\">Quick Actions and Refactorings<\/a> menu. One of the options is <strong>Rename type to CalculatorFixture<\/strong>. Visual Studio for Mac is smart enough to know that your class name doesn\u2019t match the class name implied by your filename so if you choose this item from the menu, it\u2019ll rename the class for you.<\/p>\n<p><figure id=\"attachment_231581\" aria-labelledby=\"figcaption_attachment_231581\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231581\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr.png\" alt=\"Visual Studio for Mac refactoring menu shown, with option &quot;Rename file to CalculatorFixture.cs&quot; highlighted.\" width=\"956\" height=\"706\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr.png 956w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-300x222.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-768x567.png 768w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><figcaption id=\"figcaption_attachment_231581\" class=\"wp-caption-text\">Figure 4 &#8211; Use the Quick Actions and Refactorings menu to rename the file to CalculatorFixture.cs<\/figcaption><\/figure><\/p>\n<p>Now if you look at the Solution Explorer window, you\u2019ll see that the filename now says <code>CalculatorFixture.cs<\/code>. Sure, this isn\u2019t life-changing but it\u2019s one of those little things that makes coding a little more pleasant.<\/p>\n<p><figure id=\"attachment_231582\" aria-labelledby=\"figcaption_attachment_231582\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231582\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-1.png\" alt=\"Solution Explorer shown, with CalculatorFixture.cs class selected.\" width=\"644\" height=\"484\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-1.png 644w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-1-300x225.png 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><figcaption id=\"figcaption_attachment_231582\" class=\"wp-caption-text\">Figure 5 &#8211; Renamed file in solution explorer<\/figcaption><\/figure><\/p>\n<h2>TDD Step #1: Write Your Tests<\/h2>\n<p>Now let\u2019s write the empty structure of our calculator tests by adding test methods for<code> Add()<\/code>, <code>Subtract()<\/code>, <code>Multiply()<\/code>, and <code>Divide()<\/code>. When I know that I\u2019m going to want to write a test case but I\u2019m not quite ready to write it, I\u2019ll put in a call to <code>Assert.Inconclusive()<\/code>. If I left the test method code empty, it would look like a passing test but if I put the call in to <code>Assert.Inconclusive()<\/code>, it\u2019ll provide me a nice placeholder reminding me that I need to come back to implement the tests. It\u2019s not going to fail but it won\u2019t pass either.<\/p>\n<p><figure id=\"attachment_231583\" aria-labelledby=\"figcaption_attachment_231583\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231583\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-2.png\" alt=\"Unit test class shown with Add, Subtract, Multiply, and Divide methods which all just call Assert.Inconclusive()\" width=\"964\" height=\"1094\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-2.png 964w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-2-264x300.png 264w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-2-902x1024.png 902w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-2-768x872.png 768w\" sizes=\"(max-width: 964px) 100vw, 964px\" \/><figcaption id=\"figcaption_attachment_231583\" class=\"wp-caption-text\">Figure 6 &#8211; Test methods for Add, Subtract, Multiply, and Divide marked as Inconclusive<\/figcaption><\/figure><\/p>\n<h3>Run the Tests<\/h3>\n<p>Now that we\u2019ve got some code, let\u2019s try to run the tests. The easiest way to run all the tests in a test class is to right-click on the class name and choose <strong>Run Test(s)<\/strong>.<\/p>\n<p><figure id=\"attachment_231584\" aria-labelledby=\"figcaption_attachment_231584\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231584\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-description-automaticall.png\" alt=\"CalculatorFixture class is selected, context menu shown with &quot;Run Test(s)&quot; option highlighted.\" width=\"1068\" height=\"958\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-description-automaticall.png 1068w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-description-automaticall-300x269.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-description-automaticall-1024x919.png 1024w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-description-automaticall-768x689.png 768w\" sizes=\"(max-width: 1068px) 100vw, 1068px\" \/><figcaption id=\"figcaption_attachment_231584\" class=\"wp-caption-text\">Figure 7 &#8211; Run Tests in a Test Class<\/figcaption><\/figure><\/p>\n<p>You should see the Tests panel appear and your tests should run.<\/p>\n<p><figure id=\"attachment_231585\" aria-labelledby=\"figcaption_attachment_231585\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231585\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-application-description.png\" alt=\"Tests window displayed, with Add, Divide, Multiply, and Subtract showing as Inconclusive status.\" width=\"504\" height=\"634\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-application-description.png 504w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-application-description-238x300.png 238w\" sizes=\"(max-width: 504px) 100vw, 504px\" \/><figcaption id=\"figcaption_attachment_231585\" class=\"wp-caption-text\">Figure 8 &#8211; The tests ran and were inconclusive<\/figcaption><\/figure><\/p>\n<h3>Write the Test Code<\/h3>\n<p>Now let\u2019s write some test code for the <code>Add()<\/code> method.<\/p>\n<p><figure id=\"attachment_231586\" aria-labelledby=\"figcaption_attachment_231586\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231586\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-1.png\" alt=\"Additional test code has been written in the Add method, adding 2 + 3 and expecting a result of 5\" width=\"890\" height=\"482\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-1.png 890w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-1-300x162.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-1-768x416.png 768w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><figcaption id=\"figcaption_attachment_231586\" class=\"wp-caption-text\">Figure 9 &#8211; Code for the Add() test method<\/figcaption><\/figure><\/p>\n<p>If you press Command-B to build your solution, this is going to fail. The error should be something like \u201cCalculatorFixture.cs(39,39): Error CS0246: The type or namespace name &#8216;Calculator&#8217; could not be found\u201d. This is because there\u2019s no class named Calculator yet.<\/p>\n<p>In the API project, there\u2019s a class called <code>Class1.cs<\/code>. Let\u2019s rename this class to Calculator and change the filename to <code>Calculator.cs<\/code>.<\/p>\n<p>After you\u2019ve renamed that class to be Calculator, you\u2019ll still be getting an error because of a missing using statement. In <code>CalculatorFixture.cs<\/code>, on line 12, click on the word Calculator and then type Option-Enter to bring up the Quick Actions and Refactorings menu.<\/p>\n<p>You should see an option to add a using statement. Choose that option.<\/p>\n<p><figure id=\"attachment_231587\" aria-labelledby=\"figcaption_attachment_231587\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231587\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-website-description-aut.png\" alt=\"Context menu shown, with &quot;using Benday.CalculatorDemo.Api&quot; option highlighted.\" width=\"930\" height=\"456\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-website-description-aut.png 930w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-website-description-aut-300x147.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-website-description-aut-768x377.png 768w\" sizes=\"(max-width: 930px) 100vw, 930px\" \/><figcaption id=\"figcaption_attachment_231587\" class=\"wp-caption-text\">Figure 10 &#8211; Add the missing using statement<\/figcaption><\/figure><\/p>\n<p>Now that that\u2019s fixed, try to rebuild the solution. You should now be getting an error that says \u201cError CS1061: &#8216;Calculator&#8217; does not contain a definition for &#8216;Add&#8217; and no accessible extension method &#8216;Add&#8217; accepting a first argument of type &#8216;Calculator&#8217; could be found\u201d. This is because there\u2019s no method named <code>Add()<\/code> on the Calculator class.<\/p>\n<h2>TDD Step #2: Create an implementation \/ Add the Missing Method<\/h2>\n<p>Alright. So we\u2019re trying to write that Add() method and our test class is failing to compile because that method doesn\u2019t exist yet. Let\u2019s use the Quick Actions and Refactorings menu to generate that method for us.<\/p>\n<p>In CalculatorFixture.cs, on line 19, click on the word Add() and then type Option-Enter to bring up the Quick Actions menu. One of the options should be \u201cGenerate method \u2018Calculator.Add\u2019\u201d. If you choose this option, Visual Studio for Mac will create a basic implementation for you including the arguments and return values.<\/p>\n<p><figure id=\"attachment_231588\" aria-labelledby=\"figcaption_attachment_231588\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231588\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-chat.png\" alt=\"Context menu shown, with &quot;Generate method 'Calculator.Add'&quot; selected\" width=\"876\" height=\"492\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-chat.png 876w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-chat-300x168.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-chat-768x431.png 768w\" sizes=\"(max-width: 876px) 100vw, 876px\" \/><figcaption id=\"figcaption_attachment_231588\" class=\"wp-caption-text\">Figure 11 &#8211; Generate the Add() method<\/figcaption><\/figure><\/p>\n<p>After you run the Generate method action, when you open up <code>Calculator.cs<\/code>, you\u2019ll now see that there\u2019s an implementation of the<code> Add()<\/code>\u00a0method that throws a <code>NotImplementedException<\/code>.<\/p>\n<p><figure id=\"attachment_231589\" aria-labelledby=\"figcaption_attachment_231589\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231589\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-2.png\" alt=\"Generated code for Calculator Add method shown. This method just throws NotImplementedException.\" width=\"966\" height=\"518\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-2.png 966w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-2-300x161.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-2-768x412.png 768w\" sizes=\"(max-width: 966px) 100vw, 966px\" \/><figcaption id=\"figcaption_attachment_231589\" class=\"wp-caption-text\">Figure 12 &#8211; Generated Add() method<\/figcaption><\/figure><\/p>\n<p>Now when you rebuild your solution, it builds successfully without errors. When you run all your tests, you should now see that the Add test is failing and that the other 3 tests are saying inconclusive.<\/p>\n<p><figure id=\"attachment_231590\" aria-labelledby=\"figcaption_attachment_231590\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231590\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-3.png\" alt=\"Test windows is shown, with Add test failing.\" width=\"552\" height=\"670\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-3.png 552w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-3-247x300.png 247w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><figcaption id=\"figcaption_attachment_231590\" class=\"wp-caption-text\">Figure 13 &#8211; Add test is failing<\/figcaption><\/figure><\/p>\n<h3>Implement the Add() Method<\/h3>\n<p>Next let\u2019s implement the add method in the Calculator class.<\/p>\n<p><figure id=\"attachment_231591\" aria-labelledby=\"figcaption_attachment_231591\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231591\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-3.png\" alt=\"Add method has been changed to return the sum of the two parameters.\" width=\"940\" height=\"512\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-3.png 940w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-3-300x163.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/text-description-automatically-generated-3-768x418.png 768w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><figcaption id=\"figcaption_attachment_231591\" class=\"wp-caption-text\">Figure 14 &#8211; Implement the Add() method<\/figcaption><\/figure><\/p>\n<p>After you\u2019ve added the implementation for the <code>Add()<\/code> method, re-run the tests. The test for Add should pass.<\/p>\n<p><figure id=\"attachment_231592\" aria-labelledby=\"figcaption_attachment_231592\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-231592\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-4.png\" alt=\"Test window is shown, this time with the Add test passing.\" width=\"650\" height=\"746\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-4.png 650w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2021\/01\/graphical-user-interface-text-application-descr-4-261x300.png 261w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><figcaption id=\"figcaption_attachment_231592\" class=\"wp-caption-text\">Figure 15 &#8211; The test for Add() passes<\/figcaption><\/figure><\/p>\n<h2>Summary<\/h2>\n<p>From here you can now implement the rest of the tests using the same process. Visual Studio for Mac\u2019s ability to generate code for you while you\u2019re writing your test-first unit tests can really help you to avoid some common coding headaches.<\/p>\n<p>If you\u2019re looking for more advanced info on automated testing techniques such as how to break up testing dependencies and testing ASP.NET application security, check out my <a href=\"https:\/\/www.pluralsight.com\/courses\/architecting-aspnet-core-mvc-unit-testability\">Architecting an ASP.NET Core MVC Application for Unit Testability course on Pluralsight<\/a>.<\/p>\n<h3>About the Author<\/h3>\n<p>Benjamin Day is a consultant and trainer specializing in software best practices using Scrum with Microsoft\u2019s DevOps tools. Ben\u2019s main areas of emphasis include Azure DevOps, Scrum, software testing, and software architecture. He is a Microsoft MVP, a certified Scrum trainer via Scrum.org, and a speaker at conferences such as Pluralsight Live and VSLive. When not developing software, Ben\u2019s been known to go running and sea kayaking in order to balance out his love of cheese, cured meats, and champagne. His online courses are available at <a href=\"https:\/\/courses.benday.com\">https:\/\/courses.benday.com<\/a> and at <a href=\"http:\/\/www.pluralsight.com\">http:\/\/www.pluralsight.com<\/a>. He can be contacted via <a href=\"http:\/\/www.benday.com\">http:\/\/www.benday.com<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019re writing tests for your code, right? No? Just say \u2018yes\u2019. It\u2019ll make this blog post go a lot easier. So anyway, I\u2019m happy to hear that you\u2019re writing tests for all your code. Whether you\u2019re following the \u201ctest first\u201d \/ Test-Driven Development (TDD) approach or whether you\u2019re just writing some unit tests or integration [&hellip;]<\/p>\n","protected":false},"author":50156,"featured_media":231592,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[1666,3743,452],"class_list":["post-231577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-testing","tag-visual-studio-2019-for-mac","tag-visual-studio-for-mac"],"acf":[],"blog_post_summary":"<p>You\u2019re writing tests for your code, right? No? Just say \u2018yes\u2019. It\u2019ll make this blog post go a lot easier. So anyway, I\u2019m happy to hear that you\u2019re writing tests for all your code. Whether you\u2019re following the \u201ctest first\u201d \/ Test-Driven Development (TDD) approach or whether you\u2019re just writing some unit tests or integration [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/231577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/50156"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=231577"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/231577\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/231592"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=231577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=231577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=231577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}