ConventionAssertions 0.25.2

dotnet add package ConventionAssertions --version 0.25.2
                    
NuGet\Install-Package ConventionAssertions -Version 0.25.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ConventionAssertions" Version="0.25.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ConventionAssertions" Version="0.25.2" />
                    
Directory.Packages.props
<PackageReference Include="ConventionAssertions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ConventionAssertions --version 0.25.2
                    
#r "nuget: ConventionAssertions, 0.25.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ConventionAssertions@0.25.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ConventionAssertions&version=0.25.2
                    
Install as a Cake Addin
#tool nuget:?package=ConventionAssertions&version=0.25.2
                    
Install as a Cake Tool

ConventionAssertions

Code conventions for .NET, checked from your unit tests.

CI Coverage

ConventionAssertions is a lightweight .NET 8 library for writing your own code conventions for types, methods and properties as small, reusable C# classes, and checking them from your unit tests. You pick the members to check with a scan, and every violation is reported in one failure.

Example

Pick the methods to check with a scan, and give each convention its own test:

public class MethodConventionTests
{
    private static readonly IConventionTargets<MethodInfo> _asyncMethods = ConventionTargets.FromMethods(
        a => a.FromAssemblyContaining<OrderService>(),
        methods: m => m.Where(x => x.IsAwaitable));

    private static readonly IConventionTargets<MethodInfo> _voidMethods = ConventionTargets.FromMethods(
        a => a.FromAssemblyContaining<OrderService>(),
        methods: m => m.Where(x => x.IsVoid));

    [Fact]
    public void Async_methods_end_with_Async() =>
        Convention.For(_asyncMethods, a => a.Assert<HasNameSuffix>(c => c.Suffix = "Async"));

    [Fact]
    public void Void_methods_are_not_async() =>
        Convention.For(_voidMethods, a => a.Assert<IsNotAsync>());
}

A failing convention lists every method that breaks it, not just the first one:

Found 2.
Method MyApp.Services.OrderService.Load(int) name must end with Async.
Method MyApp.Services.CustomerService.Delete(int) name must end with Async.

Check out the Quickstart for more examples!

Features at a glance

  • Checks types, methods, properties and assemblies, all as plain Type, MethodInfo, PropertyInfo and Assembly.
  • Conventions are small classes you write once and reuse, or a lambda inline for a one-off rule (writing your own).
  • Every violation in one failure, not just the first one.
  • A scan that matches nothing fails by default, so a typo in a filter can't make a test pass silently.
  • Suppress a known violation with a justification, right on the test that owns the rule.
  • Works with any test framework, and has a single dependency.

Built-in conventions

In the ConventionAssertions.Conventions namespace:

Convention Targets Checks that the target
DoesNotExposeTypes methods, properties doesn't expose any of a set of types, in its signature or nested in the types it exposes (configurable).
HasCustomAttribute types, methods, properties has a given attribute (configurable).
HasNamespaceSegment types has a given segment in its namespace (configurable).
HasNamePrefix types, methods, properties has a name starting with a given prefix (configurable).
HasNameSuffix types, methods, properties has a name ending with a given suffix (configurable).
HasNoAssemblyReferenceWithNameSegment assemblies references no assembly, directly or indirectly, with a given segment in its name (configurable).
HasNoParameterAssignableTo methods has no parameter assignable to a given type (configurable).
HasNoPropertyAssignableTo types has no public property assignable to a given type (configurable).
HasNoPublicConstructors types has no public constructor.
HasNoSetter properties has no public setter.
HasParameterAssignableTo methods has a parameter assignable to a given type, optionally an exact number of them (configurable).
HasPublicStaticParseMethod types has a public static T Parse(string) method.
HasPublicStaticTryParseMethod types has a public static bool TryParse(string, out T) method.
IsAbstractOrSealed types is abstract or sealed.
IsAssignableTo types is assignable to a given type, including open generic types (configurable).
IsInternal types is internal.
IsNotAsync methods is not async.
IsNotAssignableTo types is not assignable to a given type, including open generic types (configurable).
IsPublic types is public.
IsSealed types is sealed.
IsStatic types is a static class.
NameIsSnakeCase types, methods has a lower_snake_case name.

In the ConventionAssertions.AspNetCore.Conventions namespace (the separate, optional ConventionAssertions.AspNetCore package, also for .NET 8):

Convention Targets Checks that the target
ControllerActionHasAuthorization methods is a controller action with an Authorize or AllowAnonymous attribute on it or its controller.

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ConventionAssertions:

Package Downloads
ConventionAssertions.AspNetCore

ASP.NET Core conventions for ConventionAssertions, such as checking that controller actions have [Authorize] or [AllowAnonymous].

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.25.2 114 11/8/2026
0.25.1 116 11/7/2026
0.25.0 119 11/7/2026
0.24.1 115 11/6/2026
0.24.0 116 11/6/2026
0.23.2 120 11/6/2026
0.23.1 109 11/6/2026
0.23.0 113 11/6/2026
0.22.0 133 11/6/2026
0.21.1 107 11/6/2026
0.21.0 112 11/6/2026
0.20.0 112 11/6/2026
0.19.1 117 11/3/2026
0.19.0 120 11/3/2026
0.18.0 122 11/2/2026
0.17.0 115 11/1/2026
0.16.4 13,917 3/23/2025
0.16.1 426 3/23/2025
0.14.0 8,394 3/8/2024
0.13.4 307 3/7/2024
Loading failed