Siemens.AspNet.CodeRules.MSTest
7.7.4
Prefix Reserved
dotnet add package Siemens.AspNet.CodeRules.MSTest --version 7.7.4
NuGet\Install-Package Siemens.AspNet.CodeRules.MSTest -Version 7.7.4
<PackageReference Include="Siemens.AspNet.CodeRules.MSTest" Version="7.7.4" />
<PackageVersion Include="Siemens.AspNet.CodeRules.MSTest" Version="7.7.4" />
<PackageReference Include="Siemens.AspNet.CodeRules.MSTest" />
paket add Siemens.AspNet.CodeRules.MSTest --version 7.7.4
#r "nuget: Siemens.AspNet.CodeRules.MSTest, 7.7.4"
#:package Siemens.AspNet.CodeRules.MSTest@7.7.4
#addin nuget:?package=Siemens.AspNet.CodeRules.MSTest&version=7.7.4
#tool nuget:?package=Siemens.AspNet.CodeRules.MSTest&version=7.7.4
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
Create a normal MSTest project (
Microsoft.NET.Test.Sdk,MSTest.TestAdapter,MSTest.TestFramework).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 projectAdd exactly one class that implements
ICodeRuleSetup(see above) and register the rules withAddRule<TRule>(),Add<TRule>(),AddRulePack<TPack>()orAddRulesFrom(typeof(X).Assembly).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>andCodeRules <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 inSiemens.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
Related packages
| 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 | Versions 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. |
-
net10.0
- MSTest.TestFramework (>= 4.5.2)
- Siemens.AspNet.CodeRules.Engine (>= 7.7.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
## 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.