Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you mark XUnit tests as Explicit?

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?

like image 403
neumann1990 Avatar asked May 19 '16 21:05

neumann1990


People also ask

What xUnit attributes mark a method as a unit test?

The only unit test currently implemented is the ValidPassword() method. This method is decorated with the Fact attribute, which tells xUnit that this is a test.

What is Fact attribute in xUnit?

xUnit uses the [Fact] attribute to denote a parameterless unit test, which tests invariants in your code. In contrast, the [Theory] attribute denotes a parameterised test that is true for a subset of data. That data can be supplied in a number of ways, but the most common is with an [InlineData] attribute.

Is xUnit a test runner?

The MSBuild runner in xUnit.net v2 is capable of running unit tests from both xUnit.net v1 and v2. It can run multiple assemblies at the same time, and build file options can be used to configuration the parallelism options used when running the tests.

How do I create a test report in xUnit?

One way to do it: add " --logger=trx" to the "dotnet test" command and then use the build step "Process xUnit test result report" from the "xUnit plugin". Use the option "MSTest-Version N/A (default) Pattern" and set pattern to "*/. trx". This is the answer!


2 Answers

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.";         }     } } 
like image 110
Brennan Pope Avatar answered Oct 02 '22 22:10

Brennan Pope


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.

like image 22
neumann1990 Avatar answered Oct 02 '22 20:10

neumann1990