Siemens.AspNet.CodeRules.NUnit
7.7.4
Prefix Reserved
dotnet add package Siemens.AspNet.CodeRules.NUnit --version 7.7.4
NuGet\Install-Package Siemens.AspNet.CodeRules.NUnit -Version 7.7.4
<PackageReference Include="Siemens.AspNet.CodeRules.NUnit" Version="7.7.4" />
<PackageVersion Include="Siemens.AspNet.CodeRules.NUnit" Version="7.7.4" />
<PackageReference Include="Siemens.AspNet.CodeRules.NUnit" />
paket add Siemens.AspNet.CodeRules.NUnit --version 7.7.4
#r "nuget: Siemens.AspNet.CodeRules.NUnit, 7.7.4"
#:package Siemens.AspNet.CodeRules.NUnit@7.7.4
#addin nuget:?package=Siemens.AspNet.CodeRules.NUnit&version=7.7.4
#tool nuget:?package=Siemens.AspNet.CodeRules.NUnit&version=7.7.4
Siemens.AspNet.CodeRules.NUnit
One setup class. Every code rule becomes its own NUnit test. No test classes to write.
Runs code rules as NUnit 4 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 [TestFixture] per
group. Test Explorer, dotnet test, category filters 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 NUnit project (
Microsoft.NET.Test.Sdk,NUnit,NUnit3TestAdapter).Add the adapter and your rule packs:
dotnet add package Siemens.AspNet.CodeRules.NUnit 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.NUnit.g.cs so you can read them without the IDE.
What you get
- One test per rule, one
[TestFixture]per group:CodeRules.Generated.Code,CodeRules.Generated.Solution, ... - Test name
<ID>_<ClassName>(the class name alone when the ID is computed), no description. - Categories in clear text:
CodeRules,CodeRules <Group>,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 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.
- The engine creates every context once and thread safe: the solution is parsed once per test run.
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 |
| AI | o.UseOutputMode(CodeRuleOutputMode.Ai) in the setup, or CodeRuleSettings__OutputMode=ai (or TestSdkSettings__OutputMode=ai) |
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.NUnit.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.MSTest / Siemens.AspNet.CodeRules.XUnit |
the same for MSTest / xUnit v3 |
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
- NUnit (>= 4.6.1)
- 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 NUnit 4 tests.
- A source generator (inside the package) reads the `ICodeRuleSetup` and writes one test per registered rule, one `[TestFixture]` 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.