In This Topic
    Tips
    In This Topic

    General tips

    Improved assertions

    The examples below show how you might improve your existing assertions to both get more readable assertions and much more informative failure messages.

    If you see something missing, please consider submitting a pull request.

     Collections
    Assertion Improvement
    actual.Any().Should().BeTrue();
    actual.Should().NotBeEmpty();
    Expected True, but found False. Expected collection not to be empty.
    Assertion Improvement
    actual.Any().Should().BeFalse();
    actual.Should().BeEmpty();
    Expected False, but found True. Expected collection to be empty, but found {SomeProperty: 0, OtherProperty: }.
    Assertion Improvement
    actual.Any(x => x.SomeProperty).Should().BeTrue();
    actual.Should().Contain(x => x.SomeProperty);
    Expected True, but found False. Collection {SomeProperty: 0, OtherProperty: Actual} should have an item matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass5_0).expectedValue).
    Assertion Improvement
    actual.Any(x => x.SomeProperty).Should().BeFalse();
    actual.Should().NotContain(x => x.SomeProperty);
    Expected False, but found True. Expected Collection {SomeProperty: 0, OtherProperty: Unexpected} to not have any items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass7_0).unexpectedValue).
    Assertion Improvement
    actual.All(x => x.SomeProperty).Should().BeTrue();
    // now fails on empty collection;
    actual.Should().OnlyContain(x => x.SomeProperty)
    Expected True, but found False. Expected collection to contain only items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass9_0).expectedValue), but {SomeProperty: 0, OtherProperty: Actual} do(es) not match.
    Assertion Improvement
    actual.Contains(expected).Should().BeTrue();
    actual.Should().Contain(expected);
    Expected True, but found False. Expected collection {"Actual"} to contain "Expected".
    Assertion Improvement
    actual.Contains(unexpected).Should().BeFalse();
    actual.Should().NotContain(unexpected);
    Expected False, but found True. Expected collection {"Expected"} to not contain "Expected".
    Assertion Improvement
    actual.Any(x => x == unexpected).Should().BeFalse();
    actual.Should().NotContain(unexpected);
    Expected False, but found True. Expected collection {"Unexpected"} to not contain "Unexpected".
    Assertion Improvement
    actual.Count().Should().Be(k);
    actual.Should().HaveCount(k);
    Expected value to be 2, but found 3. Expected collection to contain 2 item(s), but found 3.
    Assertion Improvement
    actual.Count().Should().BeGreaterThan(k);
    actual.Should().HaveCountGreaterThan(k);
    Expected a value greater than 4, but found 3. Expected collection to contain more than 4 item(s), but found 3.
    Assertion Improvement
    actual.Count().Should().BeGreaterOrEqualTo(k);
    actual.Should().HaveCountGreaterOrEqualTo(k);
    Expected a value greater or equal to 4, but found 3. Expected collection to contain at least 4 item(s), but found 3.
    Assertion Improvement
    actual.Count().Should().BeLessThan(k);
    actual.Should().HaveCountLessThan(k);
    Expected a value less than 2, but found 3. Expected collection to contain fewer than 2 item(s), but found 3.
    Assertion Improvement
    actual.Count().Should().BeLessOrEqualTo(k);
    actual.Should().HaveCountLessOrEqualTo(k);
    Expected a value less or equal to 2, but found 3. Expected collection to contain at most 2 item(s), but found 3.
    Assertion Improvement
    actual.Count().Should().NotBe(k);
    actual.Should().NotHaveCount(k);
    Did not expect 3. Expected collection to not contain 3 item(s), but found 3.
    Assertion Improvement
    actual.Should().HaveCount(1);
    actual.Should().ContainSingle();
    Expected collection to contain 1 item(s), but found 3. Expected collection to contain a single item, but found {"", "", ""}.
    Assertion Improvement
    actual.Should().HaveCount(0);
    actual.Should().BeEmpty();
    Expected collection to contain 0 item(s), but found 3. Expected collection to be empty, but found {"", "", ""}.
    Assertion Improvement
    actual.Should().HaveCount(expected.Count());
    actual.Should().HaveSameCount(expected);
    Expected collection to contain 2 item(s), but found 3. Expected collection to have 2 item(s), but found 3.
    Assertion Improvement
    actual.Count().Should().NotBe(unexpected.Count());
    actual.Should().NotHaveSameCount(unexpected);
    Did not expect 2. Expected collection to not have 2 item(s), but found 2.
    Assertion Improvement
    actual.Where(x => x.SomeProperty).Should().NotBeEmpty();
    actual.Should().Contain(x => x.SomeProperty);
    Expected collection not to be empty. Collection {SomeProperty: 0, OtherProperty: Actual} should have an item matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass39_0).expectedValue).
    Assertion Improvement
    actual.Where(x => x.SomeProperty).Should().BeEmpty();
    actual.Should().NotContain(x => x.SomeProperty);
    Expected collection to be empty, but found {SomeProperty: 0, OtherProperty: Expected}. Expected Collection {SomeProperty: 0, OtherProperty: Expected} to not have any items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass41_0).expectedValue).
    Assertion Improvement
    actual.Where(x => x.SomeProperty).Should().HaveCount(1);
    actual.Should().ContainSingle(x => x.SomeProperty);
    Expected collection to contain 1 item(s), but found 0. Expected collection to contain a single item matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass43_0).expectedValue), but no such item was found.
    Assertion Improvement
    actual.Should().OnlyContain(e => !e.SomeProperty);
    actual.Should().NotContain(x => x.SomeProperty);
    Expected collection to contain only items matching (x.OtherProperty != value(UnitTests2.CollectionTests+<>c__DisplayClass44_0).unexpectedValue), but {SomeProperty: 0, OtherProperty: Unexpected} do(es) not match. Expected Collection {SomeProperty: 0, OtherProperty: Unexpected} to not have any items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass45_0).unexpectedValue).
    Assertion Improvement
    actual.Should().NotBeNull().And.NotBeEmpty();
    actual.Should().NotBeNullOrEmpty();
    Expected collection not to be empty. Expected collection not to be empty.
    Assertion Improvement
    actual.ElementAt(k).Should().Be(expected);
    actual.Should().HaveElementAt(k, expected);
    Expected string to be "Expected" with a length of 8, but "Unexpected" has a length of 10. Expected "Expected" at index 0, but found "Unexpected".
    Assertion Improvement
    actual[k].Should().Be(expected);
    actual.Should().HaveElementAt(k, expected);
    Expected string to be "Expected" with a length of 8, but "Unexpected" has a length of 10. Expected "Expected" at index 0, but found "Unexpected".
    Assertion Improvement
    actual.Skip(k).First().Should().Be(expected);
    actual.Should().HaveElementAt(k, expected);
    Expected string to be "Expected" with a length of 8, but "Unexpected" has a length of 10. Expected "Expected" at index 1, but found "Unexpected".
    Assertion Improvement
    actual.OrderBy(x => x.SomeProperty)
        .Should().Equal(actual);
    actual.Should().BeInAscendingOrder(x => x.SomeProperty);
    Expected collection to be equal to {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: }, but {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: } differs at index 0. Expected collection {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: } to be ordered" by SomeProperty" and result in {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: }.
    Assertion Improvement
    actual.OrderByDescending(x => x.SomeProperty)
        .Should().Equal(actual);
    actual.Should().BeInDescendingOrder(x => x.SomeProperty);
    Expected collection to be equal to {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: }, but {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: } differs at index 0. Expected collection {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: } to be ordered" by SomeProperty" and result in {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: }.
    Assertion Improvement
    actual.Select(e1 => e1.SomeProperty).Should()
        .Equal(expected.Select(e2 => e2.SomeProperty));
    actual.Should()
        .Equal(expected, 
            (e1, e2) => e1.SomeProperty == e2.SomeProperty);
    Expected collection to be equal to {"Expected", "Expected"}, but {"Actual", "Actual"} differs at index 0. Expected collection to be equal to {SomeProperty: 1, OtherProperty: Expected, SomeProperty: 2, OtherProperty: Expected}, but {SomeProperty: 1, OtherProperty: Actual, SomeProperty: 2, OtherProperty: Actual} differs at index 0.
    Assertion Improvement
    actual.Intersect(expected).Should().BeEmpty();
    actual.Should().NotIntersectWith(expected);
    Expected collection to be empty, but found {SomeProperty: 1, OtherProperty: Expected}. Did not expect collection to intersect with {SomeProperty: 1, OtherProperty: Expected}, but found the following shared items {SomeProperty: 1, OtherProperty: Expected}.
    Assertion Improvement
    actual.Intersect(expected).Should().NotBeEmpty();
    actual.Should().IntersectWith(expected);
    Expected collection not to be empty. Expected collection to intersect with {SomeProperty: 1, OtherProperty: Expected}, but {SomeProperty: 1, OtherProperty: Actual} does not contain any shared items.
    Assertion Improvement
    actual.Select(x => x.SomeProperty)
        .Should().NotContainNulls();
    actual.Should().NotContainNulls(e => e.OtherProperty);
    Expected collection not to contain nulls, but found one at index 0. Expected collection not to contain <null>s on e.OtherProperty, but found {SomeProperty: 1, OtherProperty: }.
    Assertion Improvement
    actual.Should().HaveSameCount(actual.Distinct());
    actual.Should().OnlyHaveUniqueItems();
    Expected collection to have 1 item(s), but found 2. Expected collection to only have unique items, but item SomeProperty: 1, OtherProperty: is not unique.
    Assertion Improvement
    actual.Select(x => x.SomeProperty)
        .Should().OnlyHaveUniqueItems();
    actual.Should().OnlyHaveUniqueItems(x => x.SomeProperty);
    Expected collection to only have unique items, but item 1 is not unique. Expected collection to only have unique items on x.SomeProperty, but item SomeProperty: 1, OtherProperty: is not unique.
    Assertion Improvement
    // From the assertion it is unclear what is being tested.
    actual.FirstOrDefault().Should().BeNull();
    // The first element is null.
    actual.Should().HaveElementAt(0, null);
    Expected string to be <null>, but found "". Expected <null> at index 0, but found "".
     Comparable and Numerics
    Assertion Improvement
    actual.Should().BeGreaterThan(0);
    actual.Should().BePositive();
    Expected a value greater than 0, but found -1. Expected positive value, but found -1
    Assertion Improvement
    actual.Should().BeLessThan(0);
    actual.Should().BeNegative();
    Expected a value less than 0, but found 1. Expected negative value, but found 1
    Assertion Improvement
    (lower <= actual && actual <= upper).Should().BeTrue();
    actual.Should().BeInRange(lower, upper);
    Expected True, but found False. Expected value to be between 1 and 5, but found 6.
    Assertion Improvement
    (lower <= actual && actual <= upper).Should().BeFalse();
    actual.Should().NotBeInRange(lower, upper);
    Expected False, but found True. Expected value to not be between 1 and 5, but found 4.
     DateTimes
    Assertion Improvement
    actual.Date.Should().Be(expected.Date);
    actual.Should().BeSameDateAs(expected);
    Expected date and time to be <2017-01-01>, but found <2017-01-02>. Expected a date and time with date <2017-01-01>, but found <2017-01-02 21:00:00>.
    Assertion Improvement
    actual.Date.Should().NotBe(unexpected.Date);
    actual.Should().NotBeSameDateAs(unexpected);
    Expected date and time not to be <2017-01-01>, but it is. Expected a date and time that does not have date <2017-01-01>, but found it does.
    Assertion Improvement
    actual.Year.Should().Be(expected.Year);
    actual.Should().HaveYear(expected.Year);
    Expected actual.Year to be 2018, but found 2017 (difference of -1). Expected the year part of actual to be 2018, but found 2017.
    Assertion Improvement
    actual.Year.Should().NotBe(unexpected.Year);
    actual.Should().NotHaveYear(unexpected.Year);
    Did not expect the year part of actual to be 2017, but it was. Did not expect actual.Year to be 2017.
    Assertion Improvement
    actual.Month.Should().Be(expected.Month);
    actual.Should().HaveMonth(expected.Month);
    Expected actual.Month to be 2, but found 1. Expected the month part of actual to be 2, but found 1.
    Assertion Improvement
    actual.Month.Should().NotBe(unexpected.Month);
    actual.Should().NotHaveMonth(unexpected.Month);
    Did not expect actual.Month to be 1. Did not expect the month part of actual to be 1, but it was.
    Assertion Improvement
    actual.Day.Should().Be(expected.Day);
    actual.Should().HaveDay(expected.Day);
    Expected actual.Day to be 2, but found 1. Expected the day part of actual to be 2, but found 1.
    Assertion Improvement
    actual.Day.Should().NotBe(unexpected.Day);
    actual.Should().NotHaveDay(unexpected.Day);
    Did not expect actual.Day to be 1. Did not expect the day part of actual to be 1, but it was.
    Assertion Improvement
    actual.Hour.Should().Be(expected.Hour);
    actual.Should().HaveHour(expected.Hour);
    Expected actual.Hour to be 19, but found 16 (difference of -3). Expected the hour part of actual to be 19, but found 16.
    Assertion Improvement
    actual.Hour.Should().NotBe(unexpected.Hour);
    actual.Should().NotHaveHour(unexpected.Hour);
    Did not expect actual.Hour to be 16. Did not expect the hour part of actual to be 16, but it was.
    Assertion Improvement
    actual.Minute.Should().Be(expected.Minute);
    actual.Should().HaveMinute(expected.Minute);
    Expected actual.Minute to be 31, but found 30 (difference of -1). Expected the minute part of actual to be 31, but found 30.
    Assertion Improvement
    actual.Minute.Should().NotBe(unexpected.Minute);
    actual.Should().NotHaveMinute(unexpected.Minute);
    Did not expect actual.Minute to be 30. Did not expect the minute part of actual to be 30, but it was.
    Assertion Improvement
    actual.Second.Should().Be(expected.Second);
    actual.Should().HaveSecond(expected.Second);
    Expected actual.Second to be 18, but found 17 (difference of -1). Expected the seconds part of actual to be 18, but found 17.
    Assertion Improvement
    actual.Second.Should().NotBe(unexpected.Second);
    actual.Should().NotHaveSecond(unexpected.Second);
    Did not expect actual.Second to be 17. Did not expect the seconds part of actual to be 17, but it was.
     Dictionaries
    Assertion Improvement
    actual.ContainsKey(expected).Should().BeTrue();
    actual.Should().ContainKey(expected);
    Expected True, but found False. Expected dictionary {[0, ]} to contain key 1.
    Assertion Improvement
    actual.ContainsKey(expected).Should().BeFalse();
    actual.Should().NotContainKey(expected);
    Expected False, but found True. Dictionary {[0, ]} should not contain key 0, but found it anyhow.
    Assertion Improvement
    actual.ContainsValue(expected).Should().BeTrue();
    actual.Should().ContainValue(expected);
    Expected True, but found False. Expected dictionary {[0, ]} to contain value "expected".
    Assertion Improvement
    actual.ContainsValue(expected).Should().BeFalse();
    actual.Should().NotContainValue(expected);
    Expected False, but found True. Dictionary {[0, expected]} should not contain value "expected", but found it anyhow.
    Assertion Improvement
    actual.Should().ContainKey(expectedKey)
        .And.ContainValue(expectedValue);
    actual.Should().Contain(expectedKey, expectedValue);
    Expected dictionary {[0, ]} to contain value "expected". Expected dictionary to contain value "expected" at key 0, but found "".
    Assertion Improvement
    actual.Should().ContainKey(expected.Key)
        .And.ContainValue(expected.Value);
    actual.Should().Contain(expected);
    Expected dictionary {[0, ]} to contain value "expected". Expected dictionary to contain value "expected" at key 0, but found "".
     Exceptions
    Assertion Improvement
    Action act = () => throw new InvalidOperationException("Problems, errorCode2 and more Problems");
    
    act.Should().ThrowExactly<InvalidOperationException>()
        .Which.Message.Should().Contain("errorCode1");
    Action act = () => throw new InvalidOperationException("Problems, errorCode2 and more Problems");
    
    // using wildcards
    act.Should().ThrowExactly<InvalidOperationException>()
        .WithMessage("*errorCode1*");
    Expected string "Problems, errorCode2 and more Problems" to contain "errorCode1". Expected exception message to match the equivalent of "*errorCode1*", but "Problems, errorCode2 and more Problems" does not.
    Assertion Improvement
    Action act = () => throw new InvalidOperationException("Problems, errorCode2 and more Problems");
    
    act.Should().Throw<Exception>()
        .Which.Message.Should().Contain("errorCode1");
    Action act = () => throw new InvalidOperationException("Problems, errorCode2 and more Problems");
    
    // using wildcards
    act.Should().Throw<Exception>()
        .WithMessage("*errorCode1*");
    Expected string "Problems, errorCode2 and more Problems" to contain "errorCode1". Expected exception message to match the equivalent of "*errorCode1*", but "Problems, errorCode2 and more Problems" does not.
    Assertion Improvement
    Action act = () => throw new Exception("Problems, errorCode2 and more Problems", new InvalidOperationException());
    
    act.Should().Throw<Exception>()
        .Which.InnerException.Should().BeAssignableTo<ArgumentException>();
    Action act = () => throw new Exception("Problems, errorCode2 and more Problems", new InvalidOperationException());
    
    act.Should().Throw<Exception>()
        .WithInnerException<ArgumentException>();
    Expected action to be assignable to System.ArgumentException, but System.InvalidOperationException is not. Expected inner System.ArgumentException, but found System.InvalidOperationException with message "Operation is not valid due to the current state of the object."
    Assertion Improvement
    Action act = () => throw new Exception("Problems, errorCode2 and more Problems", new ArgumentNullException());
    
    act.Should().Throw<Exception>()
        .Which.InnerException.Should().BeOfType<ArgumentException>();
    Action act = () => throw new Exception("Problems, errorCode2 and more Problems", new ArgumentNullException());
    
    act.Should().Throw<Exception>()
        .WithInnerExceptionExactly<ArgumentException>();
    Expected type to be System.ArgumentException, but found System.ArgumentNullException. Expected inner System.ArgumentException, but found System.ArgumentNullException with message "Value cannot be null."
     Nullables
    Assertion Improvement
    actual.HasValue.Should().BeTrue();
    actual.Should().HaveValue();
    Expected True, but found False. Expected a value.
    Assertion Improvement
    actual.HasValue.Should().BeFalse();
    actual.Should().NotHaveValue();
    Expected False, but found True. Did not expect a value, but found 1.
     Strings
    Assertion Improvement
    actual.StartsWith(expectedPrefix).Should().BeTrue();
    actual.Should().StartWith(expectedPrefix);
    Expected True, but found False. Expected string to start with "Expected", but "ActualString" differs near "Act" (index 0).
    Assertion Improvement
    actual.EndsWith(expectedSuffix).Should().BeTrue();
    actual.Should().EndWith(expectedSuffix);
    Expected True, but found False. Expected string "ActualString" to end with "Expected".
    Assertion Improvement
    actual.Should().NotBeNull().And.NotBeEmpty();
    actual.Should().NotBeNullOrEmpty();
    Did not expect empty string. Expected string not to be <null> or empty, but found "".
    Assertion Improvement
    string.IsNullOrEmpty(actual).Should().BeTrue();
    actual.Should().BeNullOrEmpty();
    Expected True, but found False. Expected string to be <null> or empty, but found "Actual".
    Assertion Improvement
    string.IsNullOrEmpty(actual).Should().BeFalse();
    actual.Should().NotBeNullOrEmpty();
    Expected False, but found True. Expected string not to be <null> or empty, but found "".
    Assertion Improvement
    string.IsNullOrWhiteSpace(actual).Should().BeTrue();
    actual.Should().BeNullOrWhiteSpace();
    Expected True, but found False. Expected string to be <null> or whitespace, but found "Actual".
    Assertion Improvement
    string.IsNullOrWhiteSpace(actual).Should().BeFalse();
    actual.Should().NotBeNullOrWhiteSpace();
    Expected False, but found True. Expected string not to be <null> or whitespace, but found " ".
    Assertion Improvement
    actual.Length.Should().Be(k);
    actual.Should().HaveLength(k);
    Expected value to be 4, but found 6. Expected string with length 4, but found string "Actual" with length 6.
     Types
    Assertion Improvement
    actual.GetType().Should().Be(typeof(T));
    actual.Should().BeOfType<T>();
    Expected type to be UnitTests2.MyClass, but found UnitTests2.MyIdenticalClass. Expected type to be UnitTests2.MyClass, but found UnitTests2.MyIdenticalClass.
    Assertion Improvement
    actual.GetType().Should().NotBe(typeof(T));
    actual.Should().NotBeOfType<T>();
    Expected type not to be [UnitTests2.MyClass, UnitTests2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], but it is. Expected type not to be [UnitTests2.MyClass, UnitTests2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], but it is.
    Assertion Improvement
    (actual is T).Should().BeTrue();
    actual.Should().BeAssignableTo<T>();
    Expected True, but found False. Expected object to be assignable to UnitTests2.MyClass, but UnitTests2.MyIdenticalClass is not.
    Assertion Improvement
    (actual as T).Should().NotBeNull();
    actual.Should().BeAssignableTo<T>();
    Expected object not to be <null>. Expected object to be assignable to UnitTests2.MyClass, but UnitTests2.MyIdenticalClass is not.
    Assertion Improvement
    (actual is T).Should().BeFalse();
    actual.Should().NotBeAssignableTo<T>();
    Expected False, but found True. Expected object to not be assignable to UnitTests2.MyClass, but UnitTests2.MyClass is.
    Assertion Improvement
    (actual as T).Should().BeNull();
    actual.Should().NotBeAssignableTo<T>();
    Expected object to be <null>, but found SomeProperty: 1, OtherProperty: actual. Expected object to not be assignable to UnitTests2.MyClass, but UnitTests2.MyClass is.

    MSTest Migration

    The examples below show how you might write equivalent MSTest assertions using Fluent Assertions including the failure message from each case. We think this is both a useful migration guide and a convincing argument for switching.

    If you see something missing, please consider submitting a pull request.

     Assert
    MSTest Fluent Assertions
    Assert.IsTrue(actual);
    actual.Should().BeTrue();
    Assert.IsTrue failed. Expected True, but found False.
    MSTest Fluent Assertions
    Assert.IsFalse(actual);
    actual.Should().BeFalse();
    Assert.IsFalse failed. Expected False, but found True.
    MSTest Fluent Assertions
    Assert.IsNull(actual);
    actual.Should().BeNull();
    Assert.IsNull failed. Expected object to be <null>, but found System.Object (HashCode=51904525).
    MSTest Fluent Assertions
    Assert.IsNotNull(actual);
    actual.Should().NotBeNull();
    Assert.IsNotNull failed. Expected object not to be <null>.
    MSTest Fluent Assertions
    Assert.AreEqual(expected, actual);
    actual.Should().Be(expected);
    Assert.AreEqual failed. Expected:. Actual:. Expected object to be SomeProperty: 2, OtherProperty: expected, but found SomeProperty: 1, OtherProperty: actual.
    MSTest Fluent Assertions
    Assert.AreEqual(expected, actual, delta);
    actual.Should().BeApproximately(expected, delta);
    Assert.AreEqual failed. Expected a difference no greater than <0.5> between expected value <2> and actual value <1.25>. Expected value 1.25 to approximate 2.0 +/- 0.5, but it differed by 0.75.
    MSTest Fluent Assertions
    Assert.AreNotEqual(expected, actual);
    actual.Should().NotBe(expected);
    Assert.AreNotEqual failed. Expected any value except:. Actual:. Did not expect object to be equal to SomeProperty: 1, OtherProperty: expected.
    MSTest Fluent Assertions
    Assert.AreNotEqual(expected, actual, delta);
    actual.Should().NotBeApproximately(expected, delta);
    Assert.AreNotEqual failed. Expected a difference greater than <0.5> between expected value <2> and actual value <2>. Expected value 2.0 to not approximate 2.0 +/- 0.5, but it only differed by 0.0.
    MSTest Fluent Assertions
    Assert.AreSame(expected, actual);
    actual.Should().BeSameAs(expected);
    Assert.AreSame failed. Expected object to refer to SomeProperty: 1, OtherProperty: actual, but found SomeProperty: 1, OtherProperty: actual.
    MSTest Fluent Assertions
    Assert.AreNotSame(expected, actual);
    actual.Should().NotBeSameAs(expected);
    Assert.AreNotSame failed. Did not expect reference to object SomeProperty: 1, OtherProperty: actual.
    MSTest Fluent Assertions
    Assert.IsInstanceOfType(actual, typeof(T));
    actual.Should().BeOfType<T>();
    Assert.IsInstanceOfType failed. Expected type:. Actual type:. Expected type to be UnitTests2.MyIdenticalClass, but found UnitTests2.MyClass.
    MSTest Fluent Assertions
    Assert.IsNotInstanceOfType(actual, typeof(T));
    actual.Should().NotBeOfType<T>();
    Assert.IsNotInstanceOfType failed. Wrong Type:. Actual type:. Expected type not to be [UnitTests2.MyClass, UnitTests2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], but it is.
    MSTest Fluent Assertions
    Assert.IsTrue(actual == expected);
    // refer to the exact same object in memory
    actual.Should().BeSameAs(expected);
    Assert.IsTrue failed. Expected object to refer to SomeProperty: 2, OtherProperty: expected, but found SomeProperty: 1, OtherProperty: actual.
    MSTest Fluent Assertions
    Assert.IsFalse(actual == expected);
    // refer to the different object in memory
    actual.Should().NotBeSameAs(expected);
    Assert.IsFalse failed. Did not expect reference to object SomeProperty: 1, OtherProperty: expected.
    MSTest Fluent Assertions
    Assert.IsTrue(actual != expected);
    // refer to the different object in memory
    actual.Should().NotBeSameAs(expected);
    Assert.IsTrue failed. Did not expect reference to object SomeProperty: 1, OtherProperty: expected.
    MSTest Fluent Assertions
    Assert.IsFalse(actual != expected);
    // refer to the exact same object in memory
    actual.Should().BeSameAs(expected)
    Assert.IsFalse failed. Expected object to refer to SomeProperty: 2, OtherProperty: expected, but found SomeProperty: 1, OtherProperty: actual.
    MSTest Fluent Assertions
    Assert.IsTrue(actual > expected);
    actual.Should().BeGreaterThan(expected);
    Assert.IsTrue failed. Expected a value greater than 2, but found 1.
    MSTest Fluent Assertions
    Assert.IsFalse(actual > expected);
    actual.Should().BeLessThanOrEqualTo(expected);
    Assert.IsFalse failed. Expected a value less or equal to 1, but found 2.
    MSTest Fluent Assertions
    Assert.IsTrue(actual >= expected);
    actual.Should().BeGreaterThanOrEqualTo(expected);
    Assert.IsTrue failed. Expected a value greater or equal to 2, but found 1.
    MSTest Fluent Assertions
    Assert.IsFalse(actual >= expected);
    actual.Should().BeLessThan(expected);
    Assert.IsFalse failed. Expected a value less than 1, but found 2.
    MSTest Fluent Assertions
    Assert.IsTrue(actual < expected);
    actual.Should().BeLessThan(expected);
    Assert.IsTrue failed. Expected a value less than 1, but found 2.
    MSTest Fluent Assertions
    Assert.IsFalse(actual < expected);
    actual.Should().BeGreaterThanOrEqualTo(expected);
    Assert.IsFalse failed. Expected a value greater or equal to 2, but found 1.
    MSTest Fluent Assertions
    Assert.IsTrue(actual <= expected);
    actual.Should().BeLessThanOrEqualTo(expected);
    Assert.IsTrue failed. Expected a value less or equal to 1, but found 2.
    MSTest Fluent Assertions
    Assert.IsFalse(actual <= expected);
    actual.Should().BeGreaterThan(expected);
    Assert.IsFalse failed. Expected a value greater than 2, but found 1.
     CollectionAssert
    MSTest Fluent Assertions
    CollectionAssert.AllItemsAreUnique(actual);
    actual.Should().OnlyHaveUniqueItems();
    CollectionAssert.AllItemsAreUnique failed. Duplicate item found:. Expected collection to only have unique items, but item SomeProperty: 1, OtherProperty: item is not unique.
    MSTest Fluent Assertions
    CollectionAssert.AreEqual(expected, actual);
    actual.Should().Equal(expected);
    CollectionAssert.AreEqual failed. (Element at index 0 do not match.) Expected collection to be equal to {SomeProperty: 1, OtherProperty: item}, but {SomeProperty: 1, OtherProperty: different} differs at index 0.
    MSTest Fluent Assertions
    CollectionAssert.AreNotEqual(expected, actual);
    actual.Should().NotEqual(expected);
    CollectionAssert.AreNotEqual failed. (Both collection contain same elements.) Did not expect collections {SomeProperty: 1, OtherProperty: item} and {SomeProperty: 1, OtherProperty: item} to be equal.
    MSTest Fluent Assertions
    CollectionAssert.AreEquivalent(expected, actual);
    actual.Should().BeEquivalentTo(expected);
    CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of . The actual collection contains 0 occurrence(s). Expected collection {SomeProperty: 1, OtherProperty: item, SomeProperty: 2, OtherProperty: item} to be equivalent to {SomeProperty: 1, OtherProperty: item, SomeProperty: 2, OtherProperty: other}, but it misses {SomeProperty: 2, OtherProperty: other}.
    MSTest Fluent Assertions
    CollectionAssert.AreNotEquivalent(expected, actual);
    actual.Should().NotBeEquivalentTo(expected);
    CollectionAssert.AreNotEquivalent failed. Both collections contain the same elements. Expected collection {SomeProperty: 1, OtherProperty: item, SomeProperty: 2, OtherProperty: other} not be equivalent with collection {SomeProperty: 1, OtherProperty: item, SomeProperty: 2, OtherProperty: other}.
    MSTest Fluent Assertions
    CollectionAssert.Contains(actual, expected);
    actual.Should().Contain(expected);
    CollectionAssert.Contains failed. Expected collection {SomeProperty: 2, OtherProperty: other} to contain SomeProperty: 1, OtherProperty: item.
    MSTest Fluent Assertions
    CollectionAssert.DoesNotContain(actual, expected);
    actual.Should().NotContain(expected);
    CollectionAssert.DoesNotContain failed. Expected collection {SomeProperty: 1, OtherProperty: item} to not contain SomeProperty: 1, OtherProperty: item.
    MSTest Fluent Assertions
    CollectionAssert.IsSubsetOf(actual, expectedSuperset);
    actual.Should().BeSubsetOf(expectedSuperset);
    CollectionAssert.IsSubsetOf failed. Expected collection to be a subset of {SomeProperty: 1, OtherProperty: item}, but items {SomeProperty: 2, OtherProperty: other} are not part of the superset.
    MSTest Fluent Assertions
    CollectionAssert.IsNotSubsetOf(actual, expectedSuperset);
    actual.Should().NotBeSubsetOf(expectedSuperset);
    CollectionAssert.IsNotSubsetOf failed. Did not expect collection {SomeProperty: 1, OtherProperty: item} to be a subset of {SomeProperty: 1, OtherProperty: item}.
    MSTest Fluent Assertions
    CollectionAssert.AllItemsAreNotNull(actual);
    actual.Should().NotContainNulls();
    CollectionAssert.AllItemsAreNotNull failed. Expected collection not to contain nulls, but found one at index 0.
    MSTest Fluent Assertions
    CollectionAssert.AllItemsAreInstancesOfType(actual, typeof(T));
    actual.Should().AllBeAssignableTo<T>();
    CollectionAssert.AllItemsAreInstancesOfType failed. Element at index 0 is not of expected type. Expected type:. Actual type:. Expected type to be "T", but found [System.String],
     StringAssert
    MSTest Fluent Assertions
    StringAssert.Contains(actual, expectedSubstring);
    actual.Should().Contain(expectedSubstring);
    StringAssert.Contains failed. String 'SomeLongString' does not contain string 'Short'. . Expected string "SomeLongString" to contain "Short".
    MSTest Fluent Assertions
    StringAssert.StartWith(actual, expectedPrefix);
    actual.Should().StartWith(expectedPrefix);
    StringAssert.StartsWith failed. String 'LongString' does not start with string 'Short'. . Expected string to start with "Short", but "LongString" differs near "Lon" (index 0).
    MSTest Fluent Assertions
    StringAssert.EndsWith(actual, expectedSuffix);
    actual.Should().EndWith(expectedSuffix);
    StringAssert.EndsWith failed. String 'StringLong' does not end with string 'Short'. . Expected string "StringLong" to end with "Short".
    MSTest Fluent Assertions
    StringAssert.Matches(actual, expectedPattern);
    actual.Should().MatchRegex(expectedPattern);
    StringAssert.Matches failed. String 'SomeLong' does not match pattern '.*String.*'. . Expected string to match regex ".*String.*", but "SomeLong" does not match.
    MSTest Fluent Assertions
    StringAssert.DoesNotMatch(actual, expectedPattern);
    actual.Should().NotMatchRegex(expectedPattern);
    StringAssert.DoesNotMatch failed. String 'SomeStringLong' matches pattern '.*String.*'. . Did not expect string to match regex ".*String.*", but "SomeStringLong" matches.
     Exceptions
    MSTest Fluent Assertions
    [ExpectedException(typeof(InvalidOperationException))]
    public void MyTest()
    {
        func();
    }
    public void MyTest()
    {
        Action act = () => func();
    
        act.Should().ThrowExactly<InvalidOperationException>();
    }
    Test method threw exception System.InvalidCastException, but exception System.ArgumentNullException was expected. Exception System.InvalidCastException: Specified cast is not valid. Expected a to be thrown, but found a : System.InvalidCastException with message "Specified cast is not valid." at UnitTests2.ExceptionTests.<>c.b__1_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 31 at UnitTests2.ExceptionTests.<>c__DisplayClass1_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 34 at FluentAssertions.Specialized.ActionAssertions .InvokeSubjectWithInterception() .
    MSTest Fluent Assertions
    Action act = () => func();
    
    // MSTest V2
    Assert.ThrowsException<InvalidOperationException>(act);
    Action act = () => func();
    
    act.Should().ThrowExactly<InvalidOperationException>();
    Assert.ThrowsException failed. Threw exception InvalidCastException, but exception ArgumentNullException was expected. Exception Message: Specified cast is not valid. Stack Trace: at UnitTests2.ExceptionTests.<>c.b__2_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 44 at UnitTests2.ExceptionTests.<>c__DisplayClass2_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 47 at Microsoft.VisualStudio.TestTools.UnitTesting.Assert .ThrowsException[T](Action action, String message, Object[] parameters) Expected a to be thrown, but found a : System.InvalidCastException with message "Specified cast is not valid." at UnitTests2.ExceptionTests.<>c.b__1_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 31 at UnitTests2.ExceptionTests.<>c__DisplayClass1_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 34 at FluentAssertions.Specialized.ActionAssertions .InvokeSubjectWithInterception() .
    MSTest Fluent Assertions
    [ExpectedException(typeof(SystemException),
                       AllowDerivedTypes = true)]
    public void MyTest()
    {
        func();
    }
    public void MyTest()
    {
        Action act = () => func();
    
        act.Should().Throw<InvalidOperationException>();
    }
    Test method threw exception System.InvalidCastException, but exception System.ArgumentException or a type derived from it was expected. Exception System.InvalidCastException: Specified cast is not valid. Expected a to be thrown, but found a : System.InvalidCastException with message "Specified cast is not valid." at UnitTests2.ExceptionTests.<>c.b__3_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 57 at UnitTests2.ExceptionTests.<>c__DisplayClass3_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 60 at FluentAssertions.Specialized .ActionAssertions.InvokeSubjectWithInterception().
    MSTest Fluent Assertions
    [ExpectedException(typeof(InvalidOperationException))]
    public void MyTest()
    {
        try
        {
            func();
        }
        catch (InvalidOperationException ex)
        {
            ex.Message.Should().Be(errorMessage);
            throw;
        }
    }
    public void MyTest()
    {
        Action act = () => func();
    
        act.Should().ThrowExactly<InvalidOperationException>()
            .WithMessage(errorMessage);
    }
    Expected string to be "expectedMessage" with a length of 15, but "actualMessage" has a length of 13. Expected exception message to match the equivalent of "expectedMessage", but "actualMessage" does not.

    xUnit Migration

    The examples below show how you might write equivalent xUnit assertions using Fluent Assertions including the failure message from each case. We think this is both a useful migration guide and a convincing argument for switching.

    If you see something missing, please consider submitting a pull request.

     Assert
    xUnit Fluent Assertions
    Assert.True(actual);
    actual.Should().BeTrue();
    Assert.True() Failure Expected: True Actual: False Expected actual to be True, but found False.
    xUnit Fluent Assertions
    Assert.False(actual);
    actual.Should().BeFalse();
    Assert.False() Failure Expected: False Actual: True Expected actual to be False, but found True.
    xUnit Fluent Assertions
    Assert.Equal(expected, actual);
    actual.Should().Be(expected);
    Assert.Equal() Failure: Values differ Expected: 2 Actual: 1 Expected actual to be 2, but found 1.
    xUnit Fluent Assertions
    Assert.NotEqual(expected, actual);
    actual.Should().NotBe(expected);
    Assert.NotEqual() Failure: Values are equal Expected: Not 1 Actual: 1 Did not expect actual to be 1.
    xUnit Fluent Assertions
    Assert.Null(actual);
    actual.Should().BeNull();
    Assert.Null() Failure: Value is not null Expected: null Actual: Object { } Expected actual to be <null>, but found System.Object (HashCode=33420276).
    xUnit Fluent Assertions
    Assert.NotNull(actual);
    actual.Should().NotBeNull();
    Assert.NotNull() Failure: Value is null Expected actual not to be <null>.
    xUnit Fluent Assertions
    Assert.Same(expected, actual);
    actual.Should().BeSameAs(expected);
    Assert.Same() Failure: Values are not the same instance Expected: { OtherProperty = "expected", SomeProperty = 2 } Actual: { OtherProperty = "actual", SomeProperty = 1 } Expected actual to refer to { OtherProperty = "expected", SomeProperty = 2 }, but found { OtherProperty = "actual", SomeProperty = 1 }.
    xUnit Fluent Assertions
    Assert.NotSame(expected, actual);
    actual.Should().NotBeSameAs(expected);
    Assert.NotSame() Failure: Values are the same instance Did not expect actual to refer to { OtherProperty = "expected", SomeProperty = 1 }.
    xUnit Fluent Assertions
    Assert.Contains(expected, actual);
    actual.Should().Contain(expected);
    Assert.Contains() Failure: Item not found in collection Collection: [1, 2] Not found: 3 Expected actual {1, 2} to contain 3.
    xUnit Fluent Assertions
    Assert.DoesNotContain(unexpected, actual);
    actual.Should().NotContain(unexpected);
    Assert.DoesNotContain() Failure: Item found in collection ↓ (pos 1) Collection: [1, 2] Found: 2 Expected actual {1, 2} to not contain 2.
    xUnit Fluent Assertions
    Assert.Empty(actual);
    actual.Should().BeEmpty();
    Assert.Empty() Failure: Collection was not empty Collection: [1] Expected actual to be empty, but found at least one item {1}.
    xUnit Fluent Assertions
    Assert.NotEmpty(actual);
    actual.Should().NotBeEmpty();
    Assert.NotEmpty() Failure: Collection was empty Expected actual not to be empty.
    xUnit Fluent Assertions
    Assert.Single(actual);
    actual.Should().ContainSingle();
    Assert.Single() Failure: The collection contained 2 items Collection: [1, 2] Expected actual to contain a single item, but found {1, 2}.
    xUnit Fluent Assertions
    Assert.IsType<T>(actual);
    actual.Should().BeOfType<T>();
    Assert.IsType() Failure: Value is not the exact type Expected: typeof(string) Actual: typeof(int) Expected type to be System.String, but found System.Int32.
    xUnit Fluent Assertions
    Assert.IsAssignableFrom<T>(actual);
    actual.Should().BeAssignableTo<T>();
    Assert.IsAssignableFrom() Failure: Value is an incompatible type Expected: typeof(string) Actual: typeof(int) Expected actual to be assignable to System.String, but System.Int32 is not.
    xUnit Fluent Assertions
    Assert.Throws<TException>(() => ...);
    action.Should().ThrowExactly<TException>();
    Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(System.InvalidOperationException) Actual: typeof(System.ArgumentException) Expected type to be System.InvalidOperationException, but found System.ArgumentException.
    See Also

    Extensibility