Home > .NET, .NET User Groups, Test Driven Development > TDD Teaser #1 – Do you know the answer?

TDD Teaser #1 – Do you know the answer?

I recentlyconcluded a TDD with ASP.NET presentation in Cleveland with several TDD Teaser problems for the audience. The idea proved to be a lot of fun so I decided to repost the problems on my blog.

The Rules:

  1. Prize (a Thycotic keyring light) to be mailed to the firstcomment with the correct answer on this blog post withvalid contact information.
  2. My definition of the problem is the correct one. 🙂 Other answersmight be right butwon’t win the prize.
  3. I will post the answer within a few days.

What is wrong with the following code?

1using NUnit.Framework; 
3namespace UnitTests.BadCode
5 [TestFixture]
6 public class Problem1
7 {
8 [Test]
9 public virtual void TestSomething()
10 {
11 MyClass a = new MyClass();
12 TestAmount(a);
13 }
15 public virtual void TestAmount(MyClass a)
16 {
17 Assert.AreEqual(2500, a.Amount);
18 }
19 }

Winner: Roy Osherove

Answer & Discussion (click and drag your mouse to see the answer)

Kirk – you are right. It doesn’t compile the return type on TestAmount should have been void (Note to self: make sure your teaser compiles!). Unfortunately that wasn’t the one I was looking for. I have changed the return type to void as it should have been.

Dean – virtual methods and inheritance can be quite useful in tests – you should refactor your tests as judiciously as your production code and this may result in inherited tests. I often use inheritance when writing validity checks for different scenarios. I agree with your comments about TestAmount being poorly designed.

NUnit, for backwards compatibility to 1.0, will identify TestAmount as a test due to the “Test” prefix on the method name. This results in an error since Tests may not have parameters. Why did NUnit 1.0 look for the Test prefix rather than use attributes? – because it was ported from JUnit which uses naming conventions since Java doesn’t have attributes.

Well done Roy! (your keyring light is on its way)

Thanks to everyone for participating! 🙂

  1. Kirk Marple
    October 7, 2004 at 12:50 am

    TestAmount is supposed to be returning a string but all it does is Assert. it shouldn’t even compile.


  2. http://
    October 7, 2004 at 1:43 am

    I can see two things at first glance:

    First is the fact that all methods are virtual. The class should be probably be sealed to start with – test cases are not meant to be extended in that manner.

    Second, you’ve separated your test case into two methods. What happens if you change one method and not the other? At the very least your TestAmount method should have an “expected” parameter which it uses instead of the hard-coded 2500. The TestSomething method can pass in the expected value when it calls TestAmount. What I’m trying to say here is that TestSomething knows what the state of “a” should be, but TestAmount does not necessarily know.


  3. Roy Osherove
    October 7, 2004 at 2:37 am

    TestAmount is public and begins with “Test”. because of backwards compatibility issues Nunit will run that method as well thinking it is a test. However, it accepts parameters which means it will won’t run and you’ll get a nice error.

  4. Roy Osherove
    October 7, 2004 at 2:38 am

    oh yeah – Roy at osherove dot com is my email 🙂

  5. Roy Osherove
    October 7, 2004 at 2:41 am

    other ideas:
    if you *are* going to use a separate verification method, it should at least be named “VerifyXX” and be private. You also didint’ specify an error message on failure (the third missing parameter in the assert) and that’s usually a no-no.

  6. http://
    October 7, 2004 at 4:02 am

    Assert.AreEqual(2500, a.Amount) will never run since the method does not have the [Test] attribute?

  7. Matt Berther
    October 7, 2004 at 4:02 am

    Roy: I thought that that was the issue as well, but after thinking about it, I think that the backwards compatability with methods that begin with “Test” also requires the method to return void and accept no parameters.

  8. Haacked
    October 7, 2004 at 4:28 am

    Kirk is correct in that it won’t compile.

    Assuming you correct that by changing TestAmount to return void, you end up with two test methods in NUnit.

    One that correctly runs the assertion and one that displays the method TestAmount and says it must not have parameters.


  9. Roy Osherove
    October 7, 2004 at 9:16 am

    dang – it won’t compile.

  10. Samir
    April 8, 2007 at 2:26 am

    http://8620f5c96361d9bb2976aa017a6abddb-t.k49ols9.info 8620f5c96361d9bb2976aa017a6abddb [url]http://8620f5c96361d9bb2976aa017a6abddb-b1.k49ols9.info[/url] [url=http://8620f5c96361d9bb2976aa017a6abddb-b2.k49ols9.info]8620f5c96361d9bb2976aa017a6abddb[/url] [u]http://8620f5c96361d9bb2976aa017a6abddb-b3.k49ols9.info[/u] 00c1f002ef240e804156597842785ea4

  11. Hassan
    April 16, 2007 at 4:12 am

    http://076e49ab98b667676d0058829f382fb9-t.k90u0h.info 076e49ab98b667676d0058829f382fb9 [url]http://076e49ab98b667676d0058829f382fb9-b1.k90u0h.info[/url] [url=http://076e49ab98b667676d0058829f382fb9-b2.k90u0h.info]076e49ab98b667676d0058829f382fb9[/url] [u]http://076e49ab98b667676d0058829f382fb9-b3.k90u0h.info[/u] 86b4eaaaf8201e29470f084ce1d2d695

  12. Panayotis
    November 5, 2007 at 7:37 pm


  13. Miltos
    November 8, 2007 at 7:39 pm


  14. Marinos
    November 9, 2007 at 11:39 am


  15. Lambro
    November 9, 2007 at 2:19 pm


  16. Kleanthe
    November 12, 2007 at 11:03 pm


  17. Zacharias
    November 13, 2007 at 12:14 pm


  18. Loukianos
    November 13, 2007 at 3:39 pm


  19. Stavros
    November 13, 2007 at 6:56 pm


  20. Apostolos
    November 13, 2007 at 7:32 pm


  21. Philippos
    November 13, 2007 at 7:50 pm


  22. Petros
    November 13, 2007 at 11:28 pm


  23. Yannas
    November 14, 2007 at 1:03 am


  24. Achilles
    November 14, 2007 at 6:58 am


  25. Herakles
    November 14, 2007 at 7:27 am


  26. Spiridon
    November 15, 2007 at 3:42 pm

    Sorry 😦

  27. Ivan
    November 16, 2007 at 10:46 am


  28. Ivan
    November 17, 2007 at 1:07 am


  29. Spiro
    November 17, 2007 at 7:43 am


  30. Pantelis
    November 19, 2007 at 12:42 am


  31. Kalinikos
    November 20, 2007 at 9:04 am


  32. Metrophanes
    November 22, 2007 at 2:18 am


  33. Ivan
    November 22, 2007 at 2:22 am


  34. Cosmo
    November 24, 2007 at 1:30 am


  35. Euaggelos
    December 1, 2007 at 10:45 am


  36. Charilaos
    December 9, 2007 at 7:51 pm


  37. Ivan
    December 16, 2007 at 11:45 am


  38. Ivan
    December 21, 2007 at 1:49 pm


  39. Kristion
    December 21, 2007 at 4:42 pm


  40. Ivan
    December 22, 2007 at 5:18 pm


  41. Vaggelis
    January 2, 2008 at 8:15 am


  42. Thrasyvoulos
    January 10, 2008 at 1:21 am


  43. Makis
    January 21, 2008 at 2:21 pm


  44. Stathis
    February 8, 2008 at 4:29 am


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: