Skip to content

Commit

Permalink
MsTest 2010 support
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Jun 29, 2010
1 parent 36ce712 commit 0b31fac
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Generator/Configuration/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ private void SetUnitTestDefaultsByName(string name)
case "mstest":
GeneratorUnitTestProviderType = typeof(MsTestGeneratorProvider);
break;
case "mstest.2010":
GeneratorUnitTestProviderType = typeof(MsTest2010GeneratorProvider);
break;
default:
GeneratorUnitTestProviderType = null;
break;
Expand Down
1 change: 1 addition & 0 deletions Generator/TechTalk.SpecFlow.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="SpecFlowGenerator.cs" />
<Compile Include="SpecFlowUnitTestConverter.cs" />
<Compile Include="UnitTestProvider\MbUnitTestGeneratorProvider.cs" />
<Compile Include="UnitTestProvider\MsTest2010GeneratorProvider.cs" />
<Compile Include="UnitTestProvider\XUnitTestGeneratorProvider.cs" />
<Compile Include="UnitTestProvider\IUnitTestGeneratorProvider.cs" />
<Compile Include="UnitTestProvider\MsTestGeneratorProvider.cs" />
Expand Down
50 changes: 50 additions & 0 deletions Generator/UnitTestProvider/MsTest2010GeneratorProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;

namespace TechTalk.SpecFlow.Generator.UnitTestProvider
{
public class MsTest2010GeneratorProvider : MsTestGeneratorProvider
{
private const string CATEGORY_ATTR = "Microsoft.VisualStudio.TestTools.UnitTesting.TestCategoryAttribute";

public override void SetTestFixture(CodeTypeDeclaration typeDeclaration, string title, string description)
{
base.SetTestFixture(typeDeclaration, title, description);

featureCategories = null;
}

private IEnumerable<string> featureCategories = null;

public override void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable<string> categories)
{
featureCategories = categories.ToArray();
}

public override void SetTest(CodeMemberMethod memberMethod, string title)
{
base.SetTest(memberMethod, title);
if (featureCategories != null)
SetCategories(memberMethod.CustomAttributes, featureCategories);
}

public override void SetTestCategories(CodeMemberMethod memberMethod, IEnumerable<string> categories)
{
SetCategories(memberMethod.CustomAttributes, categories);
}

private void SetCategories(CodeAttributeDeclarationCollection customAttributes, IEnumerable<string> categories)
{
foreach (var category in categories)
{
customAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(CATEGORY_ATTR),
new CodeAttributeArgument(
new CodePrimitiveExpression(category))));
}
}

}
}
8 changes: 4 additions & 4 deletions Generator/UnitTestProvider/MsTestGeneratorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MsTestGeneratorProvider : IUnitTestGeneratorProvider

private CodeTypeDeclaration currentTestTypeDeclaration = null;

public void SetTestFixture(CodeTypeDeclaration typeDeclaration, string title, string description)
public virtual void SetTestFixture(CodeTypeDeclaration typeDeclaration, string title, string description)
{
typeDeclaration.CustomAttributes.Add(
new CodeAttributeDeclaration(
Expand Down Expand Up @@ -57,12 +57,12 @@ private void SetProperty(CodeAttributeDeclarationCollection customAttributes, st
new CodePrimitiveExpression(value))));
}

public void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable<string> categories)
public virtual void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable<string> categories)
{
//MsTest does not support caregories... :(
}

public void SetTest(CodeMemberMethod memberMethod, string title)
public virtual void SetTest(CodeMemberMethod memberMethod, string title)
{
memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
Expand All @@ -78,7 +78,7 @@ public void SetTest(CodeMemberMethod memberMethod, string title)
SetProperty(memberMethod.CustomAttributes, FEATURE_TITILE_PROPERTY_NAME, featureTitle);
}

public void SetTestCategories(CodeMemberMethod memberMethod, IEnumerable<string> categories)
public virtual void SetTestCategories(CodeMemberMethod memberMethod, IEnumerable<string> categories)
{
//MsTest does not support caregories... :(
}
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Configuration/RuntimeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ private void SetUnitTestDefaultsByName(string name)
case "mstest":
RuntimeUnitTestProviderType = typeof(MsTestRuntimeProvider);
break;
case "mstest.2010":
RuntimeUnitTestProviderType = typeof(MsTest2010RuntimeProvider);
break;
default:
RuntimeUnitTestProviderType = null;
break;
Expand Down
2 changes: 2 additions & 0 deletions Runtime/TechTalk.SpecFlow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
<Compile Include="Tracing\StepDefinitonSkeletonProvider.cs" />
<Compile Include="Tracing\StepFormatter.cs" />
<Compile Include="UnitTestProvider\MbUnitRuntimeProvider.cs" />
<Compile Include="UnitTestProvider\MsTest2010RuntimeProvider.cs" />
<Compile Include="UnitTestProvider\UnitTestRuntimeProviderHelper.cs" />
<Compile Include="UnitTestProvider\XUnitRuntimeProvider.cs" />
<Compile Include="UnitTestProvider\MsTestRuntimeProvider.cs" />
<Compile Include="UnitTestProvider\NUnitRuntimeProvider.cs" />
Expand Down
7 changes: 7 additions & 0 deletions Runtime/UnitTestProvider/MsTest2010RuntimeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TechTalk.SpecFlow.UnitTestProvider
{
public class MsTest2010RuntimeProvider : MsTestRuntimeProvider
{

}
}
31 changes: 0 additions & 31 deletions Runtime/UnitTestProvider/MsTestRuntimeProvider.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace TechTalk.SpecFlow.UnitTestProvider
{
internal static class UnitTestRuntimeProviderHelper
{
public static Action<string> GetAssertMethod(string assemblyName, string typeName, string methodName)
{
Assembly msTestAssembly = Assembly.Load(assemblyName);
Type assertType = msTestAssembly.GetType(typeName, true);

MethodInfo method = assertType.GetMethod(methodName,
BindingFlags.Public | BindingFlags.Static, null,
new Type[] { typeof(string) }, null);
if (method == null)
throw new SpecFlowException("Assert method not found: " + methodName);

List<ParameterExpression> parameters = new List<ParameterExpression>();
foreach (ParameterInfo parameterInfo in method.GetParameters())
{
parameters.Add(Expression.Parameter(parameterInfo.ParameterType, parameterInfo.Name));
}
var lambda = Expression.Lambda<Action<string>>(
Expression.Call(method, parameters.Cast<Expression>().ToArray()),
parameters.ToArray());

return lambda.Compile();
}

}

public class MsTestRuntimeProvider : IUnitTestRuntimeProvider
{
private const string MSTEST_ASSEMBLY = "Microsoft.VisualStudio.QualityTools.UnitTestFramework";
Expand Down
35 changes: 35 additions & 0 deletions Runtime/UnitTestProvider/UnitTestRuntimeProviderHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace TechTalk.SpecFlow.UnitTestProvider
{
internal static class UnitTestRuntimeProviderHelper
{
public static Action<string> GetAssertMethod(string assemblyName, string typeName, string methodName)
{
Assembly msTestAssembly = Assembly.Load(assemblyName);
Type assertType = msTestAssembly.GetType(typeName, true);

MethodInfo method = assertType.GetMethod(methodName,
BindingFlags.Public | BindingFlags.Static, null,
new Type[] { typeof(string) }, null);
if (method == null)
throw new SpecFlowException("Assert method not found: " + methodName);

List<ParameterExpression> parameters = new List<ParameterExpression>();
foreach (ParameterInfo parameterInfo in method.GetParameters())
{
parameters.Add(Expression.Parameter(parameterInfo.ParameterType, parameterInfo.Name));
}
var lambda = Expression.Lambda<Action<string>>(
Expression.Call(method, parameters.Cast<Expression>().ToArray()),
parameters.ToArray());

return lambda.Compile();
}

}
}
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
1.3.2 - ???

New features:
+ Support for MsTest for .NET 4.0 categories. Configure the test provider name to
"MsTest.2010" in order to use the [TestCategory] attribute.

Fixed issues:
+ Report generation fails if no custom XSLT is provided
Expand Down

0 comments on commit 0b31fac

Please sign in to comment.