test: run process-wide tests serially and report constructor failures as themselves
Four App tests switched the process-wide UI language inside parallel classes, so another class's English assertion, or its constructor's reset to English, could land in the middle of them; they now live in the serial ArchiveLanguageTests and ImportLanguageTests. The App and Core test programs fail a test that leaves English outside a serial class at the switch, through the new TestContext.Current and TestContext.IsSerial. The FrameWindow cancellation test counted unobserved task exceptions while other classes ran beside it; it moves to the serial FrameWindowIsolationTests, and both unobserved-exception tests drain the finalizer queue before they start counting. The runner unwrapped TargetInvocationException only around the test method: a skip or an assertion thrown by a test class constructor or field initializer was reported as a failure reading "Exception has been thrown by the target of an invocation". Constructors are unwrapped the same way now. TestRunner.Discover(Type) and ExecuteAsync are public, so Tools.Tests checks the runner with nested fixtures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
13 files changed
+381
-167
No files matched your search
@@ -9,18 +9,33 @@ namespace Titano.Testing;
|
||||
/// </summary>
|
||||
public sealed class TestContext : IDisposable
|
||||
{
|
||||
private static readonly AsyncLocal<TestContext?> CurrentTest = new();
|
||||
private readonly ConcurrentQueue<string> _log = new();
|
||||
private readonly List<string> _tempDirectories = [];
|
||||
private readonly Lock _gate = new();
|
||||
|
||||
internal TestContext(string testName, CancellationToken cancellation)
|
||||
internal TestContext(string testName, bool isSerial, CancellationToken cancellation)
|
||||
{
|
||||
TestName = testName;
|
||||
IsSerial = isSerial;
|
||||
Cancellation = cancellation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The test running on this flow of execution (its constructor, body and the tasks it starts); null outside tests.
|
||||
/// Lets a test program guard process-wide state, which only a serial test may change.
|
||||
/// </summary>
|
||||
public static TestContext? Current
|
||||
{
|
||||
get => CurrentTest.Value;
|
||||
internal set => CurrentTest.Value = value;
|
||||
}
|
||||
|
||||
public string TestName { get; }
|
||||
|
||||
/// <summary>True when the test's class is <see cref="SerialAttribute"/>: no other test runs beside it.</summary>
|
||||
public bool IsSerial { get; }
|
||||
|
||||
/// <summary>Signalled when the test times out or the run is aborted.</summary>
|
||||
public CancellationToken Cancellation { get; }
|
||||
|
||||
|
||||
@@ -100,30 +100,41 @@ public static class TestRunner
|
||||
}
|
||||
|
||||
foreach (var type in types.Where(t => t is { IsClass: true, IsPublic: true } && !t.IsAbstract).OrderBy(t => t.FullName, StringComparer.Ordinal))
|
||||
list.AddRange(Discover(type));
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tests of one class, in declaration order. Assembly discovery takes top-level classes only, so a class nested
|
||||
/// in a test class is a fixture that runs only when a test hands it to <see cref="ExecuteAsync"/>.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<TestCase> Discover(Type type)
|
||||
{
|
||||
var list = new List<TestCase>();
|
||||
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).OrderBy(m => m.MetadataToken))
|
||||
{
|
||||
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).OrderBy(m => m.MetadataToken))
|
||||
if (method.GetCustomAttribute<TestAttribute>() is not null)
|
||||
{
|
||||
if (method.GetCustomAttribute<TestAttribute>() is not null)
|
||||
{
|
||||
list.Add(new TestCase(type, method, null, null));
|
||||
}
|
||||
else if (method.GetCustomAttribute<TheoryAttribute>() is not null)
|
||||
{
|
||||
foreach (var c in method.GetCustomAttributes<CaseAttribute>())
|
||||
list.Add(new TestCase(type, method, c.Arguments, c.Name));
|
||||
}
|
||||
list.Add(new TestCase(type, method, null, null));
|
||||
}
|
||||
else if (method.GetCustomAttribute<TheoryAttribute>() is not null)
|
||||
{
|
||||
foreach (var c in method.GetCustomAttributes<CaseAttribute>())
|
||||
list.Add(new TestCase(type, method, c.Arguments, c.Name));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static async Task<TestResult> ExecuteAsync(TestCase test)
|
||||
/// <summary>Runs one test (constructor, body, disposal) under its timeout and returns its outcome.</summary>
|
||||
public static async Task<TestResult> ExecuteAsync(TestCase test)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(test);
|
||||
if (test.StaticSkipReason is { } reason)
|
||||
return new TestResult(test, TestOutcome.Skipped, TimeSpan.Zero, reason, null, string.Empty);
|
||||
|
||||
using var cts = new CancellationTokenSource();
|
||||
using var context = new TestContext(test.FullName, cts.Token);
|
||||
using var context = new TestContext(test.FullName, test.Serial, cts.Token);
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
@@ -152,29 +163,21 @@ public static class TestRunner
|
||||
|
||||
private static async Task InvokeAsync(TestCase test, TestContext context)
|
||||
{
|
||||
TestContext.Current = context;
|
||||
object? instance = null;
|
||||
try
|
||||
{
|
||||
if (!test.Method.IsStatic)
|
||||
{
|
||||
var ctor = test.Type.GetConstructor([typeof(TestContext)]);
|
||||
instance = ctor is not null
|
||||
instance = Unwrapped(() => ctor is not null
|
||||
? ctor.Invoke([context])
|
||||
: Activator.CreateInstance(test.Type);
|
||||
: Activator.CreateInstance(test.Type));
|
||||
}
|
||||
|
||||
var parameters = test.Method.GetParameters();
|
||||
var args = BuildArguments(test, parameters, context);
|
||||
object? returned;
|
||||
try
|
||||
{
|
||||
returned = test.Method.Invoke(instance, args);
|
||||
}
|
||||
catch (TargetInvocationException tie) when (tie.InnerException is not null)
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(tie.InnerException).Throw();
|
||||
throw;
|
||||
}
|
||||
var returned = Unwrapped(() => test.Method.Invoke(instance, args));
|
||||
|
||||
switch (returned)
|
||||
{
|
||||
@@ -200,6 +203,23 @@ public static class TestRunner
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes a reflection call and rethrows what the constructor or method threw, so a skip stays a skip and a failure
|
||||
/// carries its own message and stack trace instead of "Exception has been thrown by the target of an invocation".
|
||||
/// </summary>
|
||||
private static object? Unwrapped(Func<object?> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return call();
|
||||
}
|
||||
catch (TargetInvocationException tie) when (tie.InnerException is not null)
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(tie.InnerException).Throw();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private static object?[] BuildArguments(TestCase test, ParameterInfo[] parameters, TestContext context)
|
||||
{
|
||||
var supplied = test.Arguments ?? [];
|
||||
|
||||
Reference in new issue
Block a user