MSTest 是否有流畅的断言 API?

发布于 2024-07-09 07:06:04 字数 102 浏览 14 评论 0原文

我最近接触了 nUnit 中的 Fluent 界面,我很喜欢它; 不过,我正在使用 msTest。

有谁知道是否有一个与测试框架无关或用于 msTest 的流畅界面?

I've recently been exposed to the fluent interface in nUnit and I love it; however, I am using msTest.

Does anyone know if there is a fluent interface that is either testing framework agnostic or for msTest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难忘№最初的完美 2024-07-16 07:06:04

请参阅流畅断言。 你可以做类似的事情

"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

See Fluent Assertions. You can do stuff like

"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
萌酱 2024-07-16 07:06:04

请参阅 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();

See http://sharptestex.codeplex.com/

NOTE: SharpTestsEx appears to no longer be actively developed, recommended alternative is http://www.fluentassertions.com/.

SharpTestsEx (Sharp Tests Extensions) is a set of extensible extensions. The main target is to write short assertions where the Visual Studio IDE intellisense is your guide. #TestsEx can be used with NUnit, MsTests, xUnit, MbUnit... even in Silverlight.

Syntax example for strongly typed assertions (taken from webpage):

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();
吻泪 2024-07-16 07:06:04

根据我的研究,没有这样的,但如果你愿意牺牲更好的可报告性来了解断言失败的原因,并愿意添加一个新的 dll,你可以引用 nunit 并使用他们的......

Based on my research there isn't one, but if your willing to sacrifice the better reportability as far as why an assert failed and willing to add a new dll you can reference nunit and use theirs....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文