【问题标题】:Is there a fluent assertion API for MSTest?MSTest 是否有流畅的断言 API?
【发布时间】:2010-09-23 01:30:54
【问题描述】:

我最近接触了 nUnit 的流畅界面,我喜欢它;但是,我正在使用 msTest。

有谁知道是否有一个流利的接口是测试框架不可知的还是用于 msTest 的?

【问题讨论】:

    标签: .net unit-testing mstest fluent fluent-interface


    【解决方案1】:

    Fluent Assertions。你可以做类似的事情

    "ABCDEFGHI".Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);
    
    new[] { 1, 2, 3 }.Should().HaveCount(4, "because we thought we put three items in the 
    collection"))
    
    dtoCollection.Should().Contain(dto => dto.Id != null);
    
    collection.Should().HaveCount(c => c >= 3);
    
    dto.ShouldHave().AllPropertiesBut(d => d.Id).EqualTo(customer);
    
    dt1.Should().BeWithin(TimeSpan.FromHours(50)).Before(dt2); 
    
    Action action = () => recipe.AddIngredient("Milk", 100, Unit.Spoon);
    action
       .ShouldThrow<RuleViolationException>()
       .WithMessage("Cannot change the unit of an existing ingredient")
       .And.Violations.Should().Contain(BusinessRule.CannotChangeIngredientQuanity
    

    【讨论】:

    • 护目镜!他们什么都不做!
    【解决方案2】:

    http://sharptestex.codeplex.com/

    注意:SharpTestsEx 似乎不再积极开发,推荐的替代方案是http://www.fluentassertions.com/

    SharpTestsEx(Sharp Tests Extensions)是一组可扩展的扩展。主要目标是编写简短的断言,其中 Visual Studio IDE 智能感知是您的指南。 #TestsEx 可以与 NUnit、MsTests、xUnit、MbUnit 一起使用...甚至在 Silverlight 中。

    强类型断言的语法示例(取自网页):

    true.Should().Be.True();
    false.Should().Be.False();
    
    const string something = "something";
    something.Should().Contain("some");
    something.Should().Not.Contain("also");
    something.ToUpperInvariant().Should().Not.Contain("some");
    
    something.Should()
        .StartWith("so")
        .And
        .EndWith("ing")
        .And
        .Contain("meth");
    
    something.Should()
        .Not.StartWith("ing")
        .And
        .Not.EndWith("so")
        .And
        .Not.Contain("body");
    
    var ints = new[] { 1, 2, 3 };
    ints.Should().Have.SameSequenceAs(new[] { 1, 2, 3 });
    ints.Should().Not.Have.SameSequenceAs(new[] { 3, 2, 1 });
    ints.Should().Not.Be.Null();
    ints.Should().Not.Be.Empty();
    
    ints.Should()
        .Contain(2)
        .And
        .Not.Contain(4);
    
    (new int[0]).Should().Be.Empty();
    

    【讨论】:

    • 虽然我认为 harrydev 可以添加一些有用的信息,但在我看来,SharpTestEx 似乎比 FluentAssertions 更成熟一些。当然是初步观察。任何人都可以随时向我展示为什么 FluentAssertions 可能会更好。
    • SharpTestEx 基本上已经死了,而 Fluent Assertions 正在积极开发中,并增加了对所有主要单元测试框架以及所有 .NET 框架版本的支持,包括 .NET 4.5、WinRT、Silverlight 5 和 WP7 /8.
    【解决方案3】:

    根据我的研究,没有,但如果您愿意牺牲更好的可报告性来说明断言失败的原因并愿意添加新的 dll,您可以参考 nunit 并使用他们的...

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多