编写测试用例以通过 NUnit 检查日期时间
Write Test case for check Date time by NUnit
我无法测试第 1 天、第 2 天和第 3 天这样的日期
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);
}
我无法测试第 1 天、第 2 天和第 3 天这样的日期
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);
}