Extending Test Frameworks

One really nice feature of TypeMock.NET 4.0 is the ability to extend any/all testing frameworks.

Each test framework has (or lacks) its own extension mechanism, making it hard add extension to all the frameworks. With TypeMock we can now write an new test decorator that will work for all frameworks, including vsTest and nunit, and all runner-tools including TestDriven.NET and ReSharper.

Actually, It is possible to decorate and extend ANY method! As long as it is run with TypeMock enabled. So it is now really simple to implement Roys Enterprise Services to do database testing, and Ian’s Even simpler way for ALL testing frameworks, without rewriting the test framework, extending base classes or forcing ALL tests in fixture to rollback.

We will be able to write the following test:

[Test,Rollback] public void Insert() { //Do some inserts into the DB here and automatically roll back }

Here is the code that does it
[UPDATE: Fixed according to Jonas comment]

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class RollbackAttribute : DecoratorAttribute { public override object Execute() { using( new TransactionScope(TransactionScopeOption.RequiresNew)) { // run the test return base.CallDecoratedMethod(); } } }

It is possible to rollback on all tests by decorating the class

[TestFixture,Rollback] public class DatabaseTests { //Do many DB tests here and automatically roll back }

We have implemented the [VerifyMocks] and [ClearMocks] using this technique.

  1. This looks really useful. I noticed you are calling the test using base.CallDecoratedMethod()and was wondering if this in compatible with VSTS data driven tests?

  2. Quick question.

    Why did you write the code with an explicit try finally instead of a using statement?

    TransactionScope scope =
    new TransactionScope(TransactionScopeOption.RequiresNew);
    // run the test
    return base.CallDecoratedMethod();

    using( new TransactionScope(TransactionScopeOption.RequiresNew) )
    // run the test
    return base.CallDecoratedMethod();

Add Comment

Required fields are marked *. Your email address will not be published.