Pex - Automated Whitebox Testing for .NET : The Hello World of Pex

The "Hello World" of Pex


Since Pex finds inputs relevant to the tested program, we can use Pex to generate the famous Hello World! string. We assume that you have created a C# MSTest Test Project and added a reference to Microsoft.Pex.Framework. If you are using another test framework, create a C# class library and refer to the test framework documentation on how to set up the project.

The example below creates a couple of constraints on the parameter called value so that Pex will generate the desired string.

using System;
using Microsoft.Pex.Framework; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
 
[TestClass]
public partial class HelloWorldTest {
    [PexMethod]
    public void HelloWorld([PexAssumeNotNull]string value) {
        if (value.StartsWith("Hello")
            && value.EndsWith("World!")
            && value.Contains(" "))
            throw new Exception("found it!");
    }
}

Once compiled and executed, Pex generates a set of tests such as the following:

Test Numbervalue
1""
2"\0\0\0\0\0\0"
3"Hellol"
4"HellolWorld!"
5"Hello World!"

We have a winner. In fact, the generated tests code should include a test like the following:

[TestMethod()]
[PexRaisedException(typeof(Exception))]
public void HelloWorld05()
{
    this.HelloWorld("Hello World!");
}

See Also

TestClassAttribute, PexClassAttribute, PexMethodAttribute
(c) Microsoft Corporation. All rights reserved. pex Wiki Documentation 0.93.50813.0