views:

149

answers:

5

It seems that in many unit tests, the values that parameterize the test are either baked in to the test themselves, or declared in a predetermined way.

For example, here is a test taken from nUnit's unit tests (EqualsFixture.cs):

[Test]
public void Int() 
{
    int val = 1;
    int expected = val;
    int actual = val;

    Assert.IsTrue(expected == actual);
    Assert.AreEqual(expected, actual);
}

This has the advantage of being deterministic; if you run the test once, and it fails, it will continue to fail until the code is fixed. However, you end up only testing a limited set of values.

I can't help but feel like this is a waste, though; the exact same test is probably run with the exact same parameters hundreds if not thousands of times across the life of a project.

What about randomizing as much input to all unit tests as possible, so that each run has a shot of revealing something new?

In the previous example, perhaps:

[Test]
public void Int() 
{
    Random rnd = new Random();
    int val = rnd.Next();
    int expected = val;
    int actual = val;
    Console.WriteLine("val is {0}", val);
    Assert.IsTrue(expected == actual);
    Assert.AreEqual(expected, actual);
}

(If the code expected a string, perhaps a random string known to be valid for the particular function could be used each time)

The benefit would be that the more times you run a test, the much larger set of possible values you know it can handle correctly.

Is this useful? Evil? Are there drawbacks to this? Am I completely missing the point of unit testing?

Thank you for your thoughts.

+3  A: 

The tests should cover your use cases, no more.

Have a look at PEX.

Preet Sangha
Thanks for the link. Pex looks like a pretty incredible tool. Its take on the answer seems to be: "Make your tests deterministic, but let us help you find the interesting values that trigger different paths of execution, instead of hand-choosing your values for functions."I think this goes a long way towards an answer, but even Pex can't look inside a black box...When you say use cases, how do you define a use case? If a function should work for an infinite set of values for X, how do you choose which?Thank you for your thoughts.
rh
If you function should work with an infinite (what about data size limitations) you need to define the finiteness of the problem. I would hazard a guess that there is more determinism than you first see.
Preet Sangha
+6  A: 

You want your unit tests to be repeatable so that they will always behave in the same way unless the code changes. Then, if the code changes and causes the unit test to fail, you can fix the code and the unit test has served its purpose. Futhermore, you know that the code is [probably] fixed when the unit test passes again.

Having random unit tests could find unusual errors, but it shouldn't be necessary. If you know how the code works (compare white box and black box approaches to testing), using random values shouldn't ever show anything that well thought of non-random unit tests would. And I'd hate to be told "run the tests a few times and this error should appear".

David Johnstone
This makes sense, and I certainly can understand how predictability is an asset. Some of this goes to the tools in question, though; if the tool was designed to exactly track how a test was parameterized each time, its result could be reproduced. If a testing program supported this, what would be your thoughts?
rh
That is an interesting idea, and as itowlson pointed out, it already exists. I think I prefer the idea of keeping unit testing non-random, but having a separate set of random tests could be useful for some things. You'd probably use the random tests differently as well, for it makes sense to run them multiple times at once. Building the tools for all the different languages for which there exist xUnit tools would be a challenge too.
David Johnstone
+2  A: 

The big problem I have with this is that since this is random, it may not cause a failure until the test has been run 200, 2000, 3000 or more times. If it fails on try 6007 months or years after the case has been written, it essentially means you have had a bug for months or years and never knew.

Instead I think it is much more useful to know your corner cases and test them all specifically. In other words think about what data could break your code and test for it.

mockedobject
A: 

Are you testing Random or the equality operator?

It seems to me that you would choose typical values plus boundary conditions, or brute force the entire Integer set. Simply choosing random integers doesn't help with either approach.

To answer your final question, what is the point of unit testing, my feeling is this: the value of proving repeatable results exceeds the cost of writing the tests.

micahtan
+2  A: 

What you are proposing makes a lot of sense, provided that you do it correctly. You don't necessarily always have to listen only to conventional wisdom that says that you must never have non-determinism in your tests.

What is really important is that each test must always exercise the same code path. That is not quite the same thing.

You can adopt what I call Constrained Non-Determinism in unit testing. This can drive you towards a more Specification-Oriented way of writing tests.

Mark Seemann
Having each test exercise the same code path makes a lot of sense. I liked your post on constrained non-determinism... good food for thought, thanks!
rh