Update the TestContext within specflow customising MSTestProvider

1,120 views
Skip to first unread message

Max Foulds

unread,
Apr 24, 2013, 10:26:32 AM4/24/13
to spec...@googlegroups.com
So i have managed to get access to the instance of TestContext for my current scenario using the following Custom generator, however if I invoke TestContext.AddResultFile() I was hoping that the file would be added to my MSTest results. How do I update the TestContext that was passed into the FeatureSetup method so that any updates to TestContext are added to results?

Resultant added/amended Generated Code in feature.cs file

        private static Microsoft.VisualStudio.TestTools.UnitTesting.TestContext myTestContext;

        public Microsoft.VisualStudio.TestTools.UnitTesting.TestContext TestContext
        {
            get
            {
                return myTestContext;
            }
            set
            {
                myTestContext = value;
            }
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute()]
        public static void FeatureSetup(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext testContext)
        {
            testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
            TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "This is some text for the feature"ProgrammingLanguage.CSharp, ((string[])(null)));
            testRunner.OnFeatureStart(featureInfo);
            myTestContext = testContext;
        }

        public virtual void ScenarioSetup(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
        {
            testRunner.OnScenarioStart(scenarioInfo);
            testRunner.ScenarioContext.Add("MSTestContext", TestContext);
        }


Custom Generator Code for above code
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MsTest2010CodedUIGeneratorProvider.cs" company="">
//   
// </copyright>
// <summary>
//   The ms test 2010 coded ui generator provider.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
 
namespace MESL.Test.SpecFlowCustomGenerator
{
    using System.CodeDom;
    using TechTalk.SpecFlow.Generator;
    using TechTalk.SpecFlow.Generator.UnitTestProvider;
    using TechTalk.SpecFlow.Utils;
 
    /// <summary>
    /// The ms test 2010 coded ui generator provider.
    /// </summary>
    public class MsTestCustomGeneratorProvider : MsTest2010GeneratorProvider
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MsTestCustomGeneratorProvider"/> class.
        /// </summary>
        /// <param name="codeDomHelper">
        /// The code dom helper.
        /// </param>
        public MsTestCustomGeneratorProvider(CodeDomHelper codeDomHelper)
            : base(codeDomHelper)
        {
        }
 
        /// <summary>
        /// The finalize test class.
        /// </summary>
        /// <param name="generationContext">
        /// The generation context.
        /// </param>
        public override void FinalizeTestClass(TestClassGenerationContext generationContext)
        {
            base.FinalizeTestClass(generationContext);
 
            var msTestContextGeneration =
                new CodeExpressionStatement(
                    new CodeMethodInvokeExpression(
                        new CodeTypeReferenceExpression("testRunner.ScenarioContext"),
                        "Add",
                        new CodeExpression[]
                        {
                            new CodePrimitiveExpression("MSTestContext"),
                            new CodeArgumentReferenceExpression("TestContext")
                        }));
 
            generationContext.ScenarioInitializeMethod.Statements.Add(msTestContextGeneration);
 
            var msTestAssignment =
                new CodeAssignStatement(
                    new CodeVariableReferenceExpression("myTestContext"),
                    new CodeVariableReferenceExpression("testContext"));
            generationContext.TestClassInitializeMethod.Statements.Add(msTestAssignment);
 
            var mstestContextFiled = new CodeMemberField
            {
                Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final,
                Name = "myTestContext",
                Type = new CodeTypeReference(TESTCONTEXT_TYPE),
            };
 
            generationContext.TestClass.Members.Add(mstestContextFiled);
 
            var mstestContextProperty = new CodeMemberProperty();
            mstestContextProperty.Name = "TestContext";
            mstestContextProperty.Type = new CodeTypeReference(TESTCONTEXT_TYPE);
            mstestContextProperty.Attributes =
                      (mstestContextProperty.Attributes & ~MemberAttributes.AccessMask) |
                         MemberAttributes.Public ;
            mstestContextProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("myTestContext")));
            mstestContextProperty.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("myTestContext"), new CodePropertySetValueReferenceExpression()));
 
            generationContext.TestClass.Members.Add(mstestContextProperty);
        }
    }
}
Reply all
Reply to author
Forward
0 new messages