Install
dotnet add package Meziantou.Framework.Assertions --version 2.0.0
| 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.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.
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 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 | ✔️ |
Changes from Previous Version
v1.0.1v2.0.0
Removed Frameworks
net8.0net9.0
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.