Saturday, December 02, 2006

xUnit: Absence of Mocks and Stubs

In September of 2004 I met James Newkirk at the Microsoft Patterns and Practice Summit in Reston Virginia. If you don't know James, among other accomplishments he was one of the creators of NUnit. At the summit I stopped him to express my appreciation for NUnit. A conversation ensued in which he caught me off guard with the question: What is missing from NUnit?

Well, it's been over two years, but I finally have an answer: Mocks and Stubs

As long as I've needed them, Mocks have been available as additional libraries. When I was using NUnit I used NMock and when using Test::Unit I use Mocha. But, why are these libraries not simply included in xUnit frameworks?

In 2004 I was also introduced to Behavioral Testing as a formal concept. I had been using mocks for a bit; however, it was wile reading Mocks Aren't Stubs by Martin Fowler when the difference between state and behavioral testing became clear. Martin concludes the write up with the following question and answer:
So which style is the best?

I find this a difficult question to answer with confidence.
It's been more than two years since Mocks Aren't Stubs was originally published and both behavioral and state based testing still exist. I'm skeptical that either will ever be declared better for all cases.

I can't imagine having to choose either behavioral or state based testing exclusively. Both have their pros and cons and, I believe, should be used accordingly. So why then do xUnit implementations continue to ignore support for behavioral testing?

Given that the tests we write as developers have matured, I believe it's fair to expect our xUnit frameworks to evolve with our needs.

10 comments:

  1. Anonymous5:23 PM

    Hi Jay,

    Do you know of any books on newer unit testing techniques? I'm familiar with Mocks, Stubs, BDD and the principles of what sort of things you're supposed to test *for*. But I'd like to make the action of my Test Fu better.

    Understand what I'm talking about?

    ReplyDelete
  2. Anonymous3:05 AM

    While I agree with your take on behavioral and state-based testing, I don't think it's so bad to have xUnit decoupled from mock frameworks. Users benefit from being able to mix and match to taste, and the loosely coupled nature breeds greater innovation and competition in each space.

    Also, NUnit has included a lightweight mock framework since v2.2. It's not intended to replace a full mock framework, though, per the release notes [http://www.nunit.org/index.php?p=releaseNotes&r=2.2]

    Cheers from NYC,
    Luke

    ReplyDelete
  3. Anonymous5:26 AM

    Did you know that RSpec has mocks and stubs built-in?

    ReplyDelete
  4. Anonymous9:22 AM

    Hello Dan,
    I do understand what you are asking for; unfortunately, I'm not sure there's much out there in the way of information. Here's a few ideas that might get you started:

    1. I've written before about my TDD observations.

    2. I've been told by a reliable resource that Dave Astels book on TDD is good.

    3. Attend conferences and find ThoughtWorks employees (aka ThoughtWorkers). They are generally passionate about testing and will have opinions they would love to give you.

    4. Get on the RSpec developers mailing list and throw questions at some great testing minds.

    That's about all I can think of right now, and it's admittedly weak. Zak Tamsen and I are putting together a paper for a new book that may help in this area also. I'll post an update about that as it progresses.

    ReplyDelete
  5. Anonymous9:32 AM

    Hello Luke,
    I believe that decoupling code increases complexity. Some times the increased complexity warrants the decision, but sometimes it doesn't.

    While saying that you can mix and match sounds plausable, when have you ever mixed xUnit frameworks? In .net, NUnit is almost your only choince. In Ruby, Test::Unit is by far the majority.

    I do enjoy competition that breeds better software, but why haven't the xUnit frameworks even entered the competition? This, in my opinion, hurts the testing space because it encourages people new to testing to do state based testing exclusively. Andy, if behavioral based testing were more mainstream it would evolve at a much faster pace.

    NUnit does contain a lightweight mock framework. But, it's not intended for prime time. In fact, I believe it was originally included for their tests, not for our use.

    ReplyDelete
  6. Anonymous9:35 AM

    Aslak,

    Is that really a question or a shameless plug. ;)

    I see from the mailing list that the mock and stub aspect of RSpec is maturing rapidly. I want to give it a look in the near future, but if it's not as good as Mocha that's a deal breaker. I'm not sure why you guys didn't just use Mocha....

    ReplyDelete
  7. Anonymous2:03 AM

    Thanks Jay,

    I asked precisely because you had explored the subject somewhat before and figured you'd have some resources that you draw upon. I'm just feeling like I need to ninja things up compared to what I've been doing.

    I'd love to get around to going to one of the cooler conferences, alas they all seem to correlate with times I can't really leave school. Maybe next year when I'm getting paid and have discretionary vacation.

    ReplyDelete
  8. Anonymous2:25 AM

    Oh, uh, PS, that second link is broken.

    ReplyDelete
  9. Anonymous6:29 AM

    Sorry, this should be the link to amazon for Dave's book.

    http://www.amazon.com/Test-Driven-Development-Practical-Guide/dp/0131016490

    ReplyDelete
  10. Jay,

    For this reason (amongst others), for the last few months I've been writing Instinct, a BDD framework for Java. It evolved from custom hacks written on top of JUnit in several projects I've worked on.

    The mocking part is not mature at all (we're working on that ATM), but it is quite suitable as a straight JUnit replacement.

    Cheers,
    Tom

    ReplyDelete

Note: Only a member of this blog may post a comment.