使用SpecFlow时,C#NUnit SetUp和TearDown函数未运行

时间:2017-11-07 13:31:58

标签: c# selenium nunit specflow

我尝试使用NUnit和selenium创建自动化测试,但是我无法使SetUp和TearDown函数工作。

[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)));
        }

他们根本就没有被召唤。我使用的代码如果我把它们放在步骤本身里面,那么这需要我添加一个步骤,例如。然后浏览器应该关闭,当我应该只能使用TearDown函数时。

1 个答案:

答案 0 :(得分:2)

这是单元测试吗?
将您[SetUpFixture]更改为[TestFixture] (注意:如果您使用的是NUnit 2.5或者很棒,可以删除[TestFixture])

后者用于一次性设置,前者用于每次测试的设置。

这是SpecFlow测试吗?
我还假设您已将 SpecFlows 测试运行器设置为 NUnit

您需要使用BeforeScenarioBeforeFeature属性,而不是NUnit属性。