您可以使用TestCase 代替Test 属性来运行具有不同数据的相同测试代码,例如
[TestCase(0.02m, "Name: parcel - Postal code 2582Cd - Weight 0,02 - Value 0,0 - Department Mail")]
[TestCase(2m, "Name: parcel - Postal code 2582Cd - Weight 2 - Value 0,0 - Department Regular")]
public void ParcelsWithSpecificWeightShouldBeSpecificToString(decimal weight, string expected)
{
var parcel = new Parcel { Name="parcel", PostalCode = "2582Cd", Weight = weigth, Value = 0.0m };
Assert.AreEqual(expected, parcel.ToString());
}
但实际上你正在测试Department 属性,所以我建议不要测试parcel.ToString(),而是parcel.Department:
[TestCase(0.02m, "Mail")]
[TestCase(2m, "Regular")]
public void ParcelsWithSpecificWeightShouldBeSpecificDepartment(decimal weight, string expected)
{
var parcel = new Parcel { Name="parcel", PostalCode = "2582Cd", Weight = weigth, Value = 0.0m };
Assert.AreEqual(expected, parcel.Department);
}