Pex - Automated Whitebox Testing for .NET : SetUp And TearDown (Exploration Life Cycle)

SetUp And TearDown (Exploration Life Cycle)


As part of the integration with test frameworks, Pex support running setup and teardown methods. Setup and teardown methods are used to refactor common code outside of the test/fixture (this is a feature supported by most unit test frameworks).

[TestClass]
public class MyFixture
{
    [TestInitialize] public void SetupStuffForMyTests() {...}
    [TestCleanup] public void SetupStuffForMyTests() {...}
}

The availability of setup and teardown methods depends on your test framework

Life Cycle

The following table gives the guaranteed execution sequence that Pex will apply. A couple things are worth precising:

  • assembly, fixture and test level setup and teardown methods are always optional.
  • assembly and fixture level setup and teardown methods will run unmonitored. This means that no constraints will be gathered when they are running.

  • assembly setup (optional - unmonitored)
    • if failed, the rest of the execution is skipped
    • fixture setup (optional - unmonitored)
      • if failed, fixture execution is skipped
      • for each exploration,
        • default constructor (always - monitored)
        • Setup (optional - monitored)
          • if failed, exploration execution is skipped
        • exploration (monitored)
        • TearDown (optional - unmonitored)
          • if failed, exploration is marked as failed
        • Dispose (optional - unmonitored)
    • fixture teardown (optional - unmonitored)
  • assembly teardown (optional - unmonitored)

Guarantees


Unless the process crashes catastrophically,

  • assembly teardown is always executed,
  • fixture teardown is always executed,
  • teardown and/or dispose is always executed if the fixture instance constructor returned successfully
  • the same fixture instance will be used for the setup and teardown of explorations
  • there is no guarantee that the same fixture instance will be used for the fixture setup/teardown. In fact, Pex will create a new instance on each test.

Example


namespace MyTests
{
    using Microsoft.Pex.Framework;
    using MbUnit.Framework;
 
    [assembly: AssemblyCleanup(typeof(MyAssemblySetup))]
 
    public static class MyAssemblySetup {
         [SetUp]
         public static void SetUp() {
             Console.WriteLine("assembly setup");
         }   
    }
 
    [TestFixture]
    public partial class MyTests {
        [TestFixtureSetUp]
        public void FixtureSetUp() {
             Console.WriteLine("some fixture wide setup here");
        }
 
        [TestFixtureTearDown]
        public void FixtureTearDown() {
             Console.WriteLine("some fixture wide teardown here");
        }
 
        [SetUp]
        public void SetUp() {
             Console.WriteLine("monitored setup");
        }
      
        [PexMethod]
        public void MyTest(int i ) {...}
 
        [TearDown]
        public void TearDown() {
             Console.WriteLine("Clean up");
        }
    }
}

The above example will result in the following execution trace:
MyAssemblySetup.SetUp
MyTests.FixtureSetUp
MyTests.SetUp
MyTests.MyTest
MyTests.TearDown
...
MyTests.SetUp
MyTests.MyTest
MyTests.TearDown
MyTests.FixtureTearDown

See Also

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