JUnit, NUnit, Assertions

Started by
9 comments, last by Cygon 15 years, 4 months ago
C# I am learning about the basics of test-driven development frameworks, such as JUnit and NUnit, and am trying to use NUnit in conjunction with a small .NET game I am writing. I could not find the answer to my question in any of the online documentation, or using conventional search engines. Take the Assert.AreEqual() method. It "asserts" that its two params are equal. If false, you should see it fail in the NUnit GUI Runner application. Else, you should get nothing but *sweeeeet green*. But what if I want that failed assertion to throw an exception? My original code was something to the tune of: if(!Assert.Equal(x, 3)) { throw new Exception("Fatal Error, Drive B"); } But then I noticed that all Assert's methods (AreEqual, IsNull, etc.) return void. Return void! What sort of non-sense is this! Then I got to thinking: Why would they choose to return void, hmmmm, perhaps throwing exceptions somehow violates the philosophy that TDD sits upon.... Does anybody have any input on this? How could I throw an exception on a failed test, or should I not even be trying to do that? And why?
Advertisement
Quote:Original post by plywood
if(!Assert.Equal(x, 3)) { throw new Exception("Fatal Error, Drive B"); }

Assertions are a debugging tool. They mean "This condition must always be true. If it isn't there is a bug in my program." From your exception message, it seems you just want to catch a runtime error:
if (x != 3) { throw new Exception("Fatal Error, Drive B"); }


Quote:Original post by plywood
How could I throw an exception on a failed test

I don't understand why you want to throw your own exceptions when JUnit already does it for you.
Theory of Mind

Theory of Mind is a concept in classic psychology that suggests young children cannot understand the notion that adults (or anybody other than themselves) don't share the same mind as they do.

For example, your son goes to his first day of kindergarten, and so you don't know his teacher, or any of his classmates yet. When he comes home, he is obviously upset, and when you ask him what's wrong he says "Amy bit me." According to this theory, your small child doesn't understand that you have a separate mind from him, and that you don't *have a clue* who Amy is. She could be another classmate, his teacher, a janitor, etc. An adult would have responded "There is this girl in my class, Amy, and she bit me today." because adults have a Theory of Mind.

I believe this theory holds true to working professionals in academia and in the general "IT industry", much as it holds true to tiny children.

For instance, let us turn to NUnit. If you google it the two most sought-after links will be the official NUnit homepage, and its wikipedia article. After pouring through the available online explanations of what this "testing framework" is, what it does and how it works, I was not aware that it throws exceptions for you. This is because the NUnit project and homepage and wiki article were all written by software engineers with no Theory of Mind. They built NUnit. They knew how it worked when they wrote the docs. But then something was "lost in translation" when it came time to actually put pen to paper and explain their product to the outside world.

So seriously folks, the next time any of you out there are writing any technical literature, have a Theory of Mind. It will make whatever it is that your documenting that much more popular and widely-comprehended, not just by those who are already "in the know."
What would be the point in a testing framework that did not throw exceptions, how would you know if anything went wrong. The software engineers could have told you how to follow the links in wiki or turn the pages of the book but i think its probably fair to make some assumptions about the knowledge your audience might have, it saves time when 'professionals' are talking to each other.

In fairness i kind of know where you are coming from with this and it becomes all the more apparent the more complex the subject but its a necessity with complex subject to matter if you want to communicate anything within a reasonable framework ie. not a completely over the top verbose manual that runs in to 10,000's of pages explaining every conceiveable detail just to accomodate the lowest common 'state of mind'.

:)
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.
Quote:Original post by plywood
Theory of Mind
You can come up with elaborate theories about why everyone other than you is such an idiot, or you can suck it up and learn to absorb new concepts efficiently. One important tool for that is building up a domain vocabulary, using more introductory material. For instance, see the Wikipedia page on unit testing. Alternatively, a good book on the subject.
Quote:So seriously folks, the next time any of you out there are writing any technical literature, have a Theory of Mind. It will make whatever it is that your documenting that much more popular and widely-comprehended, not just by those who are already "in the know."


No!

Technical literature is for people "in the know".

What's next? Documentation for illiterate?

Those "not in the know" should scale down their goals, and do something more trivial, or study to become "in the know".
Precisely! And I'm sure we all signed up for Elements of Thermodynamics our freshmen year when we thought we might be interested in physics!
Quote:Original post by plywood
Theory of Mind


Am I the only one who thought the OP was just a set up for this "Theory of Mind" rant?

The Quick Start page on the nunit website explains the basic usage to me pretty well. I think the problem is not that the authors do not understand the "Theory of Mind" as you argue, but that you simply had an incorrect notion of how the Assert.AreEqual method was implemented.

If you simply followed the Quick Start guide, you'd have been up and running and never even thought to write "if(!Assert.AreEqual(...))" because it wouldn't have made sense.

It's like complaining you got into a car accident because the driving instructor didn't tell you that the hand brake is only meant to be use while the car is already stopped.
I think the OP simply doesn't understand the difference between a Test (something can never be false) and a runtime error (if this becomes false, take appropriate measurements).
Quote:Original post by plywood
Take the Assert.AreEqual() method. It "asserts" that its two params are equal. If false, you should see it fail in the NUnit GUI Runner application. Else, you should get nothing but *sweeeeet green*.

But what if I want that failed assertion to throw an exception? My original code was something to the tune of:

if(!Assert.Equal(x, 3)) { throw new Exception("Fatal Error, Drive B"); }

But then I noticed that all Assert's methods (AreEqual, IsNull, etc.) return void. Return void! What sort of non-sense is this! Then I got to thinking: Why would they choose to return void, hmmmm, perhaps throwing exceptions somehow violates the philosophy that TDD sits upon....

Does anybody have any input on this? How could I throw an exception on a failed test, or should I not even be trying to do that? And why?


TDD means you test your code with individual test methods that are only responsible for testing specific aspects of your code. It appears you are trying to put these Asserts into your actual production code.

Asserts in production code are something quite different from unit test asserts. If you need an assert in .NET production code, use System.Diagnostics.Debug.Assert() or System.Diagnostics.Trace.Assert().

NUnit.Framework.Assert() returns void because it does throw an exception if the assertion fails!
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.

This topic is closed to new replies.

Advertisement