Lately I was having a problem with some of my tests causing problems for other developers on the project I am working on. They were using Windows for their development while I am using Debian for my work. The application that was being tested was not designed for non Unix system – now rarely anybody is hosting they production code on Windows Server – everybody is using Unix for .net.
Still tests need to not disrupt whatever you are doing now for development. This needed to be fixed.
Some of those tests were actually only valid on Linux OS. There were no way to make it work on different Os. The quickest way to make this work was to just disable them on Windows.
This would work but is not pretty:
[Fact] public void LinuxOnly() { if (!OperatingSystem.IsWindows()) { //actual test code } }
More elegant way would be to disable them with an attribute. And it is possible. We can write our own Fact
attribute that disables test with a condition. And it is pretty easy to do and looks nice.
public class LinuxFact : FactAttribute { public LinuxOnlyFact() { if (OperatingSystem.IsWindows()) { Skip = "It is not possible to run this test on Windows OS"; } } }
Then you have apply it to a test method like Fact
attribute.
[LinuxFact] public void LinuxOnly() { //actual test code }
and test will run on Linux just fine. On Windows on the other hand it will be shown as skipped.

Happy coding!