Over a million developers have joined DZone.

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

See how three solutions work together to help your teams have the tools they need to deliver quality software quickly. Brought to you in partnership with CA Technologies

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: 


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.

Discover how TDM Is Essential To Achieving Quality At Speed For Agile, DevOps, And Continuous Delivery. Brought to you in partnership with CA Technologies

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 }}