为什么我的specflow中的代码没有执行BeforeTestRun方法?

时间:2017-05-10 10:22:06

标签: specflow

我编写了一些我希望在一系列specflow测试之前执行的代码,以设置所有测试都需要的各种全局变量:

namespace MyProject.IntegrationTest
{
    public static class Global
    {
        public static Dictionary<string, string> ContextProperties { get;  set; }

        [BeforeTestRun]
        public static void TestInitialize()
        {
            // code to populate ContextProperties

            var baseUrl = Global.ContextProperties["baseUrl"];
            if (baseUrl.Contains("//localhost"))
            {
                // It's our responsibility to make sure the service is running
                // TODO start iis express for the service
            }

            // etc
        }
    }
}

但是,此代码未执行。我已确保将BeforeTestRun放在static方法上,正如文档所述,那么出了什么问题?

1 个答案:

答案 0 :(得分:2)

BeforeTestRun - 装饰方法只会在specflow 中注意到,如果它位于Binding - 装饰类中。据我所知,文档中没有明确说明这一点。

简单地向您的班级添加Binding属性:

namespace MyProject.IntegrationTest
{
    [Binding]            // <==================== here
    public static class Global
    {

,您的BeforeTestRun方法将根据需要进行调用。