终于搞清楚了,这就是我所做的。
在我的每个具有 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 的用户运行测试不会失败。