15

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
JoshBerke
  • 66,142
  • 25
  • 126
  • 164

3 Answers3

17

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
vossad01
  • 11,552
  • 8
  • 56
  • 109
Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
5

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();
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
nietras
  • 3,949
  • 1
  • 34
  • 38
  • While I think harrydev could have added some helpful information, in my opinion, SharpTestEx seems a bit more mature than FluentAssertions. Preliminary observation, of course. Anyone feel free to show me why FluentAssertions may be better. – llaughlin Nov 03 '10 at 12:52
  • SharpTestEx is basically dead, whereas Fluent Assertions is actively being developed on and adds support for all major unit testing frameworks as well as all .NET framework versions, including .NET 4.5, WinRT, Silverlight 5 and WP7/8. – Dennis Doomen Apr 22 '13 at 18:57
1

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....

JoshBerke
  • 66,142
  • 25
  • 126
  • 164