|
|
Learning to Use xUnit.Net (3 of 3)Posted by Maria Marcano on 03/10/2011 in xunit.net , unit test , mstest |
I started using xUnit.Net a few weeks ago. My first question was how to do the things I was used to do in other testing frameworks like MSTest or NUnit, especially when using this framework for unit testing and for higher level tests like Selenium RC web tests. Here's how I went about it.
This is the third of a three-part series on this experience.
On my previous post I created a Custom TestCategory Attribute and a custom xUnit.Net TestClassCommand to run tests by category. Now I want to create a Custom RunAfterTestFailed attribute to run a method whenever a test fails.
We have the following test class,
using Xunit;
using Xunit.Extensions;
namespace xUnitCustomizations
{
[CustomTestClassCommand]
public class TestClass
{
[Fact, TestCategory("Unit")]
public void FailedTest1()
{
Assert.True(false);
}
[Fact, TestCategory("Unit")]
public void FailedTest2()
{
throw new InvalidOperationException();
}
[RunAfterTestFailed]
public void TestFailed()
{
Debug.WriteLine("Run this whenever a test fails");
}
}
}
We will use CustomTestClassCommandAttribute and
CustomTestClassCommand, previously created, and make the following
changes to EnumerateTestCommands method since we need a custom command
to be able to handle errors when executing test methods.
public class CustomTestClassCommand : ITestClassCommand
{
.....
#region ITestClassCommand Members
.....
public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
{
string skipReason = MethodUtility.GetSkipReason(testMethod);
if (skipReason != null)
yield return new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), skipReason);
else
foreach (var testCommand in cmd.EnumerateTestCommands(testMethod))
yield return new AfterTestFailedCommand(testCommand);
}
....
#endregion
}
AfterTestFailedCommand will handle calling the execution of the test method and calling the proper method whenever a test fails.
public class AfterTestFailedCommand : DelegatingTestCommand
{
public AfterTestFailedCommand(ITestCommand innerCommand)
: base(innerCommand)
{}
public override MethodResult Execute(object testClass)
{
MethodResult result = null;
Type testClassType = testClass.GetType();
try
{
result = InnerCommand.Execute(testClass);
}
finally
{
if (!(result is PassedResult))
{
foreach (MethodInfo method in testClassType.GetMethods())
foreach (var attr in method.GetCustomAttributes(typeof(RunAfterTestFailedAttribute), true))
method.Invoke(testClass, null);
}
}
return result;
}
}
I liked the extensibility that xUnit.Net provides, even when there isn’t a built in solution for TestCategory and RunAfterTestFailed attributes, I was able to build it by looking at the source code, the unit tests and examples available in CodePlex (the framework has unit tests!). Hope this series of posts was helpful to get you started in migrating from MSTest to xUnit.Net and writing custom extensions for this framework.
TweetBacks (Tweet this post)
Trackback(0)
TrackBack URI for this entryComments (0)
Show/hide comments



