Tuesday, October 22, 2013

Using the ReSharper Test Runner for Unit Tests and TDD

Are you using ReSharper?  If no, ask yourself why not.

I have met devs who swear by ReSharper, devs who have never heard of it (scary), and then devs who keep putting it down due to in some cases pure ignorance or typically some brief past experience with it.  I am in the boat where I swear by it because it just provides so many efficiency gains that it's a no-brainer not to buy it for yourself and use it.  Even if your company won't it's totally worth buying your own.  One of those huge benefits is the ReSharper test runner that's included in ReSharper.  Remember people, ReSharper is not JUST intellisense.  It has a ton more benefits such as code hints and a lot more than even that to help you as a developer shave time and learn as well as you code.

Now when I say ignorance to those who keep saying ReSharper is bad, usually people who say this typically give these reasons:
  • It's slow
  • It's unnecessary, VS Intellisense has what I need
  • It costs too much
  • I don't need Intellisense I'm a hard core programmer, I code without it :)
I say to each of these nonsense!  And I will debunk those:

  • yes it used to be too slow 8 years ago when ReSharper did have some slowness issues, I even remember when I was frustrated with it at one time.  But that issue has resolved been resolved a long, long time ago by JetBrains
  • It's unnecessary because you use VS intellisense?  Ok that's fine, I'm not here to dictate what works best for you.  I'm not saying ReSharper may 100% be fore you but I bet you haven't tried it or tried it long enough to realize all the things it provides you as an add-on that VS 2010, 2012, whatever version does not currently do or do well.  I can blog post about that later on, which I plan on doing as well
  • Costs too much?  I am ashamed that devs expect everything for free and willing to sacrifice making life easier by refusing to buy a tool for $100-$200 which IMO is probably worth $600 considering what JetBrains has done. That includes Webstorm as well, awesome IDE that they sell for ONLY $50.  The fact is there are tools that provide just too much benefit not to buy yourself if your employer won't divy up a lousy $150.  In the scheme of things $150 is NOT a lot of money.  Also ReSharper now gives you free upgrades for an entire year.  Then after that it's only $90 to renew it yearly and again you get free upgrades for another year and so on..not a bad deal IMO considering how awesome this tool is and the feat it took to even build it
  • I won't even comment on the last bullet point, it's not worth my time, that's ridiculous

Now back to what you came here for, more about ReSharper's test runner.  I've always been an NUnit guy for unit tests.  I have although used MSTest in the past 2  years.  Regardless of what framework, I use ReSharper's test runner.  You can always use ReSharper's test runner as an alternative to most of the unit test framework's native test runners out there.  We know NUnit's test runner sucks, but obviously not the framework.  I think I tried MSTest's runner once and it didn't even compare to the ReSharper test runner (at least at the time, which was trying it 2 years go).  ReSharper's test runner makes it convenient, detailed but not too detailed, and simple to use.

Now lets take a look at what the test runner looks like and a few things it'll provide you and show some screen shots of that happening when writing your tests.

First, know that ReSharper will automatically pick up the fact that you are using a framework like NUnit or MSTest just by including the using statement to it in your test class.  Once ReSharper sees this, it'll provide some nice icons that you wouldn't otherwise get with vanilla Visual Studio when working in your test class:













Notice the icons point out in the orange area in Visual Studio.  When you start decorating your test class and test methods with attributes such as [TestClass] and [TestMethod] (or [TestFixture] and [Test] in NUnit), little round icons show up.  Those are a nice convenience in that they allow you to click and run a specific method or all methods (if you click on the icon at the class level) in your test class.

Also notice the red bulb icon..   This will allow you to create instances of those types that have not yet been created (i.e. your TDD test goes Red).  Those instances will be created within your test class.  You can also right-click the type and choose to create a new class for it which will generate a completely separate .cs file for it...which is probably more desirable.









Now lets look at an example of running a test.  I'll click on the first test and run it so we can see the test runner pop open automatically right within Visual Studio:













It gives us the option to run it or debug it.  If you want to step through your test while you run it, add a debug point first and then go back and choose the debug option here and it'll stop at your debug point once you click on the debug option here so that you can actually run your test but also step through it at the same time if you wish.

I'm just going to choose to run this method (not debug) for now.  Note: be aware that this is not a finished TDD method in this screen shot, you're only seeing the first line of it..no asserts are there yet.

When you run this test method, it'll will show the Resharper Test Runner Pane at the bottom of VS for the first time, and the results of the run:






















So now it ran and you can see that this method succeeded.  Also notice all the nice bells and whistles the runner can offer you.  

Back to the test runner.  Now lets show a TDD method where you are specifying a type that doesn't yet exist so your test is Red (does not build because of it):














When I try to run this unit test, it'll fail because the class InventoryService doesn't yet exist (as this is how you do TDD, you fail first, then create it to make the test go green by creating the actual type after).
























Here, we've said ok, I need to make sure I can instantiate a class that doesn't yet exist...that is, the service class we'll be creating.  Well (and I'm sure there's a keyboard shortcut for this), just right-click the type and choose to create it!:













And this creates a new .cs file and class for you:





























Now go back and run the test, and it'll turn green.  Now you're god to go again and to continue on your merry way and create more tests.

Finally there's a nice window to show you all the tests in your solution and get to them quickly and start running whichever tests you want:






































This is a small taste of what the ReSharper test runner can provide you.  It's just very usable and one part of functionality ReSharper can give you outside of pure intellisense, code complete, and the array of other stuff it provides out of the box.

Try, it, give it whirl sometime and please, please be patient and allow yourself to get used to ReSharper.  Don't expect to use all of what it offers either, over time you'll find things you like about it as you use it.  The point isn't to use everything, that's impossible.  I think if you give ReSharper a chance, you're going to really benefit from it in the long run.

1 comment:

  1. I built some integration tests for some of our projects here at work. They're not really "unit" tests per se, but they test the most common issues we have during deployment. Things like permissions on certain folders, the correct AD account in IIS. I do a loop over all entities in my EF project and do a basic select on each one... verifying that all the tables and views are there (at least for read only) etc. It makes a huge difference in how much time we spend

    ReplyDelete