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

Why isn't the code in my specflow BeforeTestRun method being executed?

我已经编写了要在 运行 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 方法上,所以有什么问题吗?

BeforeTestRun-修饰的方法只有在 Binding-修饰的 class 中才会被 specflow 注意到。据我所知,文档中并未明确指出这一点。

只需将 Binding 属性添加到您的 class:

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

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