1

I want to run NUnit tests from my code. To be more specific I have an Web API (.net core) that recieves set of test names that needs to be run and after the run, the endpoint will return set of results.

The tests are in another .dll (another project that's using Microsoft.Playwright and Serilog to log to ElasticSearch - it's .net core also but it has classes marked as [TextFixutre]).

At first, I just wanted to check if the RemoteTestRunner is loading all of my tests here is the demo code:

    [HttpPost]
    [Route("/api/TestSuiteRun/Run")]
    public IActionResult RunTestSuite([FromBody] AutomationAPI.Models.TestSuite testSuite)
    {
        try
        {
            CoreExtensions.Host.InitializeService();
            TestSuiteBuilder builder = new TestSuiteBuilder();
            TestPackage testPackage = new TestPackage(@"C:\Desktop\Automation-v2\Automation-v2\bin\Debug\netcoreapp3.1\Automation-v2.dll");
            RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
            remoteTestRunner.Load(testPackage);
            NUnit.Core.TestSuite suite = builder.Build(testPackage);
            NUnit.Core.TestSuite test = suite.Tests[0] as NUnit.Core.TestSuite;
            var numberOfTests = ((TestFixture)test.Tests[0]).TestCount;

            foreach (TestMethod t in ((TestFixture)test.Tests[0]).Tests)
            {
                Console.WriteLine(t.TestName.Name);
            }
            return StatusCode(200);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            return StatusCode(500);
        }
    }

but I'm ending up with following exception on remoteTestRunner.Load(testPackage) line:

"Could not load type 'System.Runtime.Remoting.Messaging.ILogicalThreadAffinative' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."

and I don't know why is this happening. Any ideas?

As for the second question that I have, is it possible to pass TestCaseSource to the test with RemoteTestRunner or it must be written in the .dll explicitly (before load in the RemoteTestRunner) like this:

    [Test, TestCaseSource(typeof(UserProvider), "ProvideUsers")]
    public async Task ExportPPTTimeTestAsync(User currentUser)
    {...
1
  • Reinstall your (or Nuget) package again. Or check the compatibility version issue. Ensure that it should be built properly. Commented Jul 1, 2021 at 10:10

1 Answer 1

3

I'm guessing you found some instructions for running NUnit programmatically on the web - maybe even some I wrote many years ago. You should always look with suspicion on older articles.

In this case, we know that you are using RemoteTestRunner a class from the core of NUnit V2, which is either legacy or obsolete code depending on how you look at it. The problem with ILogicalThreadAffinitive is just the tip of the iceberg. The nunit.core package will never run modern NUnit3 code, and can't even load a .NET Core assembly.

If you want to run code written against current versions of NUnit under .NET Core on the web, your best bet at this time is to call the NUnit Framework API directly from a driver you create yourself. The driver source code incorporated in the latest version of the NUnit3 Console project on GitHub would give you some idea of how it can be done.

Unfortunately, such a project is kind of a big deal so I can't post sample code for you here. If you decide to try it, start by examining the source code of the NUnit3 framework drivers, which are part of the nunit3 console runner project on GitHub. Post specific questions if you run into problems.

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

Comments

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.