New Tools: XUnit.Net

27. December 2009 Uncategorized 0

For the first 6 years of my professional developer life, I worked in a group that did twice yearly releases. Typically one in April and one in November. To do this, we had about 4 months of “open development.” Small bugs were fixed, and specs and designs were done during this time. We then had about a month of locked down access, where changes needed to be approved by team leads before they could be committed. We then had a 2-3 week period of regression testing. We had a good sized group of developers, about 20, and had a large internal user base (600 or so). So we had a good group of people to test before we released our product to the “real world.”

Three years ago, I moved to a company where I’m the only Windows developer. We’ve had between 3-5 web guys, but so far, I’m it for Windows. Add to this fact, my internal user base is now about 6 people, who are extremely busy answering phones and making sales, and so have little time to help do regression testing (or any testing.) Out of this new experience, I learned about unit testing, and NUnit in particular. I’ve been using NUnit for about 3 years now, and have been fairly pleased with it. But with converting It’s Greek To Me from VB6 to .Net, I thought I’d take the chance to see what else is out there.

I stumbled across XUnit.Net I’ve used it for all of my testing needs on this project. So far, I’ve found it to be an adequate replacement for NUnit. I’m sure “adequate” isn’t quite what the boys over at XUnit would like to hear, but let me also say that for what I’m doing right now, I don’t have a lot of advanced testing needs. It’s more on the option of “If I pass option A to the function, block A gets called.”

There are two things that I like a lot about it. First, the way it handles exceptions.

FlashCard fc = new FlashCard();
            Assert.Throws(
                delegate
                {
                    fc.GetParadigms(null);
                });

This block of code simply asserts that calling my GetParadigms method with a null parameter will cause a null exception to be thrown. NUnit handles this as well, but the XUnit code appears to be cleaner, to my eyes.

The second thing that I really like is the [PropertyData] attribute. With this attribute, I can create a property and have several items get returned, one for each test. For example:

public static IEnumerable Participles
        {
            get
            {
                yield return new object[] { new ParticpleParadigmOptions { IsActive = true }, 9 };
                yield return new object[] { new ParticpleParadigmOptions { IsPassive = true }, 9 };
            }
        }

I can then take this property and use it on a test, like this:

[Theory]
        [PropertyData("Participles")]
        public void GetParticiples(ParticpleParadigmOptions part, int count)
        {
            FlashCard fc = new FlashCard();
            Assert.Equal(count, fc.GetParticiples(part).Count());
        }

The first value passed in, is my participle options. The second, is the number of items I expect to be returned from the GetParticiples() method call.

In this case, the first test will verify that there are 9 active participles. The second test will verify that there are 9 passive participles.

The only thing I don’t like right now, is the lack of runner inside VS for XUnit. Or, perhaps I should say, the lack of free, robust runner. Tests can be run via TestDriven.Net, however I don’t have a license. Perhaps I’m just spoiled at my full-time job. As it stands, I’ve just made the XUnit console run after each successful build. The results are pasted into the output window. It’s not a super sexy GUI and it doesn’t allow for debugging tests, but right now I’m doing fine without those. If they ever become “mission critical” then maybe I’ll buy TestDriven.net