ANcpLua.NET.Sdk.Test) provides xUnit v3 with Microsoft Testing Platform and injects base classes for integration testing.
Microsoft Testing Platform (MTP)
The SDK uses xUnit v3 with Microsoft Testing Platform instead of VSTest.Auto-Configuration
MTP requires test projects to output executables. The SDK handles this automatically when you referencexunit.v3.mtp-v2:
CLI Syntax
MTP uses different command syntax than VSTest:.NET 10 includes native MTP support via
global.json. The -- separator is
only needed for .NET 8/9 with explicit MTP opt-in.Troubleshooting
| Exit Code | Cause | Fix |
|---|---|---|
| 5 | Unknown CLI option | Use MTP syntax (--filter-method), not VSTest (--filter "FQN~") |
| 8 | Zero tests discovered | Check filter pattern; ensure test methods are public |
| N/A | Tests not found | Verify xunit.v3.mtp-v2 reference; SDK sets OutputType=Exe |
xUnit v3 TestContext
xUnit v3 replaces constructor-injectedITestOutputHelper with ambient TestContext. Access test output anywhere without passing it through constructor chains.
Before vs After
| Aspect | xUnit v2 (Before) | xUnit v3 (After) |
|---|---|---|
| Test constructor params | fixture, testOutputHelper | fixture |
| Fields to store | 2 | 1 |
| Builder constructor params | 4 | 3 |
| Boilerplate per test class | ~3 lines | 0 |
| How output is accessed | Passed through chain | Ambient TestContext.Current |
Migration Example
IntegrationTestBase
In-memory TestServer for fast API testing.WebApplicationFactory<TProgram> internally.
KestrelTestBase
Real Kestrel server for HTTP/2, WebSockets, SSE, or Playwright.UseKestrel(0).
FakeLogger
See Extensions forFakeLogCollector test helpers.
AOT/Trim Testing
The SDK provides infrastructure for testing Native AOT and trimmed applications. Tests run as separate processes withPublishAot=true or PublishTrimmed=true to verify runtime behavior.
Enabling AOT Testing
ANcpLua.AotTesting.Attributes.
Writing AOT Tests
AOT tests are methods returningint with exit code 100 for success:
Test Attributes
[AotTest]
[AotTest]
Publishes with
PublishAot=true and executes the native binary.[TrimTest]
[TrimTest]
Publishes with
PublishTrimmed=true for testing IL trimming.TrimAssert Helpers
Verify types are trimmed or preserved at runtime:Feature Switches
Disable runtime features during tests:| Constant | Feature Switch |
|---|---|
JsonReflection | System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault |
DebuggerSupport | System.Diagnostics.Debugger.IsSupported |
EventSourceSupport | System.Diagnostics.Tracing.EventSource.IsSupported |
MetricsSupport | System.Diagnostics.Metrics.Meter.IsSupported |
XmlSerialization | System.Xml.XmlSerializer.IsEnabled |
BinaryFormatter | System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization |
InvariantGlobalization | System.Globalization.Invariant |
HttpActivityPropagation | System.Net.Http.EnableActivityPropagation |
StartupHooks | System.StartupHookProvider.IsSupported |
How It Works
- Discovery: MSBuild target scans for
[AotTest]/[TrimTest]methods - Project Generation: Creates temporary
.csprojfor each test - Publish: Runs
dotnet publishwith AOT/Trim flags - Execute: Runs the published executable
- Validate: Checks exit code (100 = success)
Exit Code Convention
| Code | Meaning |
|---|---|
| 100 | Success |
| 1-99 | Test failure |
| -1 | TrimAssert.TypeTrimmed failed (type exists) |
| -2 | TrimAssert.TypePreserved failed (type missing) |
Related Analyzers
| Rule | Description |
|---|---|
| AL0041 | [AotTest]/[TrimTest] must return int |
| AL0042 | Should return 100 for success |
| AL0043 | [TrimSafe] cannot call [RequiresUnreferencedCode] |
| AL0044 | [AotSafe] cannot call [RequiresDynamicCode] |
| AL0052 | [AotSafe] must not call [AotUnsafe] |
| AL0053 | Unnecessary [AotUnsafe] attribute |
| AL0094 | Avoid dynamic keyword in AOT |
| AL0095 | Avoid Expression.Compile() in AOT |
| AL0101 | Avoid Activator.CreateInstance in AOT |
| AL0102 | Avoid Type.GetType with dynamic name |
