MSpec 是否支持 "row tests" 或数据驱动的测试,例如 NUnit TestCase?
Does MSpec support "row tests" or data-driven tests, like NUnit TestCase?
我们正在使用 Machine.Specification 作为我当前项目的测试框架。这适用于我们正在测试的大部分内容。但是,我们有许多视图模型,其中我们有 'formatted' 属性,这些属性采用一些原始数据,应用一些逻辑,以及 return 该数据的格式化版本。
由于格式化涉及逻辑(空检查、零的特殊情况等),我想测试一些可能的数据值,包括边界条件。对我来说,这感觉不是 MSpec 的正确用例,我们应该下降到 NUnit 之类的东西,在那里我可以使用 [TestCase] 属性之类的东西编写数据驱动的测试。
是否有一种干净、简单的方法可以在 MSpec 中编写此类测试,或者我认为我们应该为此类测试使用不同的工具是否正确?
查看模型
public class DwellingInformation
{
public DateTime? PurchaseDate { get; set; }
public string PurchaseDateFormatted
{
if(PurchaseDate == null)
return "N/A";
return PurchaseDate.Value.ToShortDateString();
}
public int? ReplacementCost { get; set; }
public string ReplacementCostFormatted
{
if(ReplacementCost == null)
return "N/A";
if(ReplacementCost == 0)
return "Not Set";
return ReplacementCost.ToString("C0");
}
// ... and so on...
}
MSpec 测试
public class When_ReplacementCost_is_null
{
private static DwellingInformation information;
Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = null };
};
It ReplacementCostFormatted_should_be_Not_Available = () => information.ReplacementCostFormatted.ShouldEqual("N/A");
}
public class When_ReplacementCost_is_zero
{
private static DwellingInformation information;
Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = "0" };
};
It ReplacementCostFormatted_should_be_Not_Set = () => information.ReplacementCostFormatted.ShouldEqual("Not Set");
}
public class When_ReplacementCost_is_a_non_zero_value
{
private static DwellingInformation information;
Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = 200000 };
};
It ReplacementCostFormatted_should_be_formatted_as_currency = () => information.ReplacementCostFormatted.ShouldEqual("0,000");
}
N个单位w/TestCase
[TestCase(null, "N/A")]
[TestCase(0, "Not Set")]
[TestCase(200000, "0,000")]
public void ReplacementCostFormatted_Correctly_Formats_Values(int? inputVal, string expectedVal)
{
var information = new DwellingInformation { ReplacementCost = inputVal };
information.ReplacementCostFormatted.ShouldEqual(expectedVal);
}
是否有更好的方法来编写我缺少的 MSpec 测试,因为我对 MSpec 还不够熟悉,或者 MSpec 在这种情况下真的只是错误的工具?
注意: 团队中的另一位开发人员认为我们应该在 MSpec 中编写所有测试,因为他不想在项目中引入多个测试框架。虽然我理解他的观点,但我想确保我们为正确的工作使用正确的工具,所以如果 MSpec 不是正确的工具,我正在寻找可以用来论证引入另一个框架的观点。
简短回答,使用 NUnit 或 xunit。组合测试不是 mspec 的最佳选择,而且可能永远不会。我从不关心项目中的多个测试框架,尤其是当第二个工具更适合特定场景时。 Mspec 最适合行为规范。测试输入变体不是。
我们正在使用 Machine.Specification 作为我当前项目的测试框架。这适用于我们正在测试的大部分内容。但是,我们有许多视图模型,其中我们有 'formatted' 属性,这些属性采用一些原始数据,应用一些逻辑,以及 return 该数据的格式化版本。
由于格式化涉及逻辑(空检查、零的特殊情况等),我想测试一些可能的数据值,包括边界条件。对我来说,这感觉不是 MSpec 的正确用例,我们应该下降到 NUnit 之类的东西,在那里我可以使用 [TestCase] 属性之类的东西编写数据驱动的测试。
是否有一种干净、简单的方法可以在 MSpec 中编写此类测试,或者我认为我们应该为此类测试使用不同的工具是否正确?
查看模型
public class DwellingInformation
{
public DateTime? PurchaseDate { get; set; }
public string PurchaseDateFormatted
{
if(PurchaseDate == null)
return "N/A";
return PurchaseDate.Value.ToShortDateString();
}
public int? ReplacementCost { get; set; }
public string ReplacementCostFormatted
{
if(ReplacementCost == null)
return "N/A";
if(ReplacementCost == 0)
return "Not Set";
return ReplacementCost.ToString("C0");
}
// ... and so on...
}
MSpec 测试
public class When_ReplacementCost_is_null
{
private static DwellingInformation information;
Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = null };
};
It ReplacementCostFormatted_should_be_Not_Available = () => information.ReplacementCostFormatted.ShouldEqual("N/A");
}
public class When_ReplacementCost_is_zero
{
private static DwellingInformation information;
Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = "0" };
};
It ReplacementCostFormatted_should_be_Not_Set = () => information.ReplacementCostFormatted.ShouldEqual("Not Set");
}
public class When_ReplacementCost_is_a_non_zero_value
{
private static DwellingInformation information;
Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = 200000 };
};
It ReplacementCostFormatted_should_be_formatted_as_currency = () => information.ReplacementCostFormatted.ShouldEqual("0,000");
}
N个单位w/TestCase
[TestCase(null, "N/A")]
[TestCase(0, "Not Set")]
[TestCase(200000, "0,000")]
public void ReplacementCostFormatted_Correctly_Formats_Values(int? inputVal, string expectedVal)
{
var information = new DwellingInformation { ReplacementCost = inputVal };
information.ReplacementCostFormatted.ShouldEqual(expectedVal);
}
是否有更好的方法来编写我缺少的 MSpec 测试,因为我对 MSpec 还不够熟悉,或者 MSpec 在这种情况下真的只是错误的工具?
注意: 团队中的另一位开发人员认为我们应该在 MSpec 中编写所有测试,因为他不想在项目中引入多个测试框架。虽然我理解他的观点,但我想确保我们为正确的工作使用正确的工具,所以如果 MSpec 不是正确的工具,我正在寻找可以用来论证引入另一个框架的观点。
简短回答,使用 NUnit 或 xunit。组合测试不是 mspec 的最佳选择,而且可能永远不会。我从不关心项目中的多个测试框架,尤其是当第二个工具更适合特定场景时。 Mspec 最适合行为规范。测试输入变体不是。