如何根据当前平台跳过 xUnit 中的特定测试 [英] How do I skip specific tests in xUnit based on current platform

查看:23
本文介绍了如何根据当前平台跳过 xUnit 中的特定测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我有一个在 Windows 上构建的程序集
  • 我想在 Linux 的单声道上运行 xUnit 测试.

然而,我发现虽然其中 400 个测试可以运行(按顺序),但某些测试要么挂起 xUnit 运行程序,要么完全关闭.

我不关心某些测试是否无法在 Linux 上运行,某些测试与 DTC 和一些我们不需要在那里支持的非托管数据有关.>

然而,我想要的是对这些测试应用忽略,并在构建输出中正确标记测试被忽略的事实.

这个问题可以归结为我猜的一些可能的解决方案

  • 如何通过控制台运行程序在 xUnit 中运行特定测试?(我还没有找到这方面的文档,也许我只是不够努力)
  • 是否可以反过来说这是一个程序集,但请忽略这些特定测试"
  • 建议在这些测试上添加属性是一种更好的方法,以正式记录这些测试是特定于平台的 - 这可能吗?

如果我能避免过多地修改原始代码,那就太好了,因为代码并不是我真正可以改变的,而且应用大量跨平台的黑客攻击可能不会太顺利.

解决方案

我会避免外部化跳过测试(即,如果可能的话,一个配置/命令文件).这在某种程度上不利于使测试易于运行且值得信赖.当其他人开始参与时,在代码中忽略测试是最安全的方法.

我可以看到许多选项,这里有两个涉及修改现有代码.

选项 1 - 最具侵入性的编译时平台检测

在 VS 解决方案中,定义另一个配置,该配置定义了一个预编译器标志 MONOWIN(只是为了明确表示它是一个标志,表示它用于在 Windows 上编译的代码用于在 Mono 上使用).

然后定义一个属性,当为 Mono 编译时,该属性将使测试被忽略:

public class IgnoreOnMonoFactAttribute : FactAttribute {#if MONOWIN公共 IgnoreOnMonoFactAttribute() {Skip =忽略单声道";}#万一}

实际上很难找到这种方法的任何优势,因为它涉及对原始解决方案的模拟,并添加了另一个需要支持的配置.

选项 2 - 有点侵入性 - 运行时平台检测

这里有一个类似于 option1 的解决方案,只是不需要单独的配置:

public class IgnoreOnMonoFactAttribute : FactAttribute {公共 IgnoreOnMonoFactAttribute() {if(IsRunningOnMono()) {Skip =忽略单声道";}}///<总结>///确定运行时是否为 Mono.///取自 http://stackoverflow.com/questions/721161///</总结>/// True 如果在 Mono 中执行,否则为 false.</returns>public static bool IsRunningOnMono() {return Type.GetType("Mono.Runtime") != null;}}

注意 1

如果一个方法被[Fact][IgnoreOnMonoFact] 标记,则

xUnit runner 将运行该方法两次.(CodeRush 不这样做,在这种情况下,我认为 xUnit 是正确的).这意味着任何测试方法都必须将 [Fact] 替换为 [IgnoreOnMonoFact]

注意事项 2

CodeRush 测试运行程序仍然运行 [IgnoreOnMonoFact] 测试,但它确实忽略了 [Fact(Skip=reason")] 测试.我认为这是由于 CodeRush 反映了 xUnit 而实际上并没有在 xUnit 库的帮助下运行它.这适用于 xUnit runner.

  • I have an assembly that I've built on Windows
  • I want to run the xUnit tests on mono in Linux.

However, I have found that while 400 of these tests can run (in order), that certain tests either hang the xUnit runner, or bring it down entirely.

I don't care if certain tests are not able to run on Linux, certain tests are to do with the DTC and some unmanaged gumph that we don't need to support there.

What I do want however, is to apply an ignore to those tests, and have the fact that the test was ignored flagged properly in the build output.

The question can be boiled down to I guess a number of possible solutions

  • How do I run specific tests in xUnit via the console runner? (I haven't found documentation to this end, maybe I'm just not looking hard enough)
  • Is it possible to go the other way and say "Here is an assembly, please ignore these specific tests though"
  • Having an attribute on those tests has been suggested a better way, to formally document that these tests are platform specific - is this possible?

If I could avoid modifying the original code too much that would be grand, as the code isn't really mine to change, and applying lots of cross-platform hacks probably won't go down too well.

解决方案

I would avoid externalising skipping tests (i.e. a config/command file if it's possible). This somewhat goes against making the tests easy to run and trustworthy. Making the tests ignored in code is the safest approach when other people start to get involved.

I could see a number of options, here are two that involve modification of existing code.

Option 1 - Most intrusive, compile time platform detection

In the VS Solution, define another configuration that defines a precompiler flag MONOWIN (just so that it's explicitly a flag the says that it is for code compiled on Windows for use on Mono).

Then define an attribute that will make the test ignored when compiled for Mono:

public class IgnoreOnMonoFactAttribute : FactAttribute {
#if MONOWIN
    public IgnoreOnMonoFactAttribute() {
        Skip = "Ignored on Mono";
    }
#endif
}

It's actually hard to find any advantages to this method as it involves mocking with the original solution and adds another confiration that needs to be supported.

Option 2 - somewhat intrusive - runtime platform detection

Here is a similar solution to option1, except no separate configuration is required:

public class IgnoreOnMonoFactAttribute : FactAttribute {

    public IgnoreOnMonoFactAttribute() {
        if(IsRunningOnMono()) {
            Skip = "Ignored on Mono";
        }
    }
    /// <summary>
    /// Determine if runtime is Mono.
    /// Taken from http://stackoverflow.com/questions/721161
    /// </summary>
    /// <returns>True if being executed in Mono, false otherwise.</returns>
    public static bool IsRunningOnMono() {
        return Type.GetType("Mono.Runtime") != null;
    }
}

Note 1

xUnit runner will run a method twice if it is marked with [Fact] and [IgnoreOnMonoFact]. (CodeRush doesn't do that, in this case I assume xUnit is correct). This means that any tests methods must have [Fact] replaced with [IgnoreOnMonoFact]

Note 2

CodeRush test runner still ran the [IgnoreOnMonoFact] test, but it did ignore the [Fact(Skip="reason")] test. I assume it is due to CodeRush reflecting xUnit and not actually running it with the aid of xUnit libraries. This works fine with xUnit runner.

这篇关于如何根据当前平台跳过 xUnit 中的特定测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆