Skip to main content

xUnit

The code fix of aweXpectM003 rewrites each Xunit.Assert.* call into the corresponding Expect.That(actual).<Assertion>(expected) form and swaps the argument order of xUnit (Assert.Equal(expected, actual)) to the "actual first" convention of aweXpect.

Before (xUnit)
string title = // ...
string[] tracks = // ...
Dictionary<string, int> playCounts = // ...
double duration = // ...
object album = // ...
Action play = // ...

Assert.NotNull(title);
Assert.StartsWith("Abbey", title);
Assert.Equal(17, tracks.Length);
Assert.Contains("Come Together", tracks);
Assert.DoesNotContain("Yesterday", tracks);
Assert.Contains("Something", playCounts);
Assert.True(duration > 0, "the album has a duration");
Assert.Equal(47.1, duration, 1);
Assert.IsType<string>(album);
Assert.Throws<InvalidOperationException>(play);
aweXpectM003Migration available.

The examples show the code as the code fix writes it; the await is added in a second step.

A note in the last column marks a rewrite that behaves differently in some cases; it links to the matching entry on Behavioral differences from xUnit.

Basic​

xUnit constructRewritten to
Assert.Fail("msg")Fail.Test("msg")
Assert.Skip("msg")Skip.Test("msg")
Assert.Null(subject)Expect.That(subject).IsNull()
Assert.NotNull(subject)Expect.That(subject).IsNotNull()
Assert.Same(expected, actual)Expect.That(actual).IsSameAs(expected)
Assert.NotSame(expected, actual)Expect.That(actual).IsNotSameAs(expected)
Before (xUnit)
Assert.Fail("The album has no tracks");
aweXpectM003Migration available.

Booleans​

xUnit constructRewritten to
Assert.True(subject)Expect.That(subject).IsTrue()
Assert.False(subject)Expect.That(subject).IsFalse()
Assert.True(subject, "msg")Expect.That(subject).IsTrue().Because("msg")
Assert.False(subject, "msg")Expect.That(subject).IsFalse().Because("msg")

Equality​

xUnit constructRewritten toNote
Assert.Equal(expected, actual)Expect.That(actual).IsEqualTo(expected)nested collections, mixed number types, DateTime kind
Assert.NotEqual(expected, actual)Expect.That(actual).IsNotEqualTo(expected)
Assert.Equal(expected, actual, tolerance) (double / float / TimeSpan)Expect.That(actual).IsEqualTo(expected).Within(tolerance)DateTime kind
Assert.NotEqual(expected, actual, tolerance) (double / float)Expect.That(actual).IsNotEqualTo(expected).Within(tolerance)
Assert.Equal(expected, actual, precision) (double / float / decimal)Expect.That(Math.Round(actual, precision)).IsEqualTo(Math.Round(expected, precision))
Assert.NotEqual(expected, actual, precision) (double / float / decimal)Expect.That(Math.Round(actual, precision)).IsNotEqualTo(Math.Round(expected, precision))

The precision of xUnit rounds both values to the given number of decimal places, which no tolerance can express, so the rewrite rounds them the same way. A MidpointRounding argument is passed on to Math.Round, and a using for System is added when necessary. Any other additional argument, such as a comparer or ignoreCase: true, leaves the assertion for manual migration.

Before (xUnit)
double duration = // ...

Assert.Equal(47.1, duration, 1);
aweXpectM003Migration available.

Strings​

xUnit constructRewritten toNote
Assert.Contains(expected, actual)Expect.That(actual).Contains(expected)empty substrings
Assert.DoesNotContain(unexpected, actual)Expect.That(actual).DoesNotContain(unexpected)null subjects
Assert.StartsWith(expected, actual)Expect.That(actual).StartsWith(expected)empty substrings
Assert.EndsWith(expected, actual)Expect.That(actual).EndsWith(expected)empty substrings
Assert.Empty(actual)Expect.That(actual).IsEmpty()
Assert.NotEmpty(actual)Expect.That(actual).IsNotEmpty()

A constant StringComparison on Assert.Contains, Assert.DoesNotContain, Assert.StartsWith or Assert.EndsWith is translated when aweXpect compares the same way: StringComparison.Ordinal adds nothing and StringComparison.OrdinalIgnoreCase appends .IgnoringCase(). A culture-sensitive comparison, such as StringComparison.CurrentCultureIgnoreCase, or a comparison that is not a constant leaves the assertion for manual migration.

Before (xUnit)
string title = // ...

Assert.Contains("Road", title);
Assert.EndsWith("Road", title);
Assert.StartsWith("ABBEY", title, StringComparison.OrdinalIgnoreCase);
aweXpectM003Migration available.

Collections​

xUnit constructRewritten toNote
Assert.Distinct(actual)Expect.That(actual).All().AreUnique()
Assert.Distinct(actual, comparer)Expect.That(actual).All().AreUnique().Using(comparer)hash codes
Assert.Contains(expected, collection)Expect.That(collection).Contains(expected)
Assert.Contains(collection, predicate)Expect.That(collection).Contains(predicate)
Assert.DoesNotContain(unexpected, collection)Expect.That(collection).DoesNotContain(unexpected)
Assert.Contains(key, dictionary)Expect.That(dictionary).ContainsKey(key)
Assert.DoesNotContain(key, dictionary)Expect.That(dictionary).DoesNotContainKey(key)
Assert.Empty(collection)Expect.That(collection).IsEmpty()
Assert.NotEmpty(collection)Expect.That(collection).IsNotEmpty()

A comparer on Assert.Contains or Assert.DoesNotContain leaves the assertion for manual migration, as on Assert.Equal.

Before (xUnit)
Dictionary<string, int> playCounts = // ...

Assert.Contains("Something", playCounts);
Assert.DoesNotContain("Yesterday", playCounts);
aweXpectM003Migration available.

Exceptions​

xUnit constructRewritten to
Assert.Throws<T>(callback) / ThrowsAsync<T>(callback)Expect.That(callback).ThrowsExactly<T>()
Assert.Throws<T>(paramName, callback) / ThrowsAsync<T>(...)Expect.That(callback).ThrowsExactly<T>().WithParamName(paramName)
Assert.Throws(typeof(T), callback)Expect.That(callback).ThrowsExactly(typeof(T))
Assert.ThrowsAny<T>(callback) / ThrowsAnyAsync<T>(callback)Expect.That(callback).Throws<T>()
Before (xUnit)
Action play = // ...

Assert.Throws<InvalidOperationException>(play);
Assert.ThrowsAny<InvalidOperationException>(play);
aweXpectM003Migration available.

Types​

xUnit constructRewritten toNote
Assert.IsAssignableFrom<T>(actual)Expect.That(actual).Is<T>()
Assert.IsNotAssignableFrom<T>(actual)Expect.That(actual).IsNot<T>()null subjects
Assert.IsType<T>(actual)Expect.That(actual).IsExactly<T>()
Assert.IsType<T>(actual, exactMatch: false)Expect.That(actual).Is<T>()
Assert.IsNotType<T>(actual)Expect.That(actual).IsNotExactly<T>()null subjects
Assert.IsNotType<T>(actual, exactMatch: false)Expect.That(actual).IsNot<T>()null subjects

The non-generic overloads (Assert.IsType(typeof(T), actual) etc.) are migrated to the matching non-generic aweXpect form. An exactMatch argument that is not a true or false literal leaves the assertion for manual migration.

Before (xUnit)
Exception error = // ...

Assert.IsType<ArgumentException>(error);
Assert.IsType<ArgumentException>(error, exactMatch: false);
aweXpectM003Migration available.

Behavioral differences​

A few rewrites pass or fail for different inputs than the original xUnit assertion, by a deliberate choice of aweXpect. Each case is listed with a way to restore the original behavior on Behavioral differences from xUnit.