TDD Primer with Nunit
Total Page:16
File Type:pdf, Size:1020Kb
A P P E N D I X A TDD Primer with NUnit What tools do you use for testing in .NET? This is a question many new developers who come to the land of TDD have for experienced developers. New developers are often not sure which testing framework is the best one to use. NUnit is a testing framework that has become the standard for testing in .NET. NUnit is a member of the XUnit family of testing languages, and as such is an open-source tool. There are other testing frameworks out there; however, none have had the level adoption that NUnit has. We plan on showing you how to install NUnit, either through the NUnit web site or through NuGet. We will finish this with a TDD primer to whet your appetite for using NUnit in the case-study chapters. Installation NUnit can be installed in two ways: through the NUnit web site and through NuGet. Web Page Installation To install NUnit you will need the following: v Visual Studio (any version) v The latest version of NUnit located at www.nunit.org/index.php?p=download v Please note that in order to get the NUnit client to run, you need to have .NET Framework 2.0 installed. It won’t matter if you are using a different version of the framework for your development. You will need to go the NUnit web site and download the latest version of the tool from the download tab, as shown in Figure A-1. 281 APPENDIX A < TDD PRIMER WITH NUNIT Figure A-1. The download page at nunit.org Clicking the downloaded .msi file will launch the usual install wizard. We recommend that you select the Complete setup type, as shown in Figure A-2. 282 APPENDIX A < TDD PRIMER WITH NUNIT Figure A-2. Selecting setup type while installing NUnit from .msi file As part of the installation, the NUnit client will also be installed on your system. We will show you how to use this tool in a moment. Once the installation is complete, the NUnit DLL is registered on your machine and available to be used in your testing projects. NuGet Installation Another and more convenient option for installing NUnit is through the NuGet Package Manager. NuGet is an open-source development tool that integrates into Visual Studio and gives you a simpler way of adding libraries into your project. If you are a Ruby developer or familiar with the Ruby language, you can think of NuGet as the .NET equivalent to Ruby Gems. Installing NuGet is very simple. NuGet is available through the Extension Manager that is a part of Visual Studio, as shown in Figure A-3. If you do not see it listed, you have the option to search for it in the search bar in the upper-right corner of the window. Simply click the Download button, and when the download is complete, restart Visual Studio. You are done. 283 APPENDIX A < TDD PRIMER WITH NUNIT Figure A-3. The Extension Manager window in Visual Studio Once NuGet is installed, you can access the Package Manager Console; this is where you will interact with NuGet. To get to the Package Manager Console, navigate to View Other Windows Package Manager Console, as shown in Figure A-4. 284 APPENDIX A < TDD PRIMER WITH NUNIT Figure A-4. Navigating to the Package Manager console To install the NUnit package, first you need to have a solution open. For the TDD aspects of this, we created a solution called TDDWithNunit. We will talk more about this solution and the projects that make up the solution in a moment. From inside the Package Management Console window, run the following command: PM> Install-Package NUnit What this command does is go to the web and pull the latest version of NUnit that NuGet knows of and installs it in the project that is listed in the Default Project drop-down. NuGet will not install NUnit on the system. As part of installing NUnit into the project, NuGet will add references to NUnit to the project. To verify that the install was successful, run the following command to see which packages are installed on a project, as shown in Figure A-5. PM> Get-Package 285 APPENDIX A < TDD PRIMER WITH NUNIT Figure A-5. Checking that NUnit was successfully installed through NuGet < Note If you install NUnit through NuGet, you do not get the NUnit client installed. There are multiple tools that you can use to run NUnit tests, including TestDriven.NET and ReSharper. If you want to use the NUnit client to run your tests, then install NUnit through the .msi file on the NUnit web site. TDD Walk-through Now that you have NUnit installed, you can begin to write code using test-driven development (TDD). This is a software development practice in which the developer writes tests before code. Once the tests are written, the developer will write just enough code to get the tests to pass. Once the tests pass, the developer can be certain that the code does what the customer wanted it to do because the tests are based on acceptance criteria provided by the customer. A common pattern in TDD is red-green-refactor. You start off with failed tests (red). You then write enough code to get them to pass (green). Then you refactor the code so that it is more manageable before moving on to the next step (refactor). < Note NuGet may not always have the latest version of a DLL, but you can update the DLL that you have for your project through NuGet. As of this writing, for example, the latest version of NUnit from its web site is 2.5.10.11092. NuGet, however, installs version 2.5.9.10348. In this walk-through, we are going to write a simple math class that will handle multiplying two numbers. We will use TDD to write our tests and then write the code in the math class that will get the 286 APPENDIX A < TDD PRIMER WITH NUNIT tests to pass. First, create a new console application and call it TDDWithNUnit. Once the solution is created, we need to do the following to write the first test: Add a new class library project to the solution. This new project should have the following naming convention ProjectUnderTest.Tests, where ProjectUnderTest is the name of the project that contains the methods you will be writing the tests against. For our walk-through, you will see that Figure A-6 contains a project called TDDWithNunit that contains the math class that we will be testing and a project called TDDWithNunit_Tests that will contain the tests. If you installed NUnit from the .msi file instead of through NuGet, you will need to add a reference to the NUnit DLL (nunit.framework) to the test project, as shown in Figure A-7. If you used NuGet to install NUnit, then you should already have the reference. Add a reference to TDDWithNunit to the test project TDDWithNunit_Tests. Figure A-6. Folder structure 287 APPENDIX A < TDD PRIMER WITH NUNIT Figure A-7. Adding NUnit DLL to the test project Once NUnit is set up, we can write our first tests. Create a class under the TDDWithNunit.Tests project called tddMathSpec.cs. You can go ahead and delete the file called Class1.cs but it is not mandatory. To be able to use the NUnit framework as well as access the tddMath class that contains our production code (we will talk about this file later), we need to add the following using statements to the tddMathSpec class: using NUnit.Framework; using TDDWithNunit; Now we need to let NUnit know that this class contains tests that need to be run. To do this we mark the class tddMathSpec with the attribute TestFixture. using NUnit.Framework; using TDDWithNunit; namespace TDDWithNUnit_Tests { [TestFixture] public class tddMathSpec { 288 APPENDIX A < TDD PRIMER WITH NUNIT } } Now, let’s add the test method AddTwoNumbers_returns_3_when_given_1_and_2. (we always like to name our test methods this way so that we know what method they are testing, as well as what the expected inputs and outputs are). Once we add this method, we need to add the attribute Test to it so that NUnit knows that this is a test to be run. [Test] public void Add_returns_3_when_given_1_and_2() { } < Note All test methods should be void methods. To test the method, we need to set up the object to get it to a state that we can test the method in question. Next, add a new method to the class called Setup() and place the attribute Setup above this. What this attribute does is tell NUnit to run this method before each test that is run in the class. We use this method to set up everything we need to test the method. All that’s left is to call the method with our two inputs and assert that the result from the method is what we expect. using NUnit.Framework; using NUnitTutorial; namespace TDDWithNUnit_Tests { [TestFixture] public class tddMathSpec { tddMath tMath; decimal expected = 3; [SetUp] public void setup() { tMath = new tddMath(); } [Test] public void Add_returns_3_when_given_1_and_2() { Assert.That(tMath.Add(1, 2) == expected); } } } What this test simply does is call the Add method on the tddMath class, giving it a 1 and a 2, and asserts that result given back is a 3. When you try to build the solution, you will get compile errors saying 289 APPENDIX A < TDD PRIMER WITH NUNIT that tddMath does not exist.