MSpec支持"行测试"或数据驱动测试,例如NUnit的TestCase吗?

7
我们在当前项目中使用 Machine.Specification 作为测试框架。对于我们大部分测试来说,这个框架运行良好。但是,在一些视图模型中,我们有一些“格式化”属性,需要对一些原始数据进行逻辑处理,并返回格式化后的数据。
由于格式化中涉及到逻辑(如空值检查、零的特殊情况等),我想要测试许多可能的数据值,包括边界条件。在我看来,这不像是 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("$200,000");
}

NUnit与TestCase的结合
[TestCase(null, "N/A")]
[TestCase(0, "Not Set")]
[TestCase(200000, "$200,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不是正确的工具,我正在寻找可以用来辩论引入另一个框架的观点。


http://www.planetgeek.ch/2013/05/26/rowtest-theory-testdata-support-for-machine-specifications/ - Mohsen
1个回答

2

简而言之,使用NUnit或xunit。组合测试并不是mspec的优势,也很可能永远不会是。在我的项目中,我从来没有关心过多个测试框架,特别是当第二个工具在特定场景下表现更好时。 Mspec最适合行为规范。测试输入变量并不是它的强项。


我理解"I never cared for multiple test frameworks"的意思是"我从来不介意使用多个测试框架",因为如果不这样说,这个陈述就没有意义。如果需要大量参数化,我同意你的评估。然而,对于给定的示例,我实际上更喜欢将每个场景表达为一个离散的规范。如果您有100个变体,请使用支持参数化的框架。如果只有像问题中那样的几个,请使用上下文/规范。 - Derek Greer
1
是的,minded更适合。英语不是我的母语。 - Alexander Groß

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接