48

I'm making the transition from NUnit to XUnit (in C#), and I was writing some "Integrated Tests" (ITs) that I don't necessarily want the test runner to run as part of my automated build process. I typically do this for manually testing, when the full end to end process might not work because of environmental factors (missing data, etc.)

In NUnit, you could mark a test with the Explicit attribute and it would just get skipped by the test runner (unless you marked the test with a specific Category attribute and told the test runner to target that category explicitly).

Does XUnit have a similar way to exclude tests from the test runner?

neumann1990
  • 1,205
  • 1
  • 11
  • 18

5 Answers5

51

Jimmy Bogard solved this with a nice RunnableInDebugOnlyAttribute. See this blog post: Run tests explicitly in xUnit.net

public class RunnableInDebugOnlyAttribute : FactAttribute
{
    public RunnableInDebugOnlyAttribute()
    {
        if (!Debugger.IsAttached)
        {
            Skip = "Only running in interactive mode.";
        }
    }
}
Brennan Pope
  • 1,014
  • 7
  • 11
27

I think I found it. Apparently, you can modify your [Fact] attribute like so: [Fact(Skip="reason")]. This will skip the test, but you'll have no way of running it manually without modifying the attribute back to normal.

I'll keep looking for a better way.

neumann1990
  • 1,205
  • 1
  • 11
  • 18
2

I used

#if DEBUG
// Must test manually with https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer
[Fact]
#endif

So this is not even considered to be a test on a build server while a developer who usually builds a debug version will notice that this test fails

Darth Jurassic
  • 628
  • 5
  • 8
1

I wish there was a better way to do this. I too have a bunch of "performance" type tests that I want to keep around so I can manually run them but I don't want them automatically ran by the test runner. The solution I'm using (and I don't love it) is to change

[Fact] to //[Fact]

The main reason I don't love this solution is because it requires an extra step when I DO want to manually run the test, ie I have to uncomment the [Fact] first. But it does work for me albeit in a kludgy sort of way.

What I really want is a way to market the test as a test but one that can only be invoked manually and not by the test runner. But I have yet to find a way to do that.

RonC
  • 31,330
  • 19
  • 94
  • 139
-1

You can use the [Trait] attribute for that, like in the xunit example, e.g.,

[Trait ("Category", "Integration")]

This project takes it a little further and inherits categories like unit, integration, etc.

robi-y
  • 1,687
  • 16
  • 25
  • 10
    This does not clearly represent a way to decorate a test method in such a way that it will be able to be manually tested but not run with the entire test suite. – Brennan Pope Oct 27 '17 at 14:43
  • 2
    You can (at least in Visual Studio) sort tests by Trait and run them. And on lipeline you can exclude them by filtering by category. We marked them as "ManualTest" to make it clear. – garcipat May 19 '22 at 11:05