From 2d719cc91f69585fd73e8da0b3f68e26ea68332c Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Fri, 22 Nov 2024 14:09:35 +0100 Subject: [PATCH] fix(xunit): support netstandard2.0 --- src/Playwright.Xunit/Playwright.Xunit.csproj | 4 +-- src/Playwright.Xunit/WorkerAwareTest.cs | 30 +++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Playwright.Xunit/Playwright.Xunit.csproj b/src/Playwright.Xunit/Playwright.Xunit.csproj index f51973e50..e33e22594 100644 --- a/src/Playwright.Xunit/Playwright.Xunit.csproj +++ b/src/Playwright.Xunit/Playwright.Xunit.csproj @@ -9,7 +9,7 @@ and fixtures to enable using it within xUnit. icon.png - net8.0 + netstandard2.0 true true Microsoft.Playwright.Xunit @@ -35,9 +35,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - diff --git a/src/Playwright.Xunit/WorkerAwareTest.cs b/src/Playwright.Xunit/WorkerAwareTest.cs index 3885a0755..43c971b54 100644 --- a/src/Playwright.Xunit/WorkerAwareTest.cs +++ b/src/Playwright.Xunit/WorkerAwareTest.cs @@ -25,7 +25,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Microsoft.Playwright.Core; @@ -34,10 +33,10 @@ namespace Microsoft.Playwright.Xunit; -public class WorkerAwareTest : ExceptionCapturer, IAsyncLifetime +public class WorkerAwareTest : ExceptionCapturer { private static readonly ConcurrentStack _allWorkers = new(); - private Worker? _currentWorker = null!; + private Worker _currentWorker = null!; internal class Worker { @@ -58,8 +57,9 @@ public async Task RegisterService(string name, Func> factory) wher return (_currentWorker.Services[name] as T)!; } - public virtual Task InitializeAsync() + async public override Task InitializeAsync() { + await base.InitializeAsync().ConfigureAwait(false); if (!_allWorkers.TryPop(out _currentWorker!)) { _currentWorker = new(); @@ -69,11 +69,11 @@ public virtual Task InitializeAsync() { AssertionsBase.SetDefaultTimeout(PlaywrightSettingsProvider.ExpectTimeout.Value); } - return Task.CompletedTask; } - public virtual async Task DisposeAsync() + public async override Task DisposeAsync() { + await base.DisposeAsync().ConfigureAwait(false); if (TestOk) { foreach (var kv in _currentWorker.Services) @@ -107,16 +107,26 @@ public interface IWorkerService /// Note: There is no way of getting the test status in xUnit in the dispose method. /// For more information, see: https://stackoverflow.com/questions/28895448/current-test-status-in-xunit-net /// -public class ExceptionCapturer +public class ExceptionCapturer : IAsyncLifetime { - protected static bool TestOk { get; private set; } = true; + protected bool TestOk { get; private set; } = true; - [ModuleInitializer] - public static void Setup() + public ExceptionCapturer() { AppDomain.CurrentDomain.FirstChanceException += (_, e) => { TestOk = false; }; } + + public virtual Task InitializeAsync() + { + TestOk = true; + return Task.CompletedTask; + } + + public virtual Task DisposeAsync() + { + return Task.CompletedTask; + } }