Skip to content

Commit

Permalink
fix(xunit): support netstandard2.0 (#3063)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Nov 22, 2024
1 parent 8cd88de commit c9f0914
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Playwright.Xunit/BrowserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public override async Task InitializeAsync()

public override async Task DisposeAsync()
{
await base.DisposeAsync().ConfigureAwait(false);
if (TestOk)
{
foreach (var context in _contexts)
Expand All @@ -58,5 +57,6 @@ public override async Task DisposeAsync()
}
_contexts.Clear();
Browser = null!;
await base.DisposeAsync().ConfigureAwait(false);
}
}
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,10 +69,9 @@ public virtual Task InitializeAsync()
{
AssertionsBase.SetDefaultTimeout(PlaywrightSettingsProvider.ExpectTimeout.Value);
}
return Task.CompletedTask;
}

public virtual async Task DisposeAsync()
public async override Task DisposeAsync()
{
if (TestOk)
{
Expand All @@ -90,6 +89,7 @@ public virtual async Task DisposeAsync()
}
_currentWorker.Services.Clear();
}
await base.DisposeAsync().ConfigureAwait(false);
}
}

Expand All @@ -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 c9f0914

Please sign in to comment.