【问题标题】:How to use [SetUp] method to arrange for a unit test. How about parameters?如何使用 [SetUp] 方法安排单元测试。参数呢?
【发布时间】:2015-04-22 02:28:57
【问题描述】:
public class CoffeeStrengthEstimator
{
    /// <summary>
    ///     Estimates the strength of the coffee (how many beans to use) depending on the button pressed
    /// </summary>
    /// <param name="buttonPressed"> The numeral position of the button pressed</param>
    /// <returns>An enum value indicating the estimated coffee strength</returns>
    public CoffeeStrength EstimateCoffeeStrength(int buttonPressed)
    {
        if (buttonPressed == 1)
        {
            return CoffeeStrength.Light;
        }
        else if (buttonPressed == 2)
        {
            return CoffeeStrength.Medium;
        }
        else if (buttonPressed == 3)
        {
            return CoffeeStrength.Strong;
        }
        else
        {
            throw new ArgumentException ("Invalid Button press, please try again");
        }
    }
}

public enum CoffeeStrength
{
    Light,
    Medium,
    Strong
}

===> 写个单元测试来测试上面的方法可以吗;并使用仅采用 2 个参数的方法创建 CoffeeMaker 类的新对象和 Coffee 类的对象?

【问题讨论】:

  • 你的问题到底是什么?

标签: c# unit-testing


【解决方案1】:

不清楚你在问什么,但我假设你在问如何测试CoffeeStrengthEstimator(使用 NUnit,基于标题中的Setup)。

SetUp 方法在测试类中的每个测试方法之前运行,因此用于设置每个方法所需的公共代码 - 这可以为被测系统的任何依赖项提供存根/伪造/模拟 需要,也可能需要实例化一个被测系统的实例。

可以对测试方法进行参数化以接收不同的参数。

综上所述,CoffeeStrengthEstimator 的测试类可能如下所示

[TestFixture]
public class CoffeeStrengthEstimatorTests
{
    private CoffeeStrengthEstimator _estimator;

    [SetUp]
    public void SetUp()
    {
        // common Arrange
        _estimator = new CoffeeStrengthEstimator();
    }

    [Test]
    [TestCase(1, CoffeeStrength.Light)]
    [TestCase(2, CoffeeStrength.Medium)]
    [TestCase(3, CoffeeStrength.Strong)]
    public void EstimateCoffeeStrength_Returns_Expected_CoffeeStrength_For_Button_Pressed_1_2_or_3(int buttonPressed,
        CoffeeStrength expectedCoffeeStrength)
    {
        // Act
        var coffeeStrength = _estimator.EstimateCoffeeStrength(buttonPressed);

        // Assert
        Assert.AreEqual(expectedCoffeeStrength, coffeeStrength);
    }

    [Test]
    [TestCase(-1)]
    [TestCase(0)]
    [TestCase(4)]
    public void EstimateCoffeeStrength_Throws_ArgumentException_When_Button_Pressed_Not_1_2_or_3(int buttonPressed)
    {
        // Act and Assert
        Assert.Throws<ArgumentException>(() => _estimator.EstimateCoffeeStrength(buttonPressed));
    }
}

【讨论】:

  • 谢谢拉斯。所以你的意思是我不需要创建 CoffeeMaker 类的对象和 Coffee 类的对象来测试这个方法?
  • 本例中的被测系统是CoffeeStrengthEstimator,根据您提供的代码,它看起来可以独立于CoffeeMaker进行测试和Coffee。您还可以对CoffeeMaker 和Coffee 进行单元测试,在每种情况下,被测系统 (SUT) 将分别为CoffeeMaker 和Coffee,即单元测试应该只专注于测试 SUT。
  • 另一个问题。我需要添加一些参考吗?在程序集参考中找不到或错误的 TestFixture。 [SetUp] 是一种方法,它被用作“类型”。找不到 [Test] 和 [Testcase] 或缺少程序集引用。
  • 我使用的是Visual Studio 2013,所以我需要在VS中安装它们,我是安装所有还是某些特定的。
  • 查看 nuget 文档以了解如何安装软件包 - docs.nuget.org/Consume/Package-Manager-Dialog
猜你喜欢
  • 2023-03-11
  • 2015-10-04
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
  • 2015-05-12
相关资源
最近更新 更多