Note that there are some explanatory texts on larger screens.

plurals
  1. PONondeterminism in Unit Testing
    primarykey
    data
    text
    <p>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.</p> <p>For example, here is a test taken from nUnit's unit tests (EqualsFixture.cs):</p> <pre><code>[Test] public void Int() { int val = 1; int expected = val; int actual = val; Assert.IsTrue(expected == actual); Assert.AreEqual(expected, actual); } </code></pre> <p>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.</p> <p>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.</p> <p>What about randomizing as much input to all unit tests as possible, so that each run has a shot of revealing something new? </p> <p>In the previous example, perhaps:</p> <pre><code>[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); } </code></pre> <p>(If the code expected a string, perhaps a random string known to be valid for the particular function could be used each time)</p> <p>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.</p> <p>Is this useful? Evil? Are there drawbacks to this? Am I completely missing the point of unit testing?</p> <p>Thank you for your thoughts.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload