Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

TDD Made Easy With NCrunch

DZone's Guide to

TDD Made Easy With NCrunch

·
Free Resource
Introduction

A while ago I blogged about Test Driven Development, where I did some introduction to TDD. As a result of the blog post, I have received so many reactions from different people saying doing TDD is a waste of time, it doesnot worth it and a lot of comments. Someone even said "You write twice as much code to get the same job done, the overhead involved kills". I strongly believe testing your codes as you write them is more important than fixing bugs after you have deployed the application. And TDD can be real fun when you have the right tools to assist you. I have used ncrunch, a visual studio plugin for sometime now and it really makes TDD worth doing.

Downloading and Installing ncrunch

Ncrunch is a visual studio plugin that when installed, on your machine can save you the time involved in shuttling from visual studio and the test runner gui for running unit tests, it can help you run your tests automatically, even when changes are detected in the code, it will run the tests and display the results, thus, saving you a lot of time and making TDD fun. It can be downloaded from ncrunch.net, after the download, proceed with the installation. After installation, when you start a new project in visual studio, you will see a new NCrunch menu that has been added to visual studio, where you can make some settings and do some configurations.

Writing the codes

I will just use a trivial project to demonstrate how to use ncrunch to assist when doing TDD. Start a new class library project in visual studio and name it PasswordManager, add another project to the solution and name it PasswordManager.Tests. In the PasswordManager project add a new class and name it PasswordHasher, the class contains a HashPassword method that accepts a password and returns it hashed string, the code listing is below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace PasswordManager
{
    public class PasswordHasher
    {
        public string HashPassword(string password)
        {
            HashAlgorithm algorithm = new MD5CryptoServiceProvider();
            var data = Encoding.Default.GetBytes(password);
            var hash = algorithm.ComputeHash(data);
            string HashedPassword = Convert.ToBase64String(hash);
            return HashedPassword;
        }
    }
}

    
In the PasswordManager.Tests project, add reference to the unit testing framework that you are more comfortable with, in my case I prefer nunit, then add reference to the PasswordManager project. The code listing is below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using PasswordManager;

namespace PasswordManager.Tests
{
    [TestFixture]
    public class PasswordHasherTest
    {
        private PasswordHasher passwordHasher;

        [TestFixtureSetUp]
        public void TestSetup()
        {
            passwordHasher = new PasswordHasher();
        }

        [Test]
        public void TestHashPassword()
        {
            string password = "school";
            var hashedPassword = passwordHasher.HashPassword(password);
            Assert.IsNotNullOrEmpty(hashedPassword);
        }

        [TestFixtureTearDown]
        public void TestTearDown()
        {
            passwordHasher = null;
        }
    }
}

Putting ncrunch to work

The next thing to do is to enable ncrunch for the entire solution, set all necessary settings. This can be done by selecting Enable NCruch from the Ncrunch menu in visual studio.

 


 

After necessary configurations have been made, ncrunch does the work of compiling the class libraries and does the running of the tests. With ncrunch your tests will be automatically re-run anytime you make changes to the codes, so as to assure that nothing has been broken. And it doesnot come in your way, it does it work while you continue writing your codes, you can check the results in the ncrunch window, the tests that passed, those that failed and the processing time.

 

Conclusion

Test Driven Development is really worth doing and it saves the developer a lot of headaches later in the future and even ncrunch can make TDD worth doing and faster.
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}