在SpecFlow步骤绑定类中访问TestContext [英] Access TestContext in SpecFlow Step Binding class

查看:75
本文介绍了在SpecFlow步骤绑定类中访问TestContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从SpecFlow(1.7.1)步骤绑定类中访问MSTest TestContext? 在生成的功能文件代码中,有一个FeatureSetup方法,该方法将TestContext作为参数,但显然不对其进行任何操作.

Is it possible to access the MSTest TestContext from within a SpecFlow (1.7.1) step binding class? In the generated code of a feature file there is a method FeatureSetup which takes the TestContext as an argument but apparently doesn't do anything with it.

推荐答案

我找到了一种将参数传递给TestContext,然后从SpecFlow访问它们的方法.

I found a way to pass parameters to TestContext and then access them from SpecFlow.

通过添加具有TestContext属性的[TestClass]并将其AssemblyInit()方法标记为[AssemblyInitialize],以便在运行测试之前及早对其进行初始化,MSTest将能够填充TestContext.

By adding a [TestClass] which has a TestContext property and marking its AssemblyInit() method as [AssemblyInitialize] so it gets initialized early before runnig the tests and MSTest will be able to populate the TestContext.

{
    [TestClass]
    public class InitializeTestContext
    {
        public static TestContext Context { get; private set; }

        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            Context = context;
        }
    }
}

然后可以从我的BaseSteps类中访问它:

And then can access it from my BaseSteps class:

{
            public abstract class BaseSteps : TechTalk.SpecFlow.Steps
            {
                public string GetTestEnvironment()
                {
                    TestContext testContext = InitializeTestContext.Context;

                    string testEnvironment = testContext.Properties["Environment"].ToString();

                    return testEnvironment;
                }
            }
}

这篇关于在SpecFlow步骤绑定类中访问TestContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆