从命令行在Ubuntu上运行NUnit测试

时间:2015-06-24 23:06:23

标签: c# ubuntu mono nunit

如何从命令行在Ubuntu上运行NUnit测试?

我创建了文件OnlyTest.cs

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);
    }
}

根据文章Running NuGet command-line on Linux我已下载文件NuGet.exe和Microsoft.Build.dll

我已经运行NuGet来安装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

因为我不确定编译和运行

编译没有产生任何错误

 mcs OnlyTest.cs -target:library -r:NUnit.2.6.4/lib/nunit.framework.dll -out:OnlyTest.dll

但是当我尝试运行nunit时,我会收到类似

的错误
$ 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.

nunit-gui

也是如此

2 个答案:

答案 0 :(得分:5)

Could not load file or assembly 'nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77'

检查已安装的nunit-console / nunit程序集......我假设它正在接收GAC中的旧程序集。

让我们从头开始:

获取最新最好的NUnit AND 它的Runner工具

curl https://api.nuget.org/downloads/nuget.exe -o nuget.exe
mono nuget.exe install NUnit
mono nuget.exe install NUnit.Runners

确保单声道首先找到这些程序集(与GAC对比)

export MONO_PATH=$(PWD)/NUnit.Runners.2.6.4/tools:$(PWD)/NUnit.2.6.4/lib

创建测试示例并编译它:

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

运行它:

mono ./NUnit.Runners.2.6.4/tools/nunit-console.exe OnlyTest.dll -noresult

输出:

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

答案 1 :(得分:0)

仅供参考:我从源代码构建单声道,因为我在OS-X上,官方的disto仍然是32位。

在Ubuntu上获取最新最好的内容:

Debian,Ubuntu和衍生品 将Mono Project GPG签名密钥和软件包存储库添加到您的系统中(如果您不使用sudo,请务必切换到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

运行程序包升级以将现有程序包升级到最新的程序包。然后按用法部分中的说明安装Mono。

注意:虽然APT包是针对Debian Wheezy构建的,但它与许多Debian衍生产品(包括Ubuntu)兼容,这意味着您可以在所有这些发行版中使用相同的存储库。

http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives