简体   繁体   中英

How do I code my Nunit tests to run from command line?

I am writing some C# tests with Nunit and would like to write a Main method to be able to run them from a command line. After googling a lot I saw a few old posts with the following code, however it seems that Nunit.core was deprecated, therefore I cannot use it anymore.

I created a separate Console project to run the tests, called TestRunner and put the mentioned code that does not work. The framework project and the tests projects are both .Net framework class library:

public static void Main(String[] args)
    {
        String pathToTestLibrary = "C:\\dev\\oneshore.Tests.DLL"; //get from command line args
        TestRunner runner = new TestRunner();
        runner.run(pathToTestLibrary);
    }

    public void run(String pathToTestLibrary)
    {
        CoreExtensions.Host.InitializeService();
        TestPackage testPackage = new TestPackage(@pathToTestLibrary);
        testPackage.BasePath = Path.GetDirectoryName(pathToTestLibrary);
        TestSuiteBuilder builder = new TestSuiteBuilder();
        TestSuite suite = builder.Build(testPackage);
        TestResult result = suite.Run(new NullListener(), TestFilter.Empty);

        Console.WriteLine("has results? " + result.HasResults);
        Console.WriteLine("results count: " + result.Results.Count);
        Console.WriteLine("success? " + result.IsSuccess);
    }

You can use NUnit Console to run the tests from command line. So first download the NUnit Console ZIP package from here and unzip binaries. Create a new .net Console project as you have done it and call nunit3-console.exe to run tests through process.start method, as below:

public static void Main(String[] args)
{
    RunTests();
}
private static void RunTests()
{
    string outputFolder = @"E:\OutputFolder";
    string testDllPath = @"E:\Projects\TestProj\bin\Debug\netcoreapp3.1\TestProjTests.dll";
    //exeFilePath is where you unzipped NUnit console binaries
    string exeFilePath = @"E:\Tools\NUnit.Console-3.12.0\bin\netcoreapp3.1\nunit3-console.exe";
    //or E:\Tools\NUnit.Console-3.12.0\bin\net35 for .net framework based tests proj

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = exeFilePath;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "\"" + testDllPath + "\" --work=\"" + outputFolder + "\"";

    try
    {
        // Start the process with the info we specified.
        using (Process exeProcess = Process.Start(startInfo))
        {
            exeProcess.WaitForExit();
        }
    }
    catch (Exception ex)
    {
        // Log error.
        string msg = ex.Message;
    }
}

Once completed you will get the TestResult.xml file generated in outputFolder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM