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.

7 June 2007 | TDD, .NET Tests | Comments | Print This Post

5 Responses to “Extending Test Frameworks”

  1. 1 Colin Nash 8 June 2007 @ 11:41 am

    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. 2 Eli Lopian 8 June 2007 @ 2:04 pm

    Colin,
    It should work but I have never tried it.
    I know that it doesn’t work (yet) for ASP tests.

  3. 3 Eli Lopian 9 June 2007 @ 9:59 pm

    Colin, Works like a charm with VSTS data driven tests.

  4. 4 Jonas 10 June 2007 @ 5:10 am

    Quick question.

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

    TransactionScope scope =
    new TransactionScope(TransactionScopeOption.RequiresNew);
    try
    {
    // run the test
    return base.CallDecoratedMethod();
    }
    finally
    {
    scope.Dispose();
    }

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

  5. 5 Eli Lopian 10 June 2007 @ 9:15 am

    Jonas,
    You are correct, ‘using’ would be a better choice.
    I have fixed the post

Leave a Reply

  1.  
  2.  
  3.  
Search Development and Integrity Management by Eli Lopian

Navigation

Recent Posts

Categories

Archives

Managment