From 0b31fac209975685853560a632afcc03702be023 Mon Sep 17 00:00:00 2001 From: "gaspar.nagy" Date: Tue, 29 Jun 2010 19:01:35 +0200 Subject: [PATCH] MsTest 2010 support --- .../Configuration/GeneratorConfiguration.cs | 3 ++ Generator/TechTalk.SpecFlow.Generator.csproj | 1 + .../MsTest2010GeneratorProvider.cs | 50 +++++++++++++++++++ .../MsTestGeneratorProvider.cs | 8 +-- Runtime/Configuration/RuntimeConfiguration.cs | 3 ++ Runtime/TechTalk.SpecFlow.csproj | 2 + .../MsTest2010RuntimeProvider.cs | 7 +++ .../UnitTestProvider/MsTestRuntimeProvider.cs | 31 ------------ .../UnitTestRuntimeProviderHelper.cs | 35 +++++++++++++ changelog.txt | 2 + 10 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 Generator/UnitTestProvider/MsTest2010GeneratorProvider.cs create mode 100644 Runtime/UnitTestProvider/MsTest2010RuntimeProvider.cs create mode 100644 Runtime/UnitTestProvider/UnitTestRuntimeProviderHelper.cs diff --git a/Generator/Configuration/GeneratorConfiguration.cs b/Generator/Configuration/GeneratorConfiguration.cs index 9ff0be821..d80b767f2 100644 --- a/Generator/Configuration/GeneratorConfiguration.cs +++ b/Generator/Configuration/GeneratorConfiguration.cs @@ -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; diff --git a/Generator/TechTalk.SpecFlow.Generator.csproj b/Generator/TechTalk.SpecFlow.Generator.csproj index 9e91a1bdd..bcde8949e 100644 --- a/Generator/TechTalk.SpecFlow.Generator.csproj +++ b/Generator/TechTalk.SpecFlow.Generator.csproj @@ -69,6 +69,7 @@ + diff --git a/Generator/UnitTestProvider/MsTest2010GeneratorProvider.cs b/Generator/UnitTestProvider/MsTest2010GeneratorProvider.cs new file mode 100644 index 000000000..fef39eb2c --- /dev/null +++ b/Generator/UnitTestProvider/MsTest2010GeneratorProvider.cs @@ -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 featureCategories = null; + + public override void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable 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 categories) + { + SetCategories(memberMethod.CustomAttributes, categories); + } + + private void SetCategories(CodeAttributeDeclarationCollection customAttributes, IEnumerable categories) + { + foreach (var category in categories) + { + customAttributes.Add( + new CodeAttributeDeclaration( + new CodeTypeReference(CATEGORY_ATTR), + new CodeAttributeArgument( + new CodePrimitiveExpression(category)))); + } + } + + } +} \ No newline at end of file diff --git a/Generator/UnitTestProvider/MsTestGeneratorProvider.cs b/Generator/UnitTestProvider/MsTestGeneratorProvider.cs index e50d07cdb..289bf2470 100644 --- a/Generator/UnitTestProvider/MsTestGeneratorProvider.cs +++ b/Generator/UnitTestProvider/MsTestGeneratorProvider.cs @@ -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( @@ -57,12 +57,12 @@ private void SetProperty(CodeAttributeDeclarationCollection customAttributes, st new CodePrimitiveExpression(value)))); } - public void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable categories) + public virtual void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable 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( @@ -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 categories) + public virtual void SetTestCategories(CodeMemberMethod memberMethod, IEnumerable categories) { //MsTest does not support caregories... :( } diff --git a/Runtime/Configuration/RuntimeConfiguration.cs b/Runtime/Configuration/RuntimeConfiguration.cs index a82f3b70e..5d06dbfc8 100644 --- a/Runtime/Configuration/RuntimeConfiguration.cs +++ b/Runtime/Configuration/RuntimeConfiguration.cs @@ -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; diff --git a/Runtime/TechTalk.SpecFlow.csproj b/Runtime/TechTalk.SpecFlow.csproj index 697d17ac9..0958007eb 100644 --- a/Runtime/TechTalk.SpecFlow.csproj +++ b/Runtime/TechTalk.SpecFlow.csproj @@ -81,6 +81,8 @@ + + diff --git a/Runtime/UnitTestProvider/MsTest2010RuntimeProvider.cs b/Runtime/UnitTestProvider/MsTest2010RuntimeProvider.cs new file mode 100644 index 000000000..307898113 --- /dev/null +++ b/Runtime/UnitTestProvider/MsTest2010RuntimeProvider.cs @@ -0,0 +1,7 @@ +namespace TechTalk.SpecFlow.UnitTestProvider +{ + public class MsTest2010RuntimeProvider : MsTestRuntimeProvider + { + + } +} \ No newline at end of file diff --git a/Runtime/UnitTestProvider/MsTestRuntimeProvider.cs b/Runtime/UnitTestProvider/MsTestRuntimeProvider.cs index e6ed05f9f..0012fa570 100644 --- a/Runtime/UnitTestProvider/MsTestRuntimeProvider.cs +++ b/Runtime/UnitTestProvider/MsTestRuntimeProvider.cs @@ -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 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 parameters = new List(); - foreach (ParameterInfo parameterInfo in method.GetParameters()) - { - parameters.Add(Expression.Parameter(parameterInfo.ParameterType, parameterInfo.Name)); - } - var lambda = Expression.Lambda>( - Expression.Call(method, parameters.Cast().ToArray()), - parameters.ToArray()); - - return lambda.Compile(); - } - - } - public class MsTestRuntimeProvider : IUnitTestRuntimeProvider { private const string MSTEST_ASSEMBLY = "Microsoft.VisualStudio.QualityTools.UnitTestFramework"; diff --git a/Runtime/UnitTestProvider/UnitTestRuntimeProviderHelper.cs b/Runtime/UnitTestProvider/UnitTestRuntimeProviderHelper.cs new file mode 100644 index 000000000..a23538bc5 --- /dev/null +++ b/Runtime/UnitTestProvider/UnitTestRuntimeProviderHelper.cs @@ -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 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 parameters = new List(); + foreach (ParameterInfo parameterInfo in method.GetParameters()) + { + parameters.Add(Expression.Parameter(parameterInfo.ParameterType, parameterInfo.Name)); + } + var lambda = Expression.Lambda>( + Expression.Call(method, parameters.Cast().ToArray()), + parameters.ToArray()); + + return lambda.Compile(); + } + + } +} \ No newline at end of file diff --git a/changelog.txt b/changelog.txt index 1ffb77bde..6951f8184 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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