Adding Custom Attributes

With Typemock Isolator you can extend any testing framework by writing custom attributes that will be executed while the tests run. This will give you full control over the flow of the test and an ability to add functionality to the test framework.

Examples of custom decorators are automatic rollback, timeout, and multiple runs. Typemock Isolator comes with prebuilt decorators for best practice usage.

To write a custom attribute, only execute methods need to be implemented. In this example, the trace message is written to the console before and after the method is run. CallDecoratedMethod is used to activate the original method.

 

Custom attributes cannot be used in ASP.net tests or in generic methods or types.

 The following properties are also defined in the DecoratorAttribute class:

      OriginalMethod: returns a MethodBase object representing the decorated method.

      OriginalContext: holds the instance in which the decorated method was called.

      OriginalParams: holds the parameters passed to the decorated method

 

The following example shows how to write a custom attribute. The attribute will display a trace of the test method's entry and exit.

C#

// An example of how to write new attributes.
// Decorating a method with this attribute will cause a message to be
// written to the console at the start and end of the method execution.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class TraceableAttribute : DecoratorAttribute
{
  // In this execute, we added logic for tracing to a text file before and after activation of the
  // decorated method.
  // Returns results from activating the original invoker on the test (unless overridden) .

  public override object Execute()
  {
    // Trace start of method
    Console.WriteLine("Entering Method: " + OriginalContext.GetType() + " -- " + OriginalMethod.ToString());
    try
    {
      // Execute the original method
      return base.CallDecoratedMethod();
    }
    finally
    {
      // Trace end of method
      Console.WriteLine("Exiting Method: " + OriginalContext.GetType() + " -- " + OriginalMethod.ToString());
    }

}

// This will cause the attribute to be applied to all methods and not just test
// Parameter "methodBase" - Method being executed
// Returns true to decorate the code
// Default action is to execute only methods that have a [Test] or [TestMethod] attribute
  protected override bool DecorateMethodWhenAttributeIsClass(MethodBase methodBase)
  {
    return true;
  }
}

VB

' An example of how to write new attributes.
' Decorating a method with this attribute will cause a message to be
' written to the console at the start and  end of the method execution.

Public Class TraceableAttribute
  Inherits DecoratorAttribute
  ' In this execute, we added logic for tracing to a text file before and after activation of the decorated method.
  ' Returns the results from activating the original invoker on the test (unless overridden).

  Public Overrides Function Execute() As Object
    Console.WriteLine(String.Format("Entering Method: {0} -- {1}", OriginalContext.GetType(), OriginalMethod.ToString()))
    Try
      Return CallDecoratedMethod()
    Finally
      Console.WriteLine(String.Format("Exiting Method: {0} -- {1}", OriginalContext.GetType(), OriginalMethod.ToString()))
    End Try
  End Function

  ' This will cause the attribute to be applied to all methods and not just to test methods.
  ' By default, attributes decorating a class will only be applied to test methods.
  Protected Overrides Function DecorateMethodWhenAttributeIsClass(ByVal methodBase As MethodBase) As Boolean
    Return True
  End Function
End Class