Serhii Kozachenko
Serhii Kozachenko

Reputation: 1401

How to run specific test class via c# code in NUnit

I need to run unit tests from specific class and show results. I am using NUnit.

I can run all unit tests in assembly:

 public IntegrationTestsResults Run(Assembly assembly)
    {
        CoreExtensions.Host.InitializeService();

        TestPackage testPackage = new TestPackage(new System.Uri(assembly.CodeBase).LocalPath);
        SimpleTestRunner testRunner = new SimpleTestRunner();
        testRunner.Load(testPackage);

        TestResult testResult = testRunner.Run(new NullListener(), TestFilter.Empty, false, LoggingThreshold.All);

        var formatted = this.FormatResults(testResult, new List<TestResult> {});
        return new IntegrationTestsResults
            {
                status = (IntegrationTestResultState)testResult.ResultState,
                time = testResult.Time,
                items = formatted,
                passed =
                    formatted.SelectMany(t => t.Results.Cast<TestResult>())
                        .Count(r => r.ResultState == ResultState.Success || r.ResultState == ResultState.Ignored),
                total = formatted.SelectMany(t => t.Results.Cast<TestResult>()).Count(),
                isSuccess = testResult.ResultState == ResultState.Success
            };
    }

But how can I run unit tests only in specific class?

Upvotes: 0

Views: 623

Answers (1)

Charlie
Charlie

Reputation: 13736

Your use of TestFilter.Empty tells NUnit to run all tests. Instead, create and pass a SimpleNameFilter using the FullName of your class.

Upvotes: 1

Related Questions