【问题标题】:Define the same setup and teardown for every test fixture为每个测试夹具定义相同的设置和拆卸
【发布时间】:2020-04-06 16:27:32
【问题描述】:

我有一个包含多个 TestFixture 的项目,我想为每个测试定义相同的 [SetUp] [TearDown] 方法。考虑到我有很多 TestFixture,我想避免编辑所有文件来添加这两个指令。

浏览 nunit 文档,我认为 [SetUpFixture] 是完美的解决方案。 所以我复制了example 并尝试运行它,但似乎我的 TearDown 方法从未运行过,而 SetUp 正在按我的预期执行。为了断言这一点,我只是使用了 throw 语句。

我的问题是:

  1. 我的用例有问题吗?我应该采取不同的方式来实现我的目标吗?
  2. 我是否错误地使用了 SetUpFixture 属性?

下面是我的 SetUpFixture 类。我正在使用 Nunit 3.11。

using System;
using NUnit.Framework;

namespace MyTestProject
{
  [SetUpFixture]
  public class MySetUpClass
  {
    [OneTimeSetUp]
    public void RunBeforeAnyTests()
    {
      StaticClass.Init();
//    throw new InvalidOperationException("SetUp reached");
    }

    [OneTimeTearDown]
    public void RunAfterAnyTests()
    {
      StaticClass.Finalize();
//    throw new InvalidOperationException("TearDown reached");
    }
  }
}

【问题讨论】:

    标签: c# nunit


    【解决方案1】:
    1. 您的用例很好,您可以通过创建一个定义了 [Setup] 和 [Teardown] 的基类来做到这一点。如果您继承,它们将在每个测试中运行一次。它记录在https://github.com/nunit/docs/wiki/SetUp-and-TearDown

    2. 我似乎不清楚帮助,您对结果的解释也不清楚,但我建议您在调用 StaticClass 之前尝试一些断点或记录,以确保它们不会干扰您的行为.

    【讨论】:

    • 继承似乎是要走的路,而不是我想的 SetUpFixture。我发现我的问题与one 重复。关于 2.,我正在切换评论以避免任何干扰。
    【解决方案2】:

    我创建了示例程序来根据患者的血压水平预测健康状况,并实施了示例单元测试(NUnit 测试)。我已经使用[SetUp][Tear Down] 对每种测试方法实施了示例单元测试,以便更好地理解。

    主类

    
         public class HealthReport
        {
            public string Healthdamage(int pressure)
            {
                if (pressure == 120)
                {
                    return "normal";
                }
                if (pressure > 120 && pressure < 140)
                {
                    return "At risk";
                }
                if (pressure > 140)
                {
                    return "Highly risk";
                }
                else
                {
                    return "low BP";
                }
            }
        }
    

    单元测试类

        [TestFixture]
        public class Healthtest
        {
            private HealthReport health;
            [SetUp]
            public void SetUp()
            {
                health = new HealthReport();
            }
            [TearDown]
            public void Teardown()
            {
                health = null;
            }
    
            [Test]
            public void Health_Damage_returnnormal()
            {
                string statusreport = health.Healthdamage(120);
                Assert.That(statusreport, Is.EqualTo("normal"));
            }
            [Test]
            public void Health_Damage_returnAtrisk()
            {
                string statusreport = health.Healthdamage(130);
                Assert.That(statusreport, Is.EqualTo("At risk"));
            }
            [Test]
            public void Health_Damage_returnHighlyrisk()
            {
                string statusreport = health.Healthdamage(180);
                Assert.That(statusreport, Is.EqualTo("Highly risk"));
            }
            [Test]
            public void Health_Damage_returnlow()
            {
                string statusreport = health.Healthdamage(110);
                Assert.That(statusreport, Is.EqualTo("low BP"));
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多