Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Ian Johnson Avatar asked Nov 16 '25 22:11

Ian Johnson


1 Answers

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.

like image 122
Ralph Willgoss Avatar answered Nov 19 '25 11:11

Ralph Willgoss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!