Skip to content

Commit

Permalink
fix(xunit): support netstandard2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Nov 22, 2024
1 parent 8cd88de commit 2d719cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/Playwright.Xunit/Playwright.Xunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
and fixtures to enable using it within xUnit.
</Description>
<PackageIcon>icon.png</PackageIcon>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RunWithWarnings>true</RunWithWarnings>
<RootNamespace>Microsoft.Playwright.Xunit</RootNamespace>
Expand All @@ -35,9 +35,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<None Include="..\Common\icon.png" Pack="true" Visible="false" PackagePath="icon.png" />
Expand Down
30 changes: 20 additions & 10 deletions src/Playwright.Xunit/WorkerAwareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,10 +33,10 @@

namespace Microsoft.Playwright.Xunit;

public class WorkerAwareTest : ExceptionCapturer, IAsyncLifetime
public class WorkerAwareTest : ExceptionCapturer
{
private static readonly ConcurrentStack<Worker> _allWorkers = new();
private Worker? _currentWorker = null!;
private Worker _currentWorker = null!;

internal class Worker
{
Expand All @@ -58,8 +57,9 @@ public async Task<T> RegisterService<T>(string name, Func<Task<T>> 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();
Expand All @@ -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)
Expand Down Expand Up @@ -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
/// </summary>
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;
}
}

0 comments on commit 2d719cc

Please sign in to comment.