NCrunch, your friend with TDD
Today I wanted to talk you about NCrunch, a great tool I´ve discovered thanks to my new colleages. It allows us to automatically run our Unit Tests while we are writing the production code.
So we´ve been told about TDD, Test Driven Development. The methodology itself is easy to understand, but specially in the beginning tricky to take (I think is because it may seem as against common sense: why should I write a test for something that doesn´t exist yet).
TDD is based on three simple steps:
1. Start with a failing test, _don´t fail to fail!_. This test checks a small working piece of functionality. *Red*
2. We write the minimun amount of code that makes it pass. Minimun is minimun! *Green*
3. Refactor the code (tests and production code) to avoid duplicated code and do improvements.
We iterate though every of these steps, adding more and more tests until we have all of the user story finished. Previous tests must pass in every moment.
You´ve probably noticed that a quite important part is running tests. I´m so used to watch the tests explorer (whatever it is) and run tests manually as I follow TDD. Sometimes you can loose concentration while running a bank of tests, these three steps should be fluent, fast.
So here it´s when NCrunch comes to rescue: NCrunch is running on paralel, watching our changes, and running all of the tests again as soon as it finds any modification.
We´ll see a few things going on:
There´s a circle in the corner of the right side. It will be green if every tests pass. Otherwise it goes red with a number, this tells the number of failing tests or projects that don´t compile.
In the left side along the code, we see some arrows. These are starting tests. The arrow is green if it passes or red if the test breaks. (This agrees to te red/green cycles of TDD).
In addition we can find code coverage in a very easy graphical fashion: If you see, we have some circles in colour. This circles are green if that line of code is succesfully reached by a test, likewise it goes red if the test doesn´t pass. Circle becomes black when the line is not covered at all.
If you browse the NCrunch page you can see a video of all of this in action. An image is worth a thousand words.
You can imagine how this helps doing TDD: we write a test and it goes red inmediately. We implement the minimun code (Resharper can help us doing that from the test). It will be the minimun, otherwise we´ll see black circles appearing in the logic. Test goes green and we do refactor having the test still green, together with other previous tests.
It´s just amazing how you forget about Tests Explorer. When I started in my new job I noticed my colleagues hardly ever debug the code, and the reason for that was easy: NCrunch was doing the hard work for them. They code simply focusing on the programming part.
NCrunch can be used with MSpec, Specflow, MSTests, NUnit and probably many others. NCrunch is just another productivity tool (yes, Resharper is the big boy). I know I have a few friends that don´t like to polute their wonderful Visual Studio environment, arguing it makes VS slower. Well, while the former is true, I´m a big fan of these tools, I´m not lazy, I just want to be a more efficient programmer. Yes, the machine can go slower, but isn´t productivity increasing a good reason to ask for more memory to your manager?:)
As a side note, I would like to talk you something very funny that happened to me regarding NCrunch:
In order to get the position I got in my new company, I need to do a programming exercise. I finished it and I was proud of the result. I followed TDD and so, my code coverage ought to be great. A couple of days after I started the new job, I opened my exercise solution with my brand new Visual Studio and NCrunch installed. Suddenly I noticed an entire method full of black circles, that wasn´t covered at all… Luckily my technical leader, Richard, was so kind to let me start working with them.
Cheers mates, keep coding!