Install
dotnet add package Meziantou.Framework.Assertions --version 2.0.6
| Product | Versions |
|---|---|
| .NET |
README
Meziantou.Framework.Assertions
Assertion helpers for .NET tests.
Assertion methods
Assert provides the following assertion methods:
True: Asserts a condition is true.False: Asserts a condition is false.Null: Asserts a value is null.NotNull: Asserts a value is not null and returns it.Same: Asserts two references point to the same instance.NotSame: Asserts two references are different instances.Equal: Asserts two values are equal.NotEqual: Asserts two values are not equal.EqualUnordered: Asserts two sequences contain the same values regardless of order.NotEqualUnordered: Asserts two sequences differ when compared without ordering.Equivalent: Asserts two objects are equal by recursively comparing members.NotEquivalent: Asserts two objects are structurally different.IsType: Asserts a value is exactly of the specified type and returns it.IsNotType: Asserts a value is not exactly of the specified type.IsAssignableTo: Asserts a value is assignable to the specified type and returns it.IsNotAssignableTo: Asserts a value is not assignable to the specified type.InRange: Asserts a value is within the inclusive range.NotInRange: Asserts a value is outside the inclusive range.StartsWith: Asserts text or sequence starts with an expected prefix.EndsWith: Asserts text or sequence ends with an expected suffix.DoesNotStartWith: Asserts text or sequence does not start with a prefix.DoesNotEndWith: Asserts text or sequence does not end with a suffix.Contains: Asserts a value, key, predicate, or subsequence is present.DoesNotContain: Asserts a value, key, predicate, or subsequence is not present.Matches: Asserts text matches a regular expression.DoesNotMatch: Asserts text does not match a regular expression.All: Asserts all items in a collection satisfy an assertion.DoesNotAll: Asserts at least one item in a collection does not satisfy a predicate.Collection: Asserts collection items match a list of inspectors.Distinct: Asserts all items in a sequence are distinct.NotDistinct: Asserts a sequence contains duplicate items.Empty: Asserts a collection or sequence is empty.NotEmpty: Asserts a collection or sequence is not empty.Single: Asserts a sequence has exactly one item and returns it.HasCount: Asserts a collection has an exact count.DoesNotHaveCount: Asserts a collection count differs from the specified count.HasCountGreaterThan: Asserts a collection count is greater than a value.HasCountGreaterThanOrEqual: Asserts a collection count is greater than or equal to a value.HasCountLessThan: Asserts a collection count is less than a value.HasCountLessThanOrEqual: Asserts a collection count is less than or equal to a value.ProperSubset: Asserts a set is a proper subset of another set.NotProperSubset: Asserts a set is not a proper subset of another set.ProperSuperset: Asserts a set is a proper superset of another set.NotProperSuperset: Asserts a set is not a proper superset of another set.Throws: Asserts a specific exception type is thrown and returns it.ThrowsAny: Asserts an exception assignable to the specified type is thrown and returns it.ThrowsAsync: Asserts a specific exception type is thrown by an async delegate.ThrowsAnyAsync: Asserts an assignable exception type is thrown by an async delegate.DoesNotThrow: Asserts a delegate completes without throwing.DoesNotThrowAny: Asserts a delegate completes without throwing any exception type.Raise: Asserts a specific event is raised and captures event data.RaiseAny: Asserts an event with a compatible event args type is raised.DoesNotRaise: Asserts a specific event is not raised.DoesNotRaiseAny: Asserts no compatible event is raised.Fail: Fails the test with a custom message.XunitSkip: Skips the running xunit test with the specified reason.
Use as the default Assert class
You can override the Assert type used in your tests with a global alias:
<Using Include="Meziantou.Framework.Assertions.Assert" Alias="Assert" />
or:
global using Assert = Meziantou.Framework.Assertions.Assert;
Analyzer rules
The package ships analyzers and code fixes to help write clearer assertions.
| Id | Category | Description | Severity | Enabled |
|---|---|---|---|---|
MFAS0001 |
Assertions | Pass the expected value before the actual value | Warning | ✔️ |
MFAS0002 |
Assertions | Use Assert.Same instead of Assert.ReferenceEquals | Error | ✔️ |
MFAS0003 |
Assertions | Do not use Assert.IsType with static or abstract types | Error | ✔️ |
MFAS0004 |
Assertions | Use Assert.Empty or Assert.NotEmpty for zero count checks | Warning | ✔️ |
MFAS0005 |
Assertions | Use specialized count assertions | Warning | ✔️ |
MFAS0006 |
Assertions | Use Assert.Null for null comparisons | Warning | ✔️ |
MFAS0007 |
Assertions | Use Assert.NotNull for null comparisons | Warning | ✔️ |
MFAS0008 |
Assertions | Do not use Assert.Null with value types | Error | ✔️ |
MFAS0009 |
Assertions | Do not use Assert.NotNull with value types | Error | ✔️ |
MFAS0010 |
Assertions | Do not use Assert.Same with value types | Error | ✔️ |
MFAS0011 |
Assertions | Do not use Assert.NotSame with value types | Error | ✔️ |
MFAS0012 |
Assertions | Use Assert.IsAssignableTo for type pattern checks | Warning | ✔️ |
MFAS0013 |
Assertions | Use Assert.IsNotAssignableTo for type pattern checks | Warning | ✔️ |
MFAS0014 |
Assertions | Use Assert.Matches instead of Assert.True(Regex.IsMatch(...)) | Warning | ✔️ |
MFAS0015 |
Assertions | Use Assert.DoesNotMatch instead of Assert.False(Regex.IsMatch(...)) | Warning | ✔️ |
MFAS0016 |
Assertions | Use Assert.Contains instead of Assert.True(string.Contains(...)) | Warning | ✔️ |
MFAS0017 |
Assertions | Use Assert.DoesNotContain instead of Assert.False(string.Contains(...)) | Warning | ✔️ |
MFAS0018 |
Assertions | Use Assert.StartsWith instead of Assert.True(string.StartsWith(...)) | Warning | ✔️ |
MFAS0019 |
Assertions | Use Assert.DoesNotStartWith instead of Assert.False(string.StartsWith(...)) | Warning | ✔️ |
MFAS0020 |
Assertions | Use Assert.EndsWith instead of Assert.True(string.EndsWith(...)) | Warning | ✔️ |
MFAS0021 |
Assertions | Use Assert.DoesNotEndWith instead of Assert.False(string.EndsWith(...)) | Warning | ✔️ |
MFAS0022 |
Assertions | Use Assert.Contains instead of Assert.True(collection.Contains(...)) | Warning | ✔️ |
MFAS0023 |
Assertions | Use Assert.DoesNotContain instead of Assert.False(collection.Contains(...)) | Warning | ✔️ |
MFAS0024 |
Assertions | Use Assert.Contains instead of Assert.True(collection.Any(...)) | Warning | ✔️ |
MFAS0025 |
Assertions | Use Assert.DoesNotContain instead of Assert.False(collection.Any(...)) | Warning | ✔️ |
MFAS0026 |
Assertions | Use Assert.All instead of Assert.True(collection.All(...)) | Warning | ✔️ |
MFAS0027 |
Assertions | Use Assert.DoesNotAll instead of Assert.False(collection.All(...)) | Warning | ✔️ |
MFAS0028 |
Assertions | Use Assert.NotEmpty instead of Assert.True(collection.Any()) | Warning | ✔️ |
MFAS0029 |
Assertions | Use Assert.Empty instead of Assert.False(collection.Any()) | Warning | ✔️ |
MFAS0030 |
Assertions | Use Assert.Empty or Assert.NotEmpty instead of a count assertion | Warning | ✔️ |
MFAS0031 |
Assertions | Use Assert.Equal instead of Assert.True(a == b) | Warning | ✔️ |
MFAS0032 |
Assertions | Use Assert.NotEqual instead of Assert.True(a != b) | Warning | ✔️ |
MFAS0033 |
Assertions | Use Assert.Equal instead of Assert.True(a.Equals(b)) | Warning | ✔️ |
MFAS0034 |
Assertions | Use Assert.NotEqual instead of Assert.False(a.Equals(b)) | Warning | ✔️ |
MFAS0035 |
Assertions | Use Assert.Equal instead of Assert.True(actual.SequenceEqual(expected)) | Warning | ✔️ |
MFAS0036 |
Assertions | Use Assert.NotEqual instead of Assert.False(actual.SequenceEqual(expected)) | Warning | ✔️ |
MFAS0037 |
Assertions | Use Assert.Same instead of Assert.True(ReferenceEquals(a, b)) | Warning | ✔️ |
MFAS0038 |
Assertions | Use Assert.NotSame instead of Assert.False(ReferenceEquals(a, b)) | Warning | ✔️ |
MFAS0039 |
Assertions | Use Assert.InRange instead of Assert.True(low <= x && x <= high) | Warning | ✔️ |
MFAS0040 |
Assertions | Use Assert.NotInRange instead of Assert.False(low <= x && x <= high) | Warning | ✔️ |
MFAS0041 |
Assertions | Use Assert.ProperSubset instead of Assert.True(set.IsProperSubsetOf(other)) | Warning | ✔️ |
MFAS0042 |
Assertions | Use Assert.NotProperSubset instead of Assert.False(set.IsProperSubsetOf(other)) | Warning | ✔️ |
MFAS0043 |
Assertions | Use Assert.ProperSuperset instead of Assert.True(set.IsProperSupersetOf(other)) | Warning | ✔️ |
MFAS0044 |
Assertions | Use Assert.NotProperSuperset instead of Assert.False(set.IsProperSupersetOf(other)) | Warning | ✔️ |
MFAS0045 |
Assertions | Use Assert.False instead of Assert.True(!condition) | Warning | ✔️ |
MFAS0046 |
Assertions | Use Assert.IsType instead of Assert.True(x.GetType() == typeof(T)) | Warning | ✔️ |
MFAS0047 |
Assertions | Use Assert.IsNotType instead of Assert.False(x.GetType() == typeof(T)) | Warning | ✔️ |
MFAS0048 |
Assertions | Await assertions that return a Task | Error | ✔️ |
MFAS0049 |
Assertions | Assertion always produces the same result | Error | ✔️ |
MFAS0050 |
Assertions | Use Assert.Fail instead of an assertion that always fails | Warning | ✔️ |
MFAS0051 |
Assertions | Use Assert.Contains instead of Assert.NotEmpty(collection.Where(...)) | Warning | ✔️ |
MFAS0052 |
Assertions | Use Assert.DoesNotContain instead of Assert.Empty(collection.Where(...)) | Warning | ✔️ |
MFAS0053 |
Assertions | Use Assert.Single with a predicate instead of Assert.Single(collection.Where(...)) | Warning | ✔️ |
MFAS0054 |
Assertions | Use Assert.Contains with the expected value instead of an equality predicate | Warning | ✔️ |
MFAS0055 |
Assertions | Use Assert.DoesNotContain with the expected value instead of an equality predicate | Warning | ✔️ |
Changes from Previous Version
No structural changes between v2.0.5 and v2.0.6.
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Compatibility metadata lists 2 target frameworks; confirm the exact target used by your application.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.