【问题标题】:How to read test run settings parameter value in specflow tests?如何在 specflow 测试中读取测试运行设置参数值?
【发布时间】:2017-12-13 10:40:57
【问题描述】:

我们在 Visual Studio 中使用 .runsettings 文件来运行 specflow 测试。我们在那里配置了某些参数。我需要在运行时访问这些参数值以在 specflow 测试方法中使用。我尝试通过 TestContext 访问它们,如下所示

 [ClassInitialize]
    public static void Initialize(TestContext testContext)
        {            
            var value= 
            Convert.ToString(testContext.Properties["testParameter1"]);
        }

我在运行时遇到了 testcontext 实例的异常,如下所示。 "System.NullReferenceException: '对象引用未设置为对象的实例。'"

环境 视觉工作室企业 2017 规范流 2.2.1 单元测试提供者:MsTest

此代码在 Microsoft 单元测试项目中使用时运行良好。如何从测试运行设置文件中读取值以进行 specflow 测试?还有其他方法可以访问 runsettings 参数吗?

【问题讨论】:

    标签: mstest specflow testcontext


    【解决方案1】:

    由于它们位于 TestContext 上,因此您需要它的实例。

    您可以通过 DI 获得它:

    [When(@"I do something")]
    public void WhenIDoSomething()
    {
        var textContext = ScenarioContext.Current.ScenarioContainer.Resolve<Microsoft.VisualStudio.TestTools.UnitTesting.TestContext>();
    }
    

    完整示例:https://github.com/techtalk/SpecFlow/blob/master/Tests/TechTalk.SpecFlow.Specs/Features/MsTestProvider.feature#L43

    但请注意,它目前在 BeforeScenario 挂钩中不起作用 (https://github.com/techtalk/SpecFlow/issues/936)

    【讨论】:

    • 谢谢@Andreas Willich。这有帮助。
    猜你喜欢
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2013-09-21
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多