6

nUnit 设置夹具参考

我的解决方案是这样设置的,使用 SpecFlow Gherkin Features
Solution
- Tests Project
-- Features
-- Steps
- Pages Project
-- Pages

我使用如下命令运行 nUnit 测试运行器:

"C:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" ".\bin\Dev\Solution.dll"

我已将此代码添加到上述项目结构的步骤文件夹中。

using System;
using NUnit.Framework;

namespace TestsProject.StepDefinitions
{
    /// <summary>
    /// This class needs to be in the same namespace as the StepDefinitions
    /// see: https://www.nunit.org/index.php?p=setupFixture&r=2.4.8
    /// </summary>
    [SetUpFixture]
    public class NUnitSetupFixture
    {
        [SetUp]
        public void RunBeforeAnyTests()
        {
            // this is not working
            throw new Exception("This is never-ever being called.");
        }

        [TearDown]
        public void RunAfterAnyTests()
        {
        }
    }
}

我究竟做错了什么?为什么[SetupFixture]在 nUnit 开始所有测试之前不调用?

4

1 回答 1

5

使用OneTimeSetUpOneTimeTearDown属性,SetUpFixture因为您使用的是 NUnit 3.0 而不是这里SetUp详述的和TearDown属性。

using System;
using NUnit.Framework;

namespace TestsProject.StepDefinitions
{
    [SetUpFixture]
    public class NUnitSetupFixture
    {
        [OneTimeSetUp]
        public void RunBeforeAnyTests()
        {
            //throw new Exception("This is called.");
        }

        [OneTimeTearDown]
        public void RunAfterAnyTests()
        {
        }
    }
}
于 2017-06-16T14:04:50.337 回答