Quantcast
Channel: Testing – Falafel Software Blog
Viewing all articles
Browse latest Browse all 68

TDD with Visual Studio 2015 – Day 26 – Visual Studio 2015

$
0
0

Test Driven Development, or TDD, can be a great way to build software. When building software test-first, you create unit test to describe some functionality the system under test doesn’t yet have. Then, you write the simplest possible code you can that will make the test pass. Next, you should consider refactoring your code, to improve its design and clarify your intent. Finally, commit your changes to source control, even if only locally, marking another incremental unit of progress in your crafting of the application. This follows the Red-Green-Refactor-Commit cycle of TDD, and it’s well-supported for a variety of test frameworks in Visual Studio 2015.

To get started with TDD and unit testing in Visual Studio 2015, you can perform a simple code kata, such as FizzBuzz. To get started, create a console application, then add a test project to the solution and have it reference the console application:

vs2015-fizzbuzz-solution

As you can see in the image above, I’ve already added a few tests. You can easily tell what the tests are doing by reading the name of the class and then the name of the method, e.g. FizzBuzzService(.)FormatNumberShould…ReturnFizzGiven3. Read more about this unit test naming convention. You can also auto-run the tests with every compile, by clicking the left-most icon at the top of the Test Explorer.

One of my favorite Visual Studio features is CodeLens, which lets you see how many tests target a particular method (and their current status):

vs2015-codelens

You can also use CodeLens to see the current status of the individual tests themselves, and run them directly from the editor without having to navigate right-click menus:

vs2015-codelens-test

A new feature in Visual Studio 2015 (Ultimate) is Intellitest, which will automatically generate tests for a given class or method. After adding a bit more logic to the FormatNumber() method, it looks like this:

public class FizzBuzzService
{
    public string FormatNumber(int candidate)
    {
        if (candidate % 15 == 0)
        {
            return "FizzBuzz";
        }
        if (candidate % 3 == 0)
        {
            return "Fizz";
        }
        if (candidate % 5 == 0)
        {
            return "Buzz";
        }
        return candidate.ToString();
    }
}

Right-click on the method and select Run Intellitest. You’ll get a series of automatically generated unit tests, which immediately run. Click the save icon to save the tests to a new project, where you can give them better names and ensure they’re correct and useful to your application. Intellitest can be a good way to scaffold some tests for a method that doesn’t have any, giving you a place from which to start doing Test-Driven Development in a legacy codebase situation.

Intellitest Examples:

create-intellitest

run-intellitest

Even if were doing TDD from the start of a project, Intellitest may help you identify some corner cases or boundary conditions that you hadn’t considered. For instance, what should the FizzBuzz formatter do with 0, or negative numbers? Follow the same rules? Throw an exception? The kata doesn’t say, but in a real application you would probably need to consider these kinds of inputs and have a plan for how the application will behave in these cases.

Code Maps and Test Projects

Another nice Visual Studio feature is Code Maps. You can generate a code map by right-clicking on your solution name in Solution Explorer and choosing Show On Code Map. You can see can example, with a Console project, the Test project I created, and the automatically generated one Intellitest created, here:

vs2015-codemap

CodeMap files are interactive, allowing you to zoom in to projects, namespaces, classes, and even methods to see what they reference, and how. In this case you see all of the test methods reference the FizzBuzzService and its FormatNumber method, but in a more complex application Code Maps can be very useful for visualizing dependency trees. For unit testing, they provide a visual means of seeing which parts of the system under test are being referenced and called by test methods, perhaps making it easier to locate untested areas of code (though a test coverage tool is usually the better way to accomplish this).

Resources

 

The post TDD with Visual Studio 2015 – Day 26 – Visual Studio 2015 appeared first on Falafel Software Blog.


Viewing all articles
Browse latest Browse all 68

Trending Articles