如何使用 Xunit 2.0 动态跳过测试?

2024-02-16

Xunit 1.9.x 为用户提供了DynamicSkipExample.cs https://github.com/xunit/xunit/blob/v1/samples/AssertExamples/DynamicSkipExample.cs帮助他设置动态跳过的示例[Fact].

事实证明,这在执行一些跨平台开发时非常有用。当由于底层上下文(操作系统、文件系统等)而无法正确运行测试时,这允许暂时忽略测试。

然而,这个例子已经在提交中被删除了2deeff5 https://github.com/xunit/xunit/commit/2deeff5cb60bdb8c12fd4f3cfaa38035cdbc7ba6正在通往 2.0 版本的路上。

如何通过 Xunit 2.0 的扩展点之一重新实现这样的功能?

Note:xUnit 跟踪器中提出了有关此主题的问题。看xunit/xunit#250 https://github.com/xunit/xunit/issues/250.


[更新:XUnit v2.0 (RTM) 现已推出,并且直接支持可跳过的测试。 Use [Fact (Skip = "specific reason")] ]

请注意,XUnit v2.0 尚未发布。此示例与 Xunit 2.0 beta5 兼容,您可以在 nuget.org 上找到它。可能还有其他方法可以实现此目的(因为这只是我举的例子)。

1) 定义一个属性来装饰您的测试。

/// <inheritdoc/>
[AttributeUsage( AttributeTargets.Method, AllowMultiple = false )]
[XunitTestCaseDiscoverer( "SkippableTestCaseDiscoverer", "assemblynamehere" )]
public sealed class SkippableTestAttribute : FactAttribute
{
    public SkippableTestAttribute() { }
}    

2) 创建您的发现者。 (我们按照以下代码示例进行操作https://github.com/xunit/xunit/blob/2d9ce6fbd75e91a69a0cc83e1bc3d4eab18b2c6c/src/xunit.execution/Sdk/Frameworks/TheoryDiscoverer.cs https://github.com/xunit/xunit/blob/2d9ce6fbd75e91a69a0cc83e1bc3d4eab18b2c6c/src/xunit.execution/Sdk/Frameworks/TheoryDiscoverer.cs)

/// <summary>
/// Implementation of <see cref="IXunitTestCaseDiscoverer"/> that supports finding test cases
/// on methods decorated with <see cref="SkippableTestAttribute"/>.
/// </summary>
public class SkippableTestCaseDiscoverer : IXunitTestCaseDiscoverer
{
    /// <inheritdoc/>
    [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Needs to return test case." )]
    public IEnumerable<IXunitTestCase> Discover( ITestMethod testMethod, IAttributeInfo factAttribute )
    {
        // some of this code is from https://github.com/xunit/xunit/blob/2d9ce6fbd75e91a69a0cc83e1bc3d4eab18b2c6c/src/xunit.execution/Sdk/Frameworks/TheoryDiscoverer.cs
        if ( factAttribute.GetNamedArgument<string>( "Skip" ) != null )
            return new[] { new XunitTestCase( testMethod ) };

        var dataAttributes = testMethod.Method.GetCustomAttributes( typeof( DataAttribute ) );

        try
        {
            var results = new List<XunitTestCase>();

            foreach ( var dataAttribute in dataAttributes )
            {
                var discovererAttribute = dataAttribute.GetCustomAttributes( typeof( DataDiscovererAttribute ) ).First();
                var discoverer = ExtensibilityPointFactory.GetDataDiscoverer( discovererAttribute );
                if ( !discoverer.SupportsDiscoveryEnumeration( dataAttribute, testMethod.Method ) )
                    return new XunitTestCase[] { new XunitTheoryTestCase( testMethod ) };

// These lines are our "custom dynamic logic" that determines if we should skip the test.
                IEnumerable<object[]> data = discoverer.GetData( dataAttribute, testMethod.Method );

                if ( data == null )
                {
                    var test = new SkippableTestCase( testMethod );
                    test.SkipTest( "Test not configured with any " );
                    return new[] { test };
                }
                foreach ( var dataRow in data )
                {
                    // Attempt to serialize the test case, since we need a way to uniquely identify a test
                    // and serialization is the best way to do that. If it's not serializable, this will
                    // throw and we will fall back to a single theory test case that gets its data
                    // at runtime.
                    var testCase = new XunitTestCase( testMethod, dataRow );
                    SerializationHelper.Serialize( testCase );
                    results.Add( testCase );
                }
            }

            if ( results.Count == 0 )
                results.Add( new LambdaTestCase( testMethod,
                                               () => { throw new InvalidOperationException( String.Format( "No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name ) ); } ) );

            return results;
        }
        catch
        {
            return new XunitTestCase[] { new XunitTheoryTestCase( testMethod ) };
        }
    }
}

3) 创建一个实现 IXunitTestCase 的类(因为默认基类不允许修改跳过原因)。

// Class is similar to XunitTestCase 
[Serializable]
public class SkippableTestCase : TestMethodTestCase, IXunitTestCase 
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Obsolete("Called by the de-serializer", error: true)]
    public SkippableTestCase() { }

    /// <summary>
    /// Initializes a new instance of the <see cref="SkippableTestCase"/> class.
    /// </summary>
    /// <param name="testMethod">The test method this test case belongs to.</param>
    /// <param name="testMethodArguments">The arguments for the test method.</param>
    public SkippableTestCase(ITestMethod testMethod, object[] testMethodArguments = null)
        : base(testMethod, testMethodArguments) { }

    /// <inheritdoc />
    protected SkippableTestCase(SerializationInfo info, StreamingContext context)
        : base(info, context) { }

    public void SkipTest( string reason )
    {
        base.SkipReason = reason;
    }

    /// <inheritdoc/>
    public virtual Task<RunSummary> RunAsync( IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource )
    {
        return new XunitTestCaseRunner( this, DisplayName, SkipReason, constructorArguments, TestMethodArguments, messageBus, aggregator, cancellationTokenSource ).RunAsync();
    }
}

对于如何设置 base.SkipReason,您有多种选择。在此示例中,创建了一个公共方法。

此示例将跳过具有以下内容的测试MemberDataAttribute不返回任何数据行。您可以修改它以返回SkippableTestCase根据您的标准。例如,此发现会跳过周日的测试。

/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Needs to return test case." )]
public IEnumerable<IXunitTestCase> Discover( ITestMethod testMethod, IAttributeInfo factAttribute )
{
    if ( DateTime.Today.DayOfWeek == DayOfWeek.Sunday )
    {
        var test = new SkippableTestCase( testMethod );
        test.SkipTest( "Test not configured with any " );
        return new[] { test };
    }
    else
    {
        return new[] { new XunitTestCase( testMethod ) };
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Xunit 2.0 动态跳过测试? 的相关文章

随机推荐

  • Swift 错误“静态成员不能用于类型的实例”

    我想重载我的结构的运算符 但收到消息 静态成员 评级 不能在 GlobalSettings 类型的实例上使用 我已经阅读了这个错误的几个答案 但那里的解决方案对我根本没有帮助 我怎么解决这个问题 struct GlobalSettings
  • Android 内容提供商保护级别和不同的密钥

    我有一个应用程序提供专业版 付费 和免费版 添加支持 两个版本都使用相同的密钥存储进行签名 但每个版本都有自己的密钥别名 现在 我想开发一个与两个版本兼容的插件 通过内容提供商提供数据 数据很敏感 因此我需要只能从我的应用程序 专业版和免费
  • JSLint 的 JS 验证器替代品?

    是 JSLintit就js验证工具而言 还有其他工具吗 据我所知有两个不错的 ESLint http eslint org JSHint http jshint com
  • 多重身份验证使用一页登录laravel

    我是 Laravel 的新手 但我对多重身份验证 Laravel 很好奇 我想制作一些有两个规则的网站 客户和卖家 但是 我希望他们使用相同的登录表单登录 我尝试使用 php artisan make auth 但我不明白如何在一个控制器中
  • 在活动目录中,mailNickname 有何用途?

    在 Active Directory 中 mailNickname 是否应始终等于 samaccountname 或者 它应该始终等于邮件属性 减去 domain 我询问的原因是 我们最近将每个人的主要电子邮件地址更改为first last
  • DeviceContext (HDC) 中的 Alpha 通道

    请帮助我处理 HDC 中的 alpha 通道 我让 HDC dc 抛出 CreateCompatibleDC 然后调用 CreateDIBSection 并可以在内存中找到图像字节 然后调用 DrawFrameControl 到此 dc 一
  • Heroku:SSL 未启用

    所以我的 Rails 3 2 1 应用程序在 Heroku 上运行良好 然后我设置config force ssl true在我的生产配置文件中 打开文件后 我现在收到 SSL 未启用 错误 我以为 Heroku 允许你附带他们的 SSL
  • Powershell 字节数组到 INT

    我有一个包含两个值的字节数组 07 and DE 十六进制 我需要做的是以某种方式连接07DE并从该十六进制值获取十进制值 在这种情况下 它是2014 My code This line gives 11 bytes worth of in
  • 如何使用 Raphael.js 旋转 div?

    我是 Raphael 的新手 我真的很困惑 我想使用 Raphael 的按钮来旋转 div 及其内容 理想情况下 我希望有一个平滑的动画 当单击按钮时 动画从 0 度到 90 度 然后当再次单击按钮时 动画将反转 我想我会更改鼠标单击时的
  • 使用 jQuery 的延迟链接 ajax 请求

    我有一个网络应用程序必须多次调用服务器 到目前为止 我有一个很长的嵌套回调链 但我想使用 jQuerywhen then等功能 但是 使用后我似乎无法让东西再次运行then when get pages run tool html then
  • Objective-C 中的“超级”是什么? (自我!=超级)?

    我已阅读下面的问题和故事SEEMS simple Objective C 中的 super 到底是什么 https stackoverflow com questions 3095360 what exactly is super in o
  • 在存储过程中创建临时表

    我正在为 MySQL 编写第一个存储过程 存储过程需要创建一个临时表 然后向其中插入一些数据 如果我在存储过程之外手动创建一个普通表 那么存储过程不会创建临时表 存储过程将被创建并完全按照我的要求执行 如果我随后将创建临时表添加到存储过程中
  • Java 和 C# 正则表达式兼容吗?

    这两种语言都声称使用 Perl 风格的正则表达式 如果我用一种语言测试正则表达式的有效性 它在另一种语言中是否有效 正则表达式语法有何不同 这里的用例是一个 C NET UI 与最终的 Java 后端实现对话 该实现将使用正则表达式来匹配数
  • symfony2 按属性对对象集合进行排序

    我有这个实体 class Categoria ORM Id ORM Column type integer ORM GeneratedValue protected id ORM Column type string length 100
  • 尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”

    问题如下 我有一个登录活动 在 Android Studio 中 几天前运行良好 我不记得更改过任何内容 但是当我上次运行此应用程序时 应用程序在我单击登录按钮后立即关闭 最后一件事是关于 AsyncTask 预执行的祝酒 我不明白为什么会
  • 使用 google Oauth 2.0 进行身份验证与注册的重定向 URI 不匹配

    我正在尝试使用进行身份验证谷歌 OAuth 2 0 我在中注册了我的网址谷歌云控制台 在谷歌的规格中OAuth 2 0它说 redirect uri 确定响应发送到的位置 该参数的值必须与注册的值之一完全匹配谷歌云控制台 包括 http 或
  • 使用与主用户名不同的 RDS 代理用户连接被拒绝

    我正在尝试设置 RDS 代理来为我的应用程序提供 mysql 数据库服务 大多数应用程序运行时使用的用户名和密码等于 RDS 数据库上设置的主用户名 该用户名和密码具有所有权限 我尝试使用与主用户名不同的用户名和密码访问我的 RDS 实例
  • 如何消除这个错误? java.lang.reflect.InitationTargetException [重复]

    这个问题在这里已经有答案了 我添加了所有需要的权限 PackageManager packageManager context getPackageManager Class
  • @事务和继承

    我曾经添加过 Transactional所有 Spring 服务 类 的注释 然后我想 如果事务行为应该相同 我真的必须这样做吗 当然 如果不应该的话 我们会添加 Transational以及方法的其他参数 我试图找到一些关于继承的有用信息
  • 如何使用 Xunit 2.0 动态跳过测试?

    Xunit 1 9 x 为用户提供了DynamicSkipExample cs https github com xunit xunit blob v1 samples AssertExamples DynamicSkipExample c