Conventional Testing for .NET

  • master
  • dev
  • Join the chat at https://gitter.im/fixie/fixie

Test Lifecycle

Defining Test Lifecycle in Custom Conventions

In a custom Convention you can define the rules for test lifecycle, i.e., how often an instance of a test class is instantiated.

The two lifecycle schemes are “per case” and “per class”, and you specify which one to use in the ClassExecution definition in your Convention. (To define additional behaviors for ClassExecution, CaseExecution, and FixtureExecution, see Test Execution.)

ClassExecution.CreateInstancePerCase() will, for each test method within a test class, call the class’s constructor, call the test method, then call the class’s destructor. (See an example xUnit-style Convention that uses CreateInstancePerCase.) This makes the class’s constructor and destructor act as test setup and teardown. This is the default behavior.

using Fixie;

namespace UnitTests
{
    public class UnitTestConvention : Convention
    {
        public UnitTestConvention()
        {
            ClassExecution
                .CreateInstancePerCase();
        }
    }
}

ClassExecution.CreateInstancePerClass() will, for each test class, call the class’s constructor, call each test method within the class, then call the class’s destructor. (See an example NUnit-style Convention that uses CreateInstancePerClass.) This makes the class’s constructor and destructor act as test class setup and teardown.

using Fixie;

namespace UnitTests
{
    public class UnitTestConvention : Convention
    {
        public UnitTestConvention()
        {
            ClassExecution
                .CreateInstancePerClass();
        }
    }
}

Using a Factory Method Instead of the Default Constructor

ClassExecution.UsingFactory() allows you to specify a method (or a Func<Type, object>) that will create the test class instance instead of calling the class’s default constructor. (See an example Convention using a factory to fetch instances from an IoC container.)

Other variations on what to do before and after test classes and cases can be defined with Wrap behaviors, described in Test Execution.