C# 在运行时禁用选定的自动测试

C# 在运行时禁用选定的自动测试,c#,automated-tests,rhino-mocks,C#,Automated Tests,Rhino Mocks,是否可以在运行时禁用选定的自动测试 我正在使用VST和rhino Mock,并且有一些需要安装外部依赖性的集成测试。不是我团队中的所有开发人员都安装了这个 /// <summary> /// An MSBuild task that checks to see if MQ is installed on the current machine. /// </summary> public class IsMQInstalled : Task { /* Constr

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

我正在使用VST和rhino Mock,并且有一些需要安装外部依赖性的集成测试。不是我团队中的所有开发人员都安装了这个

/// <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;
    }
}
<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依赖性的MQ,我将以下内容添加到类或方法Decellation中

#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;
    }
}
<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的用户的测试运行失败