Siemens.AspNet.CodeRules.MSTest 7.7.4

Prefix Reserved
This package has a SemVer 2.0.0 package version: 7.7.4+3.
dotnet add package Siemens.AspNet.CodeRules.MSTest --version 7.7.4
                    
NuGet\Install-Package Siemens.AspNet.CodeRules.MSTest -Version 7.7.4
                    
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="Siemens.AspNet.CodeRules.MSTest" Version="7.7.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Siemens.AspNet.CodeRules.MSTest" Version="7.7.4" />
                    
Directory.Packages.props
<PackageReference Include="Siemens.AspNet.CodeRules.MSTest" />
                    
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 Siemens.AspNet.CodeRules.MSTest --version 7.7.4
                    
#r "nuget: Siemens.AspNet.CodeRules.MSTest, 7.7.4"
                    
#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 Siemens.AspNet.CodeRules.MSTest@7.7.4
                    
#: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=Siemens.AspNet.CodeRules.MSTest&version=7.7.4
                    
Install as a Cake Addin
#tool nuget:?package=Siemens.AspNet.CodeRules.MSTest&version=7.7.4
                    
Install as a Cake Tool

Siemens.AspNet.CodeRules.MSTest

One setup class. Every code rule becomes its own MSTest test. No test classes to write.

Runs code rules as MSTest tests. You register your rules in one ICodeRuleSetup; a source generator (shipped inside this package) reads the registrations at build time and writes one test per rule, grouped into one test class per group. Test Explorer, dotnet test, filters by category and CI test results work as for any other test.

public sealed class CodeRuleSetup : ICodeRuleSetup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCodeRuleEngine("My.slnx", o => o.AddRulePack<NamingRulePack>());
    }
}

That is the whole test project.


Quick start

  1. Create a normal MSTest project (Microsoft.NET.Test.Sdk, MSTest.TestAdapter, MSTest.TestFramework).

  2. Add the adapter and your rule packs:

    dotnet add package Siemens.AspNet.CodeRules.MSTest
    dotnet add package My.Team.CodeRules          # a rule pack, or write rules directly in this project
    
  3. Add exactly one class that implements ICodeRuleSetup (see above) and register the rules with AddRule<TRule>(), Add<TRule>(), AddRulePack<TPack>() or AddRulesFrom(typeof(X).Assembly).

  4. Build and run dotnet test.

The package brings the engine, the contracts and the source generator. One PackageReference, nothing else to wire up.

Optional: <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> writes the generated tests to obj/.../generated/CodeRules.MsTest.g.cs so you can read them without the IDE.


What you get

  • One test per rule, one test class per group: CodeRules.Generated.Code, CodeRules.Generated.Solution, CodeRules.Generated.Controllers, ...
  • Test name <ID>_<ClassName> when the rule ID is a constant string, otherwise the class name.
  • Categories CodeRules, CodeRules <Group> and CodeRules <Tag>: dotnet test --filter "TestCategory=CodeRules Naming"
  • Rules from packs and local rules of the test project; a rule the setup does not register gets no test.
  • Compiled packs get the same tests as rules in source: ID, title and tags come from the pack's [assembly: CodeRuleDescriptor] (written by the generator in Siemens.AspNet.CodeRules.Contracts).
  • Skipped rules (IRule.SkipReason, SkipRule<TRule>(reason)) show up as [Ignore]d tests with the reason.
  • A guard test fails when a registered rule has no generated test, so no rule is silently lost.
  • Parallel by design: the engine creates every context once and thread safe; the solution is parsed once per test run. Add [assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] to run the rule tests in parallel.

A failing rule

Mode Set Looks like
Human (default) nothing framed sections: rule, why, a findings table, then per finding the current code, the suggested code and the fix; file links open in the IDE
AI o.UseOutputMode(CodeRuleOutputMode.Ai) in the setup, or CodeRuleSettings__OutputMode=ai (or TestSdkSettings__OutputMode=ai for API tests and code rules together) the JSON shared by all hosts, with rerun = dotnet test --filter "FullyQualifiedName~<RuleClass>"

The environment variable wins over the setup. Order, .runsettings example: Output mode.


Build warnings

ID Meaning
CODERULE_GEN_0001 no ICodeRuleSetup in the project
CODERULE_GEN_0002 more than one ICodeRuleSetup
CODERULE_GEN_0003 a registration the generator cannot read (e.g. AddRulesFrom(variable)); its rules get no test and the guard test fails

Also runs in the CLI

Referencing this package makes the project a code rule project: build/Siemens.AspNet.CodeRules.MSTest.props sets <IsCodeRuleProject>true</IsCodeRuleProject> (only as a default, set it to false to opt out). The coderules CLI (Siemens.AspNet.CodeRules.Cli) finds such projects and runs the same ICodeRuleSetup natively, without a test host:

dotnet tool install -g Siemens.AspNet.CodeRules.Cli
coderules run                 # same rules, same result as dotnet test
coderules run --format ai     # one JSON document for AI agents and scripts
Package Purpose
Siemens.AspNet.CodeRules.Contracts write rules and rule packs
Siemens.AspNet.CodeRules.Engine ICodeRuleSetup, registration options, RuleTester<TRule> for rule unit tests
Siemens.AspNet.CodeRules.XUnit / Siemens.AspNet.CodeRules.NUnit the same for xUnit v3 / NUnit
Siemens.AspNet.CodeRules.Cli the coderules .NET tool

License

Copyright (c) Siemens AG. See LICENSE.md in the package.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
7.7.4 29 11/14/2026
7.7.3 37 11/13/2026
7.7.2 42 11/13/2026
7.7.1 29 11/12/2026
7.7.0 39 11/12/2026

## Next

First release. Runs code rules as MSTest tests.

- A source generator (inside the package) reads the `ICodeRuleSetup` and writes one test per registered rule, one test class per group, with the categories `CodeRules`, `CodeRules <Group>` and `CodeRules <Tag>`.
- Skipped rules show up as ignored tests with their reason; a guard test fails when a registered rule has no generated test.
- Build warnings `CODERULE_GEN_0001`-`0003` for a missing or duplicate setup and registrations the generator cannot read.
- Human or AI failure output (`UseOutputMode`, `CodeRuleSettings__OutputMode`).
- Marks the project as a code rule project (`IsCodeRuleProject`), so the `coderules` CLI runs the same rules.