【问题标题】:NUnit Testing failed even though there are arguments即使有参数,NUnit 测试也失败了
【发布时间】:2020-04-18 18:18:54
【问题描述】:

我正在尝试进行测试以检查 View 返回正确的字符串。我对 NUnit 测试非常陌生,并且已经查看了一些教程,但我不确定我做错了什么。

using System;

namespace ItemTracker
{

public enum Category{Book,StorageDevice,Stationary};

class Item{

    private string _id;
    private double _price;
    private Category _category;

    public Item(string id, double price, Category category){

        _id=id;
        _price=price;
        _category=category;

    }

    public string ID{

            get{return _id;}
            set{_id=value;}
    }
    public double Price{

            get{return _price;}
            set{_price=value;}
    }

    public Category Category{

            get{return _category;}
            set{_category=value;}
    }

    public string View(){

            if(_category==Category.Book){

                    return "Get ready for the adventure!";

            }

            else if(_category==Category.StorageDevice){

                    return "Data storing in progress";

            }

            else if(_category==Category.Stationary){

                    return "Learn something new with me!";

            }

            else{
                    return "Invalid";
            }

    }


    }


}

这是我的 TestClass.cs 和我已经尝试过的,即将输出的值放入数组中:

using NUnit.Framework;
using System;

namespace ItemTracker
{
[TestFixture()]
class testclass{
    [Test()]
    public void Testing(Item[] j){

        j[0]=new Item("B1001",39.90,Category.Book);
        foreach(Item x in j){

            Assert.AreEqual("Get ready for the adventure!",x.View());
        }
    }


}

}

但是我收到一条错误消息:

  Error Message:
   No arguments were provided

【问题讨论】:

  • 查看here 了解参数化测试。您需要添加注释(属性)来指定应如何传递参数。为什么不删除论点?你不需要它。在里面定义你的数组?
  • @OguzOzgul 哇,这救了我,谢谢

标签: c# unit-testing nunit


【解决方案1】:

您应该在测试方法中创建一个数组,否则您的测试将被视为参数化(但您没有为参数指定任何属性,例如TestCaseTestCaseSource

[Test]
public void Testing()
{
    var j = new Item[1];
    j[0] = new Item("B1001",39.90,Category.Book);
    foreach(Item x in j)
    {
        Assert.AreEqual("Get ready for the adventure!",x.View());
    }
}

【讨论】:

    【解决方案2】:

    确保您的班级Itempublic,我看到它的当前访问级别是internal(默认,当您不提供访问权限时)。

    除此之外,您只需要知道很遗憾,MSTest 不支持参数。 另一种选择是使用Data-driven tests。 另外,您可以查看this的答案。

    作为您案例的当前解决方案,请在您的方法中创建数组,而不是将其作为参数提供。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2016-06-23
      • 2014-08-14
      • 2018-12-17
      • 2022-11-06
      • 2019-03-04
      • 1970-01-01
      相关资源
      最近更新 更多