Ian Johnson
Ian Johnson

Reputation: 41

C# NUnit SetUp and TearDown functions not running when using SpecFlow

I'm trying to create automated tests using NUnit and selenium, however I cannot get the SetUp and TearDown functions to work.

[Binding] [SetUpFixture]
public class AuthenticatorSteps
{
    IWebDriver _driver;
    WebDriverWait wait;
    string username;
    string password;

    [SetUp]
    public void SetUp()
    {
        _driver = new ChromeDriver();
        wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
    }

    [TearDown]
    public void TearDown()
    {
        _driver.Close();
    }

[Given(@"I am on the site")]
        public void GivenIAmOnTheSite()
        {
            _driver.Manage().Window.Maximize();
            _driver.Navigate().GoToUrl("https://qa02-ukcasino.bedegaming.net");
            wait.Until(x => x.FindElement(By.CssSelector(AuthenticatorElements.LoginButton)));
        }

They just aren't being called at all. The code I'm using works if I put them inside the steps themselves, however that requires me to add a step eg. Then the browser should close, when I should be able to just use the TearDown function.

Upvotes: 3

Views: 4625

Answers (1)

Ralph Willgoss
Ralph Willgoss

Reputation: 12163

Is this a Unit Test?
Change you [SetUpFixture] to be a [TestFixture].
(note: If you are using NUnit 2.5 or great you can remove [TestFixture])

The later is used for one time setups and the former, for setups per test.

Is this a SpecFlow test?
I also assume you have set SpecFlows test runner to be NUnit.

You need to use the BeforeScenario or BeforeFeature attributes, rather than the NUnit ones.

Upvotes: 3

Related Questions