NetPlatform
The NetPlatformAttribute is used to specify platforms for which a test or fixture should be run. It is a modern
replacement for the Platform attribute, using platform names based on the .NET TargetFramework
conventions as documented in CA1416: Validate platform
compatibility.
Platforms are specified using case-insensitive string values and may be either included or excluded from the run by use
of the Include or Exclude properties respectively. Multiple comma-separated values may be specified.
If a test or fixture with the NetPlatformAttribute does not satisfy the specified platform requirements, it is
skipped.
Note
This attribute was introduced in NUnit 4.6 and provides better alignment with .NET's built-in platform checking
mechanisms compared to the older PlatformAttribute.
Test Fixture Syntax
[TestFixture]
[NetPlatform(Include = "windows")]
public class WindowsOnlyTests
{
[Test]
public void TestWindowsFeature()
{
// This test only runs on Windows
Assert.Pass();
}
}
Test Syntax
[Test]
[NetPlatform(Include = "windows")]
public void RunOnWindowsOnly()
{
// This test only runs on Windows
Assert.That(OperatingSystem.IsWindows(), Is.True);
}
Excluding Platforms
[Test]
[NetPlatform(Exclude = "linux")]
public void RunOnAllExceptLinux()
{
// This test runs on Windows, macOS, etc., but not Linux
Assert.That(OperatingSystem.IsLinux(), Is.False);
}
Platform Version Requirements
You can specify minimum version requirements for platforms:
[Test]
[NetPlatform(Include = "windows10.0")]
public void RunOnWindows10OrLater()
{
// This test requires Windows 10 or later
Assert.That(OperatingSystem.IsWindowsVersionAtLeast(10), Is.True);
}
Multiple Platforms
[Test]
[NetPlatform(Include = "windows,linux")]
public void RunOnWindowsOrLinux()
{
// This test runs on Windows or Linux, but not macOS
Assert.That(
OperatingSystem.IsWindows() || OperatingSystem.IsLinux(),
Is.True);
}
Platform Specifiers
The following platform names are supported, matching the .NET SDK's supported platform identifiers:
| Platform | Description |
|---|---|
windows |
Microsoft Windows |
linux |
Linux distributions |
macos |
Apple macOS |
android |
Android |
ios |
Apple iOS |
tvos |
Apple tvOS |
watchos |
Apple watchOS |
browser |
WebAssembly in browser |
Version Specifiers
Platform names can include version numbers to specify minimum version requirements:
windows10.0- Windows 10 or laterwindows10.0.19041- Windows 10 build 19041 or latermacos10.15- macOS Catalina (10.15) or laterios14.0- iOS 14 or later
Note
For Windows, public version numbers (like Windows 7, 8, 10, 11) are automatically mapped to their internal version numbers for compatibility checking.
Comparison with PlatformAttribute
| Feature | NetPlatformAttribute | PlatformAttribute |
|---|---|---|
| Platform names | .NET SDK style (windows, linux, macos) |
Legacy style (Win, Unix, MacOsX) |
| Version support | Full version specifiers (windows10.0.19041) |
Limited version support |
| Runtime detection | Uses OperatingSystem.IsOSPlatform() |
Uses custom detection logic |
| Analyzer support | Compatible with CA1416 | Not compatible |