Search code examples
nunit.net-6.0visual-studio-2022

Nunit won't discover working non explicit tests Visual studio 2022


I have 3 normal and 1 explicit test but when I run my test using the Test Explorer window I get this output under "Tests" in the Output window

========== Starting test run ==========
NUnit Adapter 4.2.0.0: Test execution started
Running all tests in xyz.dll
   NUnit3TestExecutor discovered 1 of 1 NUnit test cases using Current Discovery mode, Explicit run
ExplicitMethod(03/10/2022 08:00:00,03/10/2022 16:00:00): OneTimeSetUp: 
NUnit Adapter 4.2.0.0: Test execution complete
========== Test run finished: 1 Tests (0 Passed, 0 Failed, 0 Skipped) run in 462 ms ==========

*Note I'm using NUnit Adapter 4.2.1 instead of 4.2.0 so that's already weird

And this is how the Test Explorer window looks Test Explorer window

*Note the full blue test is the Explicit test that get's skipped like it should

This is a problem because it does seem to only discover tests which are explicit.

The tests I want to run are of course not explicit, here is an example

[Test]
[TestCaseSource(nameof(TestNameData))]
public async Task<float> TestName(DateTime start, DateTime end, List<CalculateHoursObj>? list = default)
{
    if (list == null) list = new List<CalculateHoursObj>();
    return await EmployeeService.CalculateOverTimeHours(start, end, list);
}
public static IEnumerable TestNameData
{
    get
    {
        yield return new TestCaseData(TenthMarch8_2022, TenthMarch16_2022).Returns(8.0f);
    }
}

It fails to discover and/or run this test.

But curiously if I break all my non explicit tests by making the data non-static like this

public IEnumerable TestNameData
{
    get
    {
        yield return new TestCaseData(TenthMarch8_2022, TenthMarch16_2022).Returns(8.0f);
    }
}

It of course breaks the test and when I run all tests it does actually discover all tests

========== Starting test run ==========
NUnit Adapter 4.2.0.0: Test execution started
Running selected tests in xyz.dll
   NUnit3TestExecutor discovered 2 of 4 NUnit test cases using Current Discovery mode, Non-Explicit run
NUnit Adapter 4.2.0.0: Test execution complete
========== Test run finished: 2 Tests (0 Passed, 2 Failed, 0 Skipped) run in 394 ms ==========

But even now it only runs 2 of the 3 broken non explicit tests and of course they all fail

I have looked up everything online for 1.5 hours and really can't find a solution.

Don't bother responding with "have you updated visual studio or the nugget packages"


Solution

  • This question was part of a bug the solution is to not use new DateTime() withing nunit tests or to have nunit test be discover with id's using the <UseParentFQNForParametrizedTests>true</UseParentFQNForParametrizedTests> <UseNUnitIdforTestCaseId>true</UseNUnitIdforTestCaseId> flags

    This was a bug because it was not reported correctly in the output window and/or Error List window.

    I've created an Issue on GitHub about this if anyone wants to read.

    Now stop editing this post because it truly is answered and the Issue link which I included shows the valid, correct and readable answer by "OsirisTerje" whom explains it way better than I could.