2

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for not only answering my question but also answering the next questions I was about to ask with my crappy sample code. ;)
@TiggerToo no problem, compiling directly with csc/mcs will get old real first, but is a great skill to understand how .Net assemblies are built... You might want to look at an IDE.. Visual Studio For Mac (the new version of Xamarin Studio) or the "basic" MonoDevelop version... They have built-in NUnit Test Runners and allow for full debugging of your Tests... ;-) visualstudio.com/vs/visual-studio-mac or monodevelop.com
I am using this for simple coding puzzles that include one test file and one class file, and I think this is easier for that. For a full fledged project, I would definitely want to use Xamarin.
Out of curiosity, why is it that the Assert.That formatting problematic? That formatting does break the code, but in trying to search for more information about it, I'm just finding comparisons between them without explaining that one might not even work in all environments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.