编写测试用例以检查NUnit的日期时间



我无法在第3天的第2天测试第1天这样的日期

public int dayInMonth(int month,int year)
{
if (month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
else if (month == 2)
{
if (year % 400 == 0)
{
return 29;
}
else if (year % 100 == 0)
{
return 28;
}
else if (year % 4 == 0)
{
return 29;
}
else return 28;
}
else
{
return 31;
}      
}

我需要使用NUnit来测试它。但我不知道如何为这种方法编写测试用例

将测试分成三部分
安排,行动&明确肯定
例如

// Arrange
var someObject = new SomeClass();
var year = 2020;
var month = 2;
var expectedResult = 29;
// Act
var actualResult = someObject.dayInMonth(year, month);
// Assert
Assert.AreEqual(expectedResult, actualResult);

正如@John提到的例子,当您需要针对多个输入运行测试时,请使用TestCaseAttribute 的参数

更新:

TestCaseAttribute example
[TestCase(2020, 1, ExpectedResult=31)]
[TestCase(2020, 2, ExpectedResult=29)]
[TestCase(2020, 3, ExpectedResult=31)]
public int DayInMonthTest(int year, int month)
{
var someObject = new SomeClass();
return someObject.dayInMonth(year, month);
}

最新更新