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).

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)


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.


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

The above example will result in the following execution trace:

See Also

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