3

I am trying to get TeamCity to run XUnit tests as part of the build process. So I created a separate file - MyProject.msbuild - living in the same folder as the .sln file, which looks like this:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <UsingTask AssemblyFile="$(MSBuildProjectDirectory)\..\bin\xunit.net\xunit.runner.msbuild.dll" TaskName="Xunit.Runner.MSBuild.xunit"
             />

    <Target Name="Build">
      <MSBuild Projects="MyProject.sln" Targets="Build" Properties="Configuration=Release">
        <xunit Assembly="MyProject.Utility.Tests\bin\Release\MyProject.Utility.Tests.dll" />
      </MSBuild>
    </Target>

</Project>

However, no matter what I do, VS2010 hates me having the element inside the element. If I run MSBuild on the file, it tells me a little bit more:

P:\MyProject\src>c:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe MyProject.msbuild /tv:4.0 /v:d
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.225]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 08.11.2011 21:08:46.
Project "P:\MyProject\src\MyProject.msbuild" on node 1 (default targets).
Building with tools version "4.0".
P:\MyProject\src\MyProject.msbuild(8,9): error MSB4067: The element <xunit> beneath element <MSBuild> is unrecognized.
Done Building Project "P:\MyProject\src\MyProject.msbuild" (default targets) -- FAILED.


Build FAILED.

"P:\MyProject\src\MyProject.msbuild" (default target) (1) ->
  P:\MyProject\src\MyProject.msbuild(8,9): error MSB4067: The element <xunit> beneath element <MSBuild> is unrecognized.

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.01

So my current guess is that it doesn't successfully load the xunit.runner.msbuild.dll somehow - or I have done something else strange.

However, I would think that if it couldn't load xunit.runner.msbuild.dll, it would tell me about it. I made sure the file is not blocked (by unpacking the xunit distribution with 7zip).

Any ideas what I can do to get MSBuild to swallow my build file and run the tests?

Rune Jacobsen
  • 9,907
  • 11
  • 58
  • 75

1 Answers1

8

You don't want to nest the calls, try this:

<Target Name="Build"> 
    <MSBuild Projects="MyProject.sln" Targets="Build" Properties="Configuration=Release"> 
    </MSBuild> 
    <xunit Assembly="MyProject.Utility.Tests\bin\Release\MyProject.Utility.Tests.dll" /> 
</Target> 

The items within a target are executed in sequence.

Murph
  • 9,985
  • 2
  • 26
  • 41
  • Well, that was embarrasingly simple. Thank you! :) – Rune Jacobsen Nov 09 '11 at 05:48
  • No worries, as an aside, I'd probably split test from build and give yourself a test target and a build target (although the example bit of msbuild I checked doesn't!), then you have options to run whichever targets in whichever context you want. – Murph Nov 09 '11 at 09:01
  • Thanks, yeah, I'll probably go in that direction. Perhaps it will make it easier to get code coverage going in TeamCity as well. :) – Rune Jacobsen Nov 09 '11 at 19:14
  • I intend to end up with targets for (something like) build, unit test, package, deploy, smoke test - currently I have the last 3 wrapped into a single target (it works...). I also need an integration test target. Challenge is I have a lot of projects to wrap all of this stuff into – Murph Nov 10 '11 at 09:37