Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run NUnit test fixture programmatically

Tags:

c#

nunit

I often want to perform a quick test, and code it up in LINQPad.

So I have a Main() entry point. Can I make NUnit "run" a fixture programmatically from there?

using NUnit.Framework;

public class Runner
{

  public static void Main()
  {
    //what do I do here?
  }

  [TestFixture]
  public class Foo
  {

    [Test]
    public void TestSomething()
    {
      // test something
    }

  }

}
like image 923
grokky Avatar asked Mar 09 '23 12:03

grokky


2 Answers

You can use the NUnitLite Runner:

using NUnit.Framework;
using NUnitLite;

public class Runner {


    public static int Main(string[] args) {
        return new AutoRun(Assembly.GetExecutingAssembly())
                       .Execute(new String[] {"/test:Runner.Foo.TestSomething"});
    }

    [TestFixture]
    public class Foo {

        [Test]
        public void TestSomething() {
            // test something
        }
    }

}

Here "/run:Runner.Foo" specifies the text fixture.

Mind that you have to reference the nunitlite.dll package as well.

like image 144
Willem Van Onsem Avatar answered Mar 11 '23 00:03

Willem Van Onsem


With 3.8, the problem that was introduced in 3.7 will be fixed. Whether that works explicitly with LINQPad, I'm not sure. You could try it out using the latest build from our MyGet feed.

like image 30
Charlie Avatar answered Mar 11 '23 01:03

Charlie