Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Writing Repeatable Random Tests

DZone's Guide to

Writing Repeatable Random Tests

A brief tutorial on how to write random, but repeatable, tests using XUnit so you know exactly what's failing.

· Agile Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

Testing our software is something that we take very serious. And in some cases, we want to go beyond testing stuff that we know. We want to test random stuff. For example, if we add 10,000 documents, then remove every 17th, what happens? Are there any differences in behavior, performance, etc?

It is easy to do random stuff, of course. But that leads to an interesting case. As long as the tests are passing, you can pat yourself on the back: “We have done well, and everything works as it should.”

But when something fails…well, the only thing that you know is that something did fail. You don’t have a way to reproduce this. Because the test is…random.

In order to handle that, we wrote the following code:

 [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
 public class InlineDataWithRandomSeed : DataAttribute
 {

     public InlineDataWithRandomSeed(params object[] dataValues)
     {
         this.DataValues = dataValues ?? new object[] {null};
     }

     public object[] DataValues { get; set; }

     public override IEnumerable<object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes)
     {
         var objects = new object[DataValues.Length+1];
         Array.Copy(DataValues,0,objects,0, DataValues.Length);
         objects[DataValues.Length] = Environment.TickCount;
         yield return objects;

     }
 }


This is using XUnit, which gives us the ability to add a seed to the test. Let us see how the test looks:

Image title

And this is what it looks like when it runs: 

image



When we have a failure, we know what the seed is, and we can run the test with that seed, and see what exactly happened there.

Do you pay to use your database? What if your database paid you? Learn more with RavenDB.

Topics:
testing ,agile

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}