Run NUnit test on Ubuntu from command line
How do I run a NUnit test on Ubuntu from the command line?
I created OnlyTest.cs file
using System;
using System.Text;
using System.Collections.Generic;
using NUnit.Framework;
[TestFixture]
public class OnlyTest
{
[Test]
public void MyTest()
{
int a = 10;
Assert.AreEqual(10, a);
}
}
According to the article Running NuGet Command Line on Linux I downloaded the NuGet.exe and Microsoft.Build.dll files
I ran NuGet to install NUnit
$ mono NuGet.exe install NUnit Installing 'NUnit 2.6.4'. Successfully installed 'NUnit 2.6.4'. $ ls -lR .: razem 1668 -rw-rw-r-- 1 mw mw 28861 cze 24 23:45 Microsoft.Build.zip -rw-rw-r-- 1 mw mw 1664512 cze 24 23:42 NuGet.exe drwxrwxr-x 3 mw mw 4096 cze 25 00:29 NUnit.2.6.4 -rw-rw-r-- 1 mw mw 208 cze 25 00:27 OnlyTest.cs ./NUnit.2.6.4: razem 108 drwxrwxr-x 2 mw mw 4096 cze 25 00:29 lib -rw-rw-r-- 1 mw mw 1131 cze 25 00:29 license.txt -rw-rw-r-- 1 mw mw 99004 cze 25 00:29 NUnit.2.6.4.nupkg ./NUnit.2.6.4/lib: razem 720 -rw-rw-r-- 1 mw mw 151552 cze 25 00:29 nunit.framework.dll -rw-rw-r-- 1 mw mw 584600 cze 25 00:29 nunit.framework.xml
Since I am not sure about compiling and running
Compilation did not result in errors
mcs OnlyTest.cs -target: library -r: NUnit.2.6.4 / lib / nunit.framework.dll -out: OnlyTest.dll
But when I try to run nunit I get errors like
$ nunit-console OnlyTest.dll -noresult NUnit-Console version 2.6.0.0 Copyright (C) 2002-2012 Charlie Poole. Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov. Copyright (C) 2000-2002 Philip Craig. All Rights Reserved. Runtime Environment - OS Version: Unix 3.16.0.30 CLR Version: 4.0.30319.17020 (Mono 4.0 (3.2.8 (Debian 3.2.8 + dfsg-4ubuntu1.1))) ProcessModel: Default DomainUsage: Single Execution Runtime: mono-4.0 Missing method .ctor in assembly / [MY_PATH ]/OnlyTest.dll, type NUnit.Framework.TestFixtureAttribute Can't find custom attr constructor image: / [MY_PATH ]/OnlyTest.dll mtoken: 0x0a000001 Could not load file or assembly 'nunit.framework, Version = 2.6.4.14350, Culture = neutral, PublicKeyToken = 96d09a1eb7f44a77' or one of its dependencies.
The same applies to nunit-gui
source to share
Could not load file or assembly 'nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77'
Check which builds nunit-console / nunit is installed ... I am assuming it is building old builds in the GAC.
Let's start at the beginning:
Get the Latest and Best NUnit & Runner Tools
curl https://api.nuget.org/downloads/nuget.exe -o nuget.exe
mono nuget.exe install NUnit
mono nuget.exe install NUnit.Runners
Make sure mono finds these assemblies first (against GAC)
export MONO_PATH=$(PWD)/NUnit.Runners.2.6.4/tools:$(PWD)/NUnit.2.6.4/lib
Create your test case and compile it:
vi OnlyTest.cs #Using the example in your question
mcs OnlyTest.cs -target:library -r:NUnit.2.6.4/lib/nunit.framework.dll -out:OnlyTest.dll
Run it:
mono ./NUnit.Runners.2.6.4/tools/nunit-console.exe OnlyTest.dll -noresult
Output:
Using default runtime: v4.0.30319
NUnit-Console version 2.6.4.14350
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
OS Version: Unix 14.3.0.0
CLR Version: 4.0.30319.17020 ( Mono 4.0 ( 4.3.0 (master/b044a27 Thu Jun 18 15:17:08 PDT 2015) ) )
ProcessModel: Default DomainUsage: Single
Execution Runtime: mono-4.0
.
Tests run: 1, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.0280499 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
source to share
FYI: I'm building mono from source as I'm on OS-X and the official disto is still 32-bit.
Getting the latest and greatest on Ubuntu:
Debian, Ubuntu and Derivatives Add the Mono Project GPG signing key and package repository to your system (if you are not using sudo, be sure to switch to root):
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
Run package update to update existing packages to the latest version. Then install Mono as described in the Usage section.
Note. Although the APT package is built against Debian Wheezy, it is compatible with several Debian derivatives (including Ubuntu), which means that you can use the same repository across all of these distributions.
http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives
source to share