简体   繁体   中英

Running unit tests from a .proj file with MSBuild

I want to run unit tests using MSBuild. Here is how I invoke msbuild today:

msbuild MySolution.sln

Instead, I want to use an MSBuild project file called "MyBuild.proj" like this:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5" DefaultTargets="Build">
  <Target Name="Build">
    <ItemGroup>
      <SolutionToBuild Include="MySolution.sln" />
      <TestContainer Include="..\Output\bin\Debug\*unittests.dll"/>
    </ItemGroup>
  </Target>
</Project>

And then call this command line:

msbuild MyBuild.proj

For some reason, when I do that the command succeeds immediately and the build doesn't even happen. I fear I must be missing something very obvious as I am new to MSBuild.

I suppose I really have 2 questions:

  1. Why doesn't this even build my solution
  2. Is the "TestContainer" element correct for executing my tests

Thanks!

You havent supplied any task to actually do anything, inside your build target you need a call to an msbuild task, your example becomes:

<Target Name="Build">
   <ItemGroup>
      <SolutionToBuild Include="MySolution.sln" />
      <TestContainer Include="..\Output\bin\Debug\*unittests.dll"/>
    </ItemGroup>
   <MSBuild Projects="@(SolutionToBuild)"/> 
</Target>

this specifies what projects you actually want msbuild to build. See: http://msdn.microsoft.com/en-us/library/vstudio/z7f65y0d.aspx for more details and the parameters it takes.

Thats part one.

As for part 2? what testing framework are you using? If using mstest id try wrapping the commandline mstest.exe in an msbuild exec statement to get it to run and execute the tests. See an example here: http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/cb87a184-6589-454b-bf1c-2e82771f c3aa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM