-
Notifications
You must be signed in to change notification settings - Fork 758
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
[Feature request] Tests with supported platform attributes #2820
Comments
In v3, we have added xunit/src/xunit.v3.core.tests/Acceptance/DynamicAssemblyAcceptanceTests.cs Lines 21 to 25 in 379140a
We do not have We are unlikely to provide this as a standalone attribute, even in the context of v3, for the same reason that we rejected the common We do have an open discussion issue on whether we should add the ability to do |
Skimming through the documentation for this attribute, this doesn't actually feel like a runtime request, and I don't think you need us to do anything here. Instead, it feels to me like what you should be doing is multi-targeting your test projects with a generic TFMs (like I'll admit that the documentation I've found here so far is terrible when trying to determine what the actual platforms are and what pre-processor directives might exist for those platforms by default, so my suggestion here may be wrong, but I think it should set you in the right direction. |
Yeah, this seems to be functional right now. Project: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
</Project> Unit test: using System.Runtime.Versioning;
using Xunit;
namespace Empty;
public static class MyMath
{
[SupportedOSPlatform("windows")]
public static int Add(int x, int y) => x + y;
public static int Subtract(int x, int y) => x - y;
}
public class UnitTest
{
#if WINDOWS
[Fact]
public void TestAdd()
{
Assert.Equal(5, MyMath.Add(3, 2));
}
#endif
[Fact]
public void TestSubtract()
{
Assert.Equal(1, MyMath.Subtract(3, 2));
}
} Running
You can see we have 1 successful test from (When I tried to use |
RE:
I found this note from the TFM design
With that in mind, I don't think OS specific TFMs were intended for this, because conditionally including tests falls into the varying behavior bucket. I'm still looking at what they mean by "using RIDs". The RID catalog does include linux targets. |
I hacked together a solution by pushing behavior into a custom Project: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project> The custom using System.Runtime.InteropServices;
namespace OSPlatformFact;
public sealed class OSPlatformFactAttribute : FactAttribute
{
public OSPlatformFactAttribute(string platformName)
{
var platform = OSPlatform.Create(platformName.ToUpperInvariant());
if (!RuntimeInformation.IsOSPlatform(platform))
{
Skip = $"Test requires {platformName}";
}
}
} Tests which use both the platform compatibility analyzer attributes as well as my custom Fact attribute: using System.Runtime.Versioning;
namespace OSPlatformFact;
public static class MyMath
{
[SupportedOSPlatform("windows")]
public static int Add(int x, int y) => x + y;
[SupportedOSPlatform("linux")]
public static int Multiply(int x, int y) => x * y;
public static int Subtract(int x, int y) => x - y;
}
[SupportedOSPlatform("windows")]
public class WindowsMathTests
{
[OSPlatformFact("windows")]
public void Test_only_on_windows()
{
Assert.Equal(5, MyMath.Add(3, 2));
}
}
[SupportedOSPlatform("linux")]
public class LinuxMathTests
{
[OSPlatformFact("linux")]
public void Test_only_on_linux()
{
Assert.Equal(6, MyMath.Multiply(3, 2));
}
}
public class MathTests
{
[Fact]
public void Test_on_all_platforms()
{
Assert.Equal(1, MyMath.Subtract(3, 2));
}
} Output of
Output of
I'm still looking for a solution where I only need to add the |
I'm not 100% sure myself what the easiest way to do this is right now. I would have to sit down and do some experiments, but surely they would be involved. FWIW, the skipping version will be easier in v3, because you can just write this as the first line of your test: Assert.SkipUnless(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "Test requires Windows"); And we use this today to skip categories of tests which don't support Mono:
As mentioned above, I have an open issue as to whether or not this would be something that could get integrated into the |
Hi,
.NET 5 added a new platform compatibility analyzer, and along with it, new attributes to use in library code:
I'm using this for example to annotate code that only works on Windows. I have tests for this code, but right now I don't really have a good way to skip these tests when running in Linux.
It would be super cool if you could annotate a Fact with a platform compatibility attribute, such that the runner will dynamically skip it on unsupported platforms.
Thanks for your consideration.
The text was updated successfully, but these errors were encountered: