TiggerToo
TiggerToo

Reputation: 669

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

I have two files. One is my test file, Tests.cs:

using NUnit.Framework;

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

The other is Greeter.cs:

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

How do I run these from the command line on a Mac?

For a simple script, I can run:

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

But when I try to run mcs -out:Tests.exe Tests.cs, I get an error: error CS0246: The type or namespace name `NUnit' could not be found. Are you missing an assembly reference?

And if that were fixed, the Tests file isn't referencing the Greeter file anyway, so how do I make it find it?

Upvotes: 2

Views: 1738

Answers (1)

SushiHangover
SushiHangover

Reputation: 74144

Compile your Tests as a library, include a reference to the NUnit framework that exists in the GAC, and add all the .cs:

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

Run the tests with Nunit console runner:

nunit-console Tests.dll

Remove the string parameter to match the test:

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

Fixed the Assert:

using NUnit.Framework;

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

Note: String testing should really be cultural aware, or use StringComparison.InvariantCulture

Upvotes: 2

Related Questions