views:

265

answers:

2

I'm trying to get Team City to build my .NET solution and run my nUnit tests.

I know I can modify the individual projects and tell them always run the unit tests. I don't want the unit tests to run when I click "build" in visual studio, but I do want the unit tests to run when Team City kicks off a msbuild task.

I tried "msbuild solutionname.sln" and gave team city the targets of "BUILD" and my custom build tag of "TEST". However, msbuild can't find any specified target when invoked against a sln solution. So, I ran msbuild to convert my solution into a project which has a target like this:

  <Target Name="Build">
    <MSBuild  Projects="@(BuildLevel0)" >
  </Target>

I naively thought I could write a new task like this:

<Target Name="BuildAndTest">
    <CallTarget Targets="Build"/> <!-- This builds everything in solution -->
    <CallTarget Targets="Test"/> <!-- DOES NOT WORK. This target exists in project that gets built by this solution -->
</Target>

The nunit target looks like this:

  <Target Name="Test" DependsOnTargets="Build" Condition=" '$(Configuration)' == 'Release'">
    <NUnit Assemblies="$(OutputPath)\Tsa.BaseTest.dll" ContinueOnError="false" ToolPath="C:\Program Files\NUnit 2.5.2\bin\net-2.0\" DisableShadowCopy="true" OutputXmlFile="$(OutputPath)\nunit-results.xml" />
  </Target>

As you can see, it references OutputPath, which only the project knows--the solution doesn't have reference to $OutputPath, else I'd just put all the test targets into the "solution project".

Any suggestions on how I can get this to work?

+1  A: 

I think you're making this a lot harder than it needs to be. TeamCity has built-in support for running NUnit unit tests after the build - you don't need to modify the MSBuild file at all. Just set up your Build Configuration (I think it's under Runner) to specify the version of NUnit and which assemblies are test assemblies.

NOTE: I checked and we have this under Runner: sln2008 (section NUnit Test Settings) in TeamCity Enterprise Version 4.5.4, but I don't see anything on the JetBrains site that states that it's specific to Enterprise. It may require a version upgrade, though. See TeamCity Testing Frameworks.

TrueWill
I'm not sure that is accurate. I'm using 4.5.2 Professional Edition. The page can detect output of nunit (the XML Report Processing section), but Team City on its own can't/won't cause an invocation of nunit to generate such output. And this version doesn't have a textbox for me to list test assemblies, either.
MatthewMartin
@Matthew - just checked at work. We're running Version 4.5.4 of TeamCity Enterprise. Under Runner: sln2008 I have NUnit Test Settings; under that I can set Run tests from and enter paths to test assemblies. There are several other options as well.
TrueWill
A: 

This is what finally worked. It is ignored by visual studio, msbuild will run this section correctly, and team city will as well, although it replaces the Target with its own an runtime (according to the build log).

TeamCity will "automatically" run nunit tests and display the results, only in the sense that it will do so after manually editing the msbuild file, doing numerous manual teaks and telling TeamCity where each assembly is and where each output file is.

<Project (snip) DefaultTargets="BuildAndTest" (snip)>
<Target Name="BuildAndTest">
    <CallTarget Targets="Build" />
    <CallTarget Targets="TestBase" />
</Target>

  <Target Name="TestBase" DependsOnTargets="Build">
    <NUnit Assemblies="Tsa.BaseTest\bin\RELEASE\Tsa.BaseTest.dll" ContinueOnError="false" ToolPath="C:\Program Files\NUnit 2.5.2\bin\net-2.0\" DisableShadowCopy="true" OutputXmlFile="$(SolutionDir)\Tsa.BaseTest\bin\RELEASE\nunit-results.xml" />
  </Target>
  </Target>
</Project>
MatthewMartin