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.
5 Comments to “Extending Test Frameworks”
Recent Posts
- Product Status Peek – 2011
- Thanks Roy
- Typemock starts 2011 in a new location
- Agile Demos Smells
- I want loud disputes in our meetings
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- January 2011
- December 2010
- October 2010
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006

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?
Colin,
It should work but I have never tried it.
I know that it doesn’t work (yet) for ASP tests.
Colin, Works like a charm with VSTS data driven tests.
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();
}
Jonas,
You are correct, ‘using’ would be a better choice.
I have fixed the post