XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.XUnit

【DLL】提供方便快捷的测试,无需编写Main方法,可直接添加特性在类或方法上进行测试

公开
关注 0 Fork 0 Star 0
README.md

XFEExtension.NetCore.XUnit 4.0

An independent .NET 10 test and statistical benchmark runner. Despite the historical package name, this project is not based on or affiliated with xUnit.net.

Tests

[TestFixture]
public class CalculatorTests
{
    [BeforeEach]
    public void SetUp() { }

    [TestCase(1, 2, 3)]
    [TestCase(2, 3, 5)]
    public void Adds(int left, int right, int expected)
        => Assert.Equal(expected, left + right);

    [Test]
    public async Task CompletesAsync()
        => await Task.Delay(1);
}

The incremental source generator discovers tests at build time, emits strongly typed invokers, and generates an entry point when the project does not already have one. async void tests are rejected because no runner can await their completion reliably.

Run tests directly:

dotnet run -c Release
dotnet run -c Release -- --filter Calculator --category Unit

Different classes run in parallel by default while methods in one class remain serial. Use [Collection], [NonParallel], [Timeout], and [Isolated] for shared resources and process isolation. Results are exported as console output, JSON, and JUnit XML.

The console automatically selects Simplified Chinese when the current UI culture is Chinese and otherwise falls back to English. Override it at any time with --language en, --language zh, or --language auto. The adaptive interface includes a two-column run card, encoding-safe status badges, aligned test durations, hierarchical failure details, success-rate and slow-test summaries, benchmark environment information, convergence warnings, and a detailed benchmark table. Redirected output remains plain text and honors NO_COLOR.

Benchmarks

public class ParserBenchmarks
{
    [Params(10, 100)]
    public int Count { get; set; }

    [Benchmark(Baseline = true)]
    public int Baseline() => Enumerable.Range(0, Count).Sum();

    [Benchmark]
    public int Candidate() => Enumerable.Range(0, Count).Aggregate(0, (sum, value) => sum + value);
}

Benchmarks are opt-in and must be run from an optimized Release build:

dotnet run -c Release -- --benchmarks
dotnet run -c Release -- --benchmarks --quick

The balanced job pilots invocation count, targets approximately 500 ms iterations, performs 6–50 warmups and 15–100 measurements, calibrates a return-shape-compatible empty workload, and stops at an approximately 2% relative error target using a 99.9% confidence interval. Raw samples, outliers, allocation, and GC counts are retained in JSON; Markdown and CSV summaries are also produced. Results below measurable infrastructure overhead are reported as such rather than presented as precise single-operation timings.

Performance gating is explicit:

dotnet run -c Release -- --benchmarks --baseline previous/benchmark-results.json --max-regression 0.05

Configuration

Project defaults may be stored in xfe.runsettings.json. Precedence is CLI, method/class/assembly attributes, configuration file, then built-in defaults.

{
  "language": "Auto",
  "tests": { "parallel": true, "maxParallelism": 8 },
  "benchmark": { "targetIterationMilliseconds": 500, "maxRelativeError": 0.02 },
  "reports": { "artifactsPath": "XfeTestArtifacts" }
}

Useful commands include --tests, --benchmarks, --all, --list, --filter, --category, --parallel, --no-parallel, --fail-fast, --explicit, --language, --artifacts, --baseline, --max-regression, and --help.

Extensions

Register one or more assembly-level [UseExtension(typeof(...))] attributes to add an ITestReporter, IBenchmarkExporter, or a single ITestActivator. A [MemberData] member may return either an enumerable of rows or an ITestCaseDataSource. The activator owns test/fixture creation and asynchronous disposal for the run.

The analyzer rejects async void and invalid lifecycle signatures. Migration diagnostics for the 3.x attributes include code fixes; the code-fix assembly is kept separate from the command-line analyzer/generator so Release builds do not acquire a Roslyn Workspaces dependency.

Migrating from 3.x

CTest, MTest, MRTest, SMTest, SetUp, and XFECode remain source-compatible for the 4.x line and are marked obsolete. Migrate to TestFixture, Test, TestCase, Benchmark, BeforeEach, and Assert. SMTest, SMNTest, SMRTest, and SMNRTest keep their original single-run behavior: they run in the default test mode without command-line options, await asynchronous results, compare legacy expected return values, and show all captured console output in a dedicated compatibility panel. Modern Benchmark methods remain opt-in through --benchmarks. The compatibility surface will be removed in 5.0.

LICENSE MIT

MIT License

Copyright (c) 2023 XFE studio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.