【问题标题】:Disable selected automated tests at runtime在运行时禁用选定的自动化测试
【发布时间】:2010-04-30 04:12:28
【问题描述】:

是否可以在运行时禁用选定的自动化测试?

我正在使用 VSTS 和 rhino 模拟,并进行了一些需要安装外部依赖项 (MQ) 的集成测试。并非我团队中的所有开发人员都安装了这个。

目前所有需要 MQ 的测试都继承自一个基类,该基类检查 MQ 是否已安装,如果未安装,则将测试结果设置为不确定。这是因为它会阻止测试运行,但会将测试运行标记为不成功并且可以隐藏其他失败。

有什么想法吗?

【问题讨论】:

    标签: c# automated-tests rhino-mocks


    【解决方案1】:

    终于搞清楚了,这就是我所做的。

    在我的每个具有 MQ 依赖项的测试类(或方法,如果该类中只有少数测试需要 MQ)中,我将以下内容添加到类(或方法)声明中

    #if !RunMQTests
        [Ignore]
    #endif
    

    这将禁用测试,除非您取消了条件补偿符号 RunMQTests,此符号未在项目文件中定义,因此默认情况下禁用测试。

    为了让开发人员不必记住他们是否安装了 MQ 以及添加或删除条件比较符号来启用这些测试,我创建了一个自定义构建任务,它将告诉我们是否安装了 MQ。

    /// <summary>
    /// An MSBuild task that checks to see if MQ is installed on the current machine.
    /// </summary>
    public class IsMQInstalled : Task
    {
        /* Constructors removed for brevity */
    
        /// <summary>Is MQ installed?</summary>
        [Output]
        public bool Installed { get; set; }
    
        /// <summary>The method called by MSBuild to run this task.</summary>
        /// <returns>true, task will never report failure</returns>
        public override bool Execute()
        {
            try
            {
                // this will fail with an exception if MQ isn't installed
                new MQQueueManager();
                Installed = true;
            }
            catch { /* MQ is not installed */ }
    
            return true;
        }
    }
    

    然后我们只需要通过将任务添加到测试项目文件的顶部来将其连接到构建过程中。

    <UsingTask TaskName="IsMQInstalled" AssemblyFile="..\..\References\CustomBuildTasks.dll" />
    

    如果这台机器安装了MQ,则在BeforeBuild目标中调用新的自定义任务并设置条件补偿符号。

    <Target Name="BeforeBuild">
      <IsMQInstalled>
        <Output TaskParameter="Installed" PropertyName="MQInstalled" />
      </IsMQInstalled>
      <Message Text="Is MQ installed: $(MQInstalled)" Importance="High" />
      <PropertyGroup Condition="$(MQInstalled)">
        <DefineConstants>$(DefineConstants);RunMQTests</DefineConstants>
      </PropertyGroup>
    </Target>
    

    这让安装了 MQ 的用户可以运行我们的 MQ 集成测试,而没有为未安装 MQ 的用户运行测试不会失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      相关资源
      最近更新 更多