在 MSBuild 下模拟 Devenv/Runexit

Emulate Devenv/Runexit under MSBuild

我是 MSBuild 的新手,正忙于 Visual Studio 解决方案的自动化测试。

我以前使用Devenv的命令行,它提供了一种方便的/Runexit操作方式。来自手册:

/Runexit (devenv.exe)  
Compiles and runs the specified solution, minimizes the IDE when the solution is run,
and closes the IDE after the solution has finished running. 

这正是我需要的功能。我现在正在迁移到 MSBuild。我发现Solution中的项目文件可以直接用于构建,因为默认的target正是Build。

如何处理与 /Runexit 具有相同效果的不同目标?你能帮我穿过迷宫吗?

这是最基本的目标,运行是项目的输出文件:

<Target Name="RunTarget">
  <Exec Command="$(TargetPath)" />
</Target>

对于 C++ 单元测试,我使用类似这样的东西;它是 属性 sheet,因此无需手动修改即可轻松添加到任何项目。它会在构建后自动 运行s 输出,因此无需指定额外的目标,它在 VS 和命令行中的工作方式相同。此外,在 VS 中,您会立即在错误列表中显示来自 Unittest++ 或 Catch 等框架的单元测试错误,因此您可以双击它们。此外,UnitTestExtraPath 属性 可以设置在其他地方以防万一(例如,在构建服务器上,我们总是希望保持 PATH 干净,但有时我们确实需要将其修改为 运行 构建的 exes)。

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup />
  <ItemDefinitionGroup />
  <ItemGroup />
  <!--Used to be AfterTargets="AfterBuild", but that is unusable since a failing test marks the build as unsuccessful,
      but in a way that VS will always try to build again. As a consequence debugging in VS is impossible since
      VS will build the project before starting the debugger but building fails time and time again.-->
  <Target Name="RunUnitTests" AfterTargets="FinalizeBuildStatus">
    <Exec Condition="$(UnitTestExtraPath)!=''" Command="(set PATH=&quot;%PATH%&quot;;$(UnitTestExtraPath)) &amp; $(TargetPath)" />
    <Exec Condition="$(UnitTestExtraPath)==''" Command="$(TargetPath)" />
  </Target>
</Project>