Emulate Devenv/Runexit under MSBuild

213 views Asked by At

I am new to MSBuild and busy automating tests of Visual Studio solutions.

I previously worked with the command line of Devenv, which provides a convenient /Runexit mode of operation. From the manual:

/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. 

This is exactly the functionality that I need. I am now migrating to MSBuild. I have discovered that the project files in the Solution can be directly used for building, as the default target is precisely Build.

What can I do to handle a different target, that will have the same effect as /Runexit ? Can you help me through the maze ?

1

There are 1 answers

1
stijn On BEST ANSWER

This is the most basic Target which runs a projects' output file:

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

For c++ unittests I use something like this; it's a property sheet so it's easy to add to any project without needing to manually modify it. It automatcially runs the output after the build so there is no need to specify an extra target and it works the same for VS and from the command line. Moreover in VS you'll get unittest errors from frameworks like Unittest++ or Catch displayed right away in the error list, so you can doubleclick them. Also the UnitTestExtraPath property can be set elsewhere just in case (e.g. on a buildserver we always want to keep the PATH clean but sometimes we do need to modify it to run built 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>