2008-11-19 48 views
14

我最近已经接触到nUnit流畅的界面,我喜欢它;不过,我正在使用msTest。MSTest有流利的断言API吗?

有谁知道是否有一个流畅的接口,要么是测试框架不可知或msTest?

回答

17

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 
0

根据我的研究没有一个,但如果你愿意去尽可能为什么断言失败的牺牲更好的可报告,并愿意加你可以参考NUnit的,并使用他们的一个新的DLL ....

5

请参阅http://sharptestex.codeplex.com/

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

SharpTestsEx(夏普测试扩展)是一组可扩展的扩展。主要目标是编写Visual Studio IDE intellisense是您的指南的简短断言。 #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(); 
+0

虽然我觉得harrydev可以添加一些有用的信息,但在我看来,SharpTestEx似乎比FluentAssertions更成熟一些。当然是初步观察。任何人都可以随时向我展示为什么FluentAssertions可能会更好。 – llaughlin 2010-11-03 12:52:24

+0

SharpTestEx基本上已经死亡,而Fluent Assertions正在积极开发并增加对所有主要单元测试框架以及所有.NET框架版本(包括.NET 4.5,WinRT,Silverlight 5和WP7/8)的支持。 – 2013-04-22 18:57:28