【问题标题】:nUnit SetupFixture class in C# not being called when tests are run运行测试时未调用 C# 中的 nUnit SetupFixture 类
【发布时间】:2017-06-16 13:22:56
【问题描述】:

nUnit SetupFixture Reference

我的解决方案是这样设置的,使用 SpecFlow Gherkin 功能
解决方案
- 测试项目
-- 特点
-- 步骤
- 页面项目
-- 页面

我使用如下命令运行 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()
        {
        }
    }
}

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

【问题讨论】:

  • 您使用的是哪个版本的 NUnit 框架?

标签: c# nunit test-runner


【解决方案1】:

使用SetUpFixtureOneTimeSetUpOneTimeTearDown 属性,因为您使用的是NUnit 3.0 而不是SetUpTearDown 属性,详细here

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()
        {
        }
    }
}

【讨论】:

  • 谢谢!我终于通过阅读您共享的链接并找到了它来工作:“任何命名空间之外的 SetUpFixture 为整个程序集提供了 SetUp 和 TearDown。”
  • 将代码放入 namespace TestsProject 而不是 namespace TestsProject.StepDefinitions 也可以。
  • 是的,两者都有效。调用 SetUpFixture 来设置它所在的任何命名空间,并在该命名空间及以下的每个测试之前和之后运行。这允许您为不同的命名空间拥有多个固定装置。
猜你喜欢
  • 2018-05-06
  • 2019-09-24
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
相关资源
最近更新 更多