Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xunit): support netstandard2.0 #3063

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by: Similar to 7f3b888.


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;
}
}
Loading