Welcome to MSDN Blogs Sign in | Join | Help

Testing Against Non-Determinism

Although computers tend to be rather deterministic in nature, you will sometimes have to deal with concepts that are de-facto non-deterministic. These include, but are not limited to:

  • Random numbers. These are typically generated by a random number generator (such as System.Random).
  • Guids. These are generated by a complex algorithm that utilizes the current system time, system properties, etc. Although not a pseudo-random number in itself (it's not designed to follow any particular stochastic distribution), it's virtually impossible to predict the value of a new Guid before its creation.
  • Current system time. You never know exactly when your code executes.

When testing code, non-determinism is undesirable, since one of the main principles of automated testing is that test results are reproducable; if you run a unit test suite a number of times without changing the code, the outcomes should all be identical. If your test target contains non-deterministic logic, you may find this principle difficult to uphold. In a small series of postings, I'll provide a few pointers on how to deal with this challenge.

Each post will cover a particular type of non-determinism, and provide examples on how to handle it in unit tests:

  1. Testing Against Randomness
  2. Testing Against Guids
  3. Testing Against The Current Time
  4. Testing Against The Passage of Time
Published Friday, May 11, 2007 9:04 PM by ploeh
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Testing Against Randomness

Friday, May 11, 2007 3:22 PM by ploeh blog

This is the first in a small series of posts about testing against non-determinism. In this installation,

# Testing Against Guids

Saturday, May 12, 2007 2:35 PM by ploeh blog

This is the second in a small series of posts about testing against non-determinism. In this installation,

# Testing Against The Current Time

Saturday, May 12, 2007 2:42 PM by ploeh blog

This is the third in a small series of posts about testing against non-determinism. In this installation,

# Testing Against The Current Time

Saturday, May 12, 2007 2:42 PM by ploeh blog

This is the third in a small series of posts about testing against non-determinism. In this installation,

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker