Keyboard Shortcuts for TestDriven.NET
A frustration that I’ve had for quite some time with TestDriven.NET is the apparent lack of keyboard bindings. Right-click/Run Tests just seems like such a nuisance.
Today I set out to solve that problem…
Using the Tools | Options | Keyboard option in Visual Studio, you can assign keystrokes to the TestDriven.NET commands. The ones you’re interested in are:
- TestDriven.NET.Client – runs the test cases on whatever the active item
- TestDriven.NET.Debugger – run in debugger option for the active item
- TestDriven.NET.Solution – Runs all tests in the current solution.
The future of MbUnit
Previously I posted my thoughts around deciding on a unit testing framework. There was some discussion in that posted that voiced concern about the future of MbUnit as Jonathan de Halleux (Peli) had given up working on MbUnit and that it was no longer open source.
This has been recently resolved, after Jonathon announced that Jamie would be the sole owner of the MbUnit project, but that he would be around in an advisory role. Jamie announced the other day that Andrew Stopford had stepped forward to take over the MbUnit project, bringing it back to tigris.org as an open source tool.
I’ve read Andrew’s blog for a while, and I certainly feel that Andrew will be wonderful in leading MbUnit to the next level. This now reaffirms my decision to recommend MbUnit as the unit testing framework of choice.
Habemus Papam
A couple hours ago, the Cardinals in Rome elected Joseph Cardinal Ratzinger as leader of the Roman Catholic church. Cardinal Ratzinger took the name Pope Benedict XVI.
This was the first time I’ve witnessed this process and have to say that it is truly fascinating. The process was surprisingly quick, but its been amazing to watch.
Unit Testing Frameworks: Deciding
As we start migrating towards a more agile environment at work, I want to bring forward a unit testing framework that we could standardize on. The way I see it there are four unit testing frameworks available for the .NET framework right now. These are NUnit, MbUnit, csUnit and Zanebug.
NUnit is the de facto standard as far as unit testing goes. NUnit started as a direct port of Kent Beck’s JUnit framework. Over the course of time, it has evolved to use several features of the .NET framework that make it much easier to use such as custom attributes and reflection related capabilities. Most books and tutorials that are written around unit testing in the .NET framework use NUnit as the framework of choice. This is a big deal.
csUnit has not been updated since November 9, 2003. No, thanks! I want something that will have continuous development and improvements.
I believe that Zanebug is the latest player in the unit testing framework arena. Zanebug looks very promising as an open source unit testing tool. It includes NUnit compatibility to the point of providing the ability to run existing NUnit tests without modification. It also provides additional features such as performance metrics, results graphing, and perfmon integration.
MbUnit was written by Jonathon de Halleux and has quickly progressed into a very robust framework. One of my favorite features of MbUnit is the console runner that you can use to generate HTML reports. It allows you to give your unit test assembly a Main method and distribute it as an exe. This is kinda cool.
A very compelling argument for MbUnit is its packaging with TestDriven.NET which is an addin that lets you execute tests on any piece of code directly in the VS.NET IDE.
Similarly to Zanebug, MbUnit is compatible with NUnit in that you only have to change the namespaces. MbUnit seems to have a lot of the same additional features that Zanebug does, including test decorators which handle repeated tests, “timed” tests, load tests, multi-threaded tests. MbUnit offers a very rich feature set, but there is also a nice extensibility model should there be something that you need that isnt present.
At work, we are using CruiseControl.NET for all of our projects and so I think it would be really nice if the unit testing framework could be integrated into NAnt and CruiseControl. At this point, the only solutions that do this are NUnit and MbUnit.
As such, I think the recommendation that Im going to bring to our team will be MbUnit, as it seems to have the most features as well as integration with the tools that we’re using (VS.NET and CruiseControl).
Am I missing anything? Which framework are you using, and why?
Introducing Agile to a legacy project
Brian Marick has a stellar post that contains his talking points for bringing Agile into a legacy code base that really needs some cleaning up.
Go ahead and read it now… I’ll wait.
His points are very valid and well stated. Specific points that rang true for me were having the programmers read Working Effectively with Legacy Code. I recently purchased this book and have started reading it. The concepts presented in there are very valuable and easy to implement.
Ron Jeffries’s analogy is completely on par. There are many times that Ive looked at some projects and thought to myself, “how will I ever get sufficient test coverage on this monster?”. The trick is to not do everything at once, but rather each time you visit the code, leave it a little nicer than you found it.
How many unit tests are enough?
This is a question that I think about frequently. When writing unit tests, it can be very easy to come up with a virtually infinite number ways to test code that might cause it to break. A general rule of thumb is that you will have as many lines of code in your unit tests as you do in your project itself (http://c2.com/cgi/wiki?ProductionCodeVsUnitTestsRatio). Given this, we need to make sure that we are testing the right things.
The first area to look at that will increase the quality of your unit tests is fairly obvious. Is the result correct? How do you know that the code ran correctly?
The next thing to increase the quality of your unit tests are boundary conditions. Boundary conditions are things that could happen around the edges of the code. Take for example this class:
public class Calculator { public int Divide(int dividend, int divisor) { return dividend / divisor; } } public class CalculatorFixture { [Test] public void TestDivide() { Calculator calc = new Calculator(); int result = calc.Divide(4, 2); Assert.AreEqual(2, result); } }
At this point, we have a passing test, and we may be tempted to call the method complete and move on to our next piece of functionality. However, if we think about boundary conditions, you may think: What if I pass 0 as the divisor to the Divide method? This will make you think a little bit more about how you expect your interface to work (ie: should this throw the DivideByZeroException from the framework, or would you prefer to wrap this up in your own custom exception?). At this point, you can see another test creeping forward (checking for the correct exception).
The Pragmatic Programmers have a great book (Pragmatic Unit Testing in C#) that has a few chapters dedicated on what should be tested. Chapter 5 deals specifically with boundary conditions, introducing the CORRECT acronym:
- Conformance – Does the value conform to an expected format?
- Ordering – Is the set of values ordered or unordered as appropriate?
- Range – Is the value within reasonable minimum and maximum values?
- Reference – Does the code reference anything external that isnt under the direct control of the code itself?
- Existence – Does the value exist (ie: is non-null, non-zero, etc)?
- Cardinality – Are there exactly enough values?
- Time – Is everything happening in order? At the right time? In time?
To get back to the original question posed by this article: How many tests are enough? I think the answer to this is that once you have tested everything that could go wrong and you have achieved 100% code coverage by your unit tests you have sufficient unit tests.
When you first start with Test Driven Development, or even creating unit tests for legacy code you may notice that the timeline seems to stretch. This is perfectly normal, and as with anything you’ll get better at it the more you do it.
I like to think of taking a little longer to deliver a product to the QA department in hopes that they dont stumble across stupid bugs that shouldnt have gotten past simple developer testing. Imagine how much more effective your QA department could be if they arent performing tasks such as database validation (that could be accomplished by unit tests). Imagine the increased quality your product could have if the QA department was spending more time in integration and user interface testing.
Comment notification
I’ve added functionality to this website that will allow you to receive an email notification for entries that you comment on. As mentioned yesterday, all comments will be moderated, so the idea behind this is that the notification script will let you know when new comments are approved or the entry itself has changed, rather forcing you to check back consistently.
I want my readers to know that I respect their privacy, and that they should feel completely comfortable in subscribing to entries that they are interested in. I have created a privacy policy that is linked to on every comment form that discusses how your email address will be used.
Comments back online
The lack of reader feedback since I turned comments off a while back has been driving me bonkers. To be honest, I’ve thought about turning comments back on for quite a while, but the thing that was holding me back was that there was no CAPTCHA implementation for MovableType and dynamic publishing (which is what Im using now).
I decided that until I can find someone to do a CAPTCHA implementation that supports the dynamic publishing feature of MovableType, I would turn comments back on. The catch though is that all comments are moderated. They will not post to the site until I manually approve them. I promise though that I will be diligent and approve the comments as quick as possible. Trackbacks are and will continue to remain turned off.
I believe that this is a win-win situation. I’m willing to do the extra work of manually moderating so that mattberther.com can once again be a discussion area that adds value to the developer community. Using the moderation technique, I can also insure that no undesireable content ends up on the site.
Thanks for everyone’s patience while Ive been working through this. Hopefully, this makes the site better for everyone.
Executing a SQL script using ADO.NET
Recently, while creating an installer application, I had the need to execute a sql script while my application was being installed. A while back, I posted a technique that would accomplish this.
The drawback to that technique was that it required the osql.exe on the machine I was installing. In most cases this would work, but I wanted to find a way to run a sql script, without any outside dependencies (other than the .NET framework that my installer would lay down).
This is what I came up with:
public void ExecuteSql(SqlConnection connection, string sqlFile) { string sql = ""; using (FileStream strm = File.OpenRead(sqlFile)) { StreamReader reader = new StreamReader(strm); sql = reader.ReadToEnd(); } Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline); string[] lines = regex.Split(sql); SqlTransaction transaction = connection.BeginTransaction(); using (SqlCommand cmd = connection.CreateCommand()) { cmd.Connection = connection; cmd.Transaction = transaction; foreach (string line in lines) { if (line.Length > 0) { cmd.CommandText = line; cmd.CommandType = CommandType.Text; try { cmd.ExecuteNonQuery(); } catch (SqlException) { transaction.Rollback(); throw; } } } } transaction.Commit(); }
The key here is the regex that looks for a line that starts with ‘GO’. This needs to be done because ADO.NET will throw a SqlException if the script contains ‘GO’. I’m now taking advantage of this technique in my installers, eliminating the osql dependency.
Also, you’ll notice that I make use of ADO.NET’s fantastic transaction capabilities. The SqlTransaction class represents a T-SQL transaction to be made in a SQL Server database.
By using it in the way that I have, if any portion of the sql script fails, the whole thing aborts (see the Rollback call in the exception handler). Since we would be using this to install or update a database, we would want to use this so that we do not leave the database in an unusable state. When all commands in the file successfully have executed, we call Commit on the transaction which sends the transaction through.
Tiger’s Back
I was watching the Masters golf tournament earlier today and saw what is easily the most incredible shot I have *ever* seen. Tiger Woods had hooked his tee shot off to the left and his ball was lying right next to the second cut.
Joseph Jaffe has made what he feels should be the next Nike commercial using footage from Tiger’s next shot. You need to watch this…
There has been a lot of rumbling about Tiger and his lack of major wins lately. His dramatic win today shows the world that he is back…


