2008-11-19 77 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