Can you mark XUnit tests as Explicit?

一个人想着一个人 提交于 2019-12-20 17:35:04

问题


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?


回答1:


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.";
        }
    }
}



回答2:


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.




回答3:


I used

#if DEBUG
// Must test manually with https://docs.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




回答4:


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.



来源:https://stackoverflow.com/questions/37334245/can-you-mark-xunit-tests-as-explicit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!