Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute NUnit test using NUnit.Runners package and psake?

Traditionally, nunit-console.exe has been included in the repository and on the build server (or any other machine) this EXE was called from some build script.

Now that the NUnit.Runners package is available I wonder how this could be used from a psake build script. It is a solution-level package so it doesn't leave any trace in packages.config and cannot be auto-restored as other project-level packages so I guess one would need to call Install-Package from the psake script, wait for the download and then execute the unit tests? Hopefully this download can be run only once and it will not slow down the build every time it runs. Or will it?

like image 852
Borek Bernard Avatar asked Feb 28 '12 16:02

Borek Bernard


People also ask

How do I run NUnit tests from command line in Visual Studio?

Use nunit-console.exe to run tests from the command line. This will run the unit tests and save the results in the results. xml file, which you can work with easily.

Which of the following runners are provided by NUnit?

Nunit provides three different runners, which may be used to load and run your tests. The console runner, nunit-console.exe, is used for batch execution. The gui runner, nunit.exe, provides interactive loading and running of tests.


2 Answers

Just ran into this myself. Quite easy to fix as follows:

  1. Get latest version of the Nuget Package Manager extension. I'm on 1.8 at the moment.
  2. Add the Nunit.Runners package to your solution
  3. Copy the element related to the runner from packages.config under your .nuget folder into the packages.config file of your unit test project

From now on, when you build, Nuget will pull down the Nunit.Runners packages if it is not on the machine. I then reference the Nunit runner from the package in my command line build.

I have this working in a little project I did for a TeamCity build light on Github. Packages.config in the unit test project has been modified as discussed above. You can look at the MSBuild file to see how I run tests from the command line. (build.proj, which references some targets and properties contained in the repository's tools\msbuild folder). You will need the latest Nuget Package Manager installed before you try to build in VS.NET or from the command line (clicktobuild.bat).

You should be able to port the idea of how to run Nunit console from the right location into psake quite easily.

like image 60
Tom Cabanski Avatar answered Oct 07 '22 10:10

Tom Cabanski


I have created an issue with nuget developers, and proposed a fix.

Modify the nuget.targets file with the following changes:

In <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'"> add this element:

<SolutionLevelPackagesConfig>$([System.IO.Path]::Combine($(SolutionDir), ".nuget\packages.config"))</SolutionLevelPackagesConfig>

In <PropertyGroup> add this element:

<RestoreSolutionLevelCommand>$(NuGetCommand) install "$(SolutionLevelPackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch) -solutionDir "$(SolutionDir) "</RestoreSolutionLevelCommand>

In <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites"> add this element before the RestoreCommand for WinNT:

<Exec Command="$(RestoreSolutionLevelCommand)"
              LogStandardErrorAsError="true"
              Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)') And Exists('$(SolutionLevelPackagesConfig)')" />

This made my msbuild to restore the solution level packages.

like image 36
Sunny Milenov Avatar answered Oct 07 '22 11:10

Sunny Milenov