如何使用单声道从 Mac 上的命令行编译和 运行 C# 文件并进行 NUnit 测试

How to compile and run C# files with NUnit tests from the command line on a Mac, using mono

我有两个文件。一个是我的测试文件,Tests.cs:

using NUnit.Framework;

[TestFixture]
public class HelloTest
{
    [Test]
    public void Stating_something ()
    {
        Assert.That(Greeter.Hello(), Is.EqualTo("Hello world."));
    }
}

另一个是Greeter.cs:

public class Greeter
{
    public static string Hello(string statement)
    {
        return "Hello world.";
    }
}

如何从 Mac 上的命令行 运行 这些?

对于简单的脚本,我可以运行:

mcs -out:Script.exe Script.cs
mono Script.exe

但是当我尝试 运行 mcs -out:Tests.exe Tests.cs 时,出现错误:错误 CS0246:找不到类型或名称空间名称“NUnit” .您是否缺少程序集参考?

如果问题得到解决,那么测试文件不会引用 Greeter 文件,那么如何让它找到它呢?

将您的测试编译为库,包括对 GAC 中存在的 NUnit 框架的引用,并添加所有 .cs:

mcs -target:library -r:nunit.framework -out:Tests.dll Greeter.cs Tests.cs

运行 使用 Nunit 控制台运行器的测试:

nunit-console Tests.dll

删除字符串参数以匹配测试:

public class Greeter
{
    public static string Hello()
    {
        return "Hello world.";
    }
}

修复断言:

using NUnit.Framework;

[TestFixture]
public class HelloTest
{
    [Test]
    public void Stating_something ()
    {
        Assert.AreEqual("Hello world.", Greeter.Hello());
    }
}

注意:字符串测试确实应该具有文化意识,或者使用 StringComparison.InvariantCulture