Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

specflow

I've written code that I want executed before a run of specflow tests, to set up various globals that all the tests will need:

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
        }
    }
}

However, this code isn't being executed. I've made sure to put BeforeTestRun on a static method, as the documentation says to, so what's wrong?

like image 541
AakashM Avatar asked Sep 06 '25 03:09

AakashM


1 Answers

The BeforeTestRun-decorated method will only be noticed by specflow if it's in a Binding-decorated class. As far as I can see this isn't explciitly called out in the documentation.

Simple add a Binding attribute to your class:

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

and your BeforeTestRun method will be called as desired.

like image 144
AakashM Avatar answered Sep 07 '25 22:09

AakashM