Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error NUnit1029: The TestCaseSource provides '1' parameter(s), but the Test method expects '1' parameter(s) #685

Closed
richardissimo opened this issue Jan 30, 2024 · 8 comments · Fixed by #686
Assignees
Milestone

Comments

@richardissimo
Copy link

richardissimo commented Jan 30, 2024

Just updated NUnit.Analyzers from 3.10.0 to 4.0.0, and now getting

error NUnit1029: The TestCaseSource provides '1' parameter(s), but the Test method expects '1' parameter(s) 

Example code:

namespace Irrelevant
{
    using NUnit.Framework;

    [TestFixture]
    public class NamespaceTests
    {
        [TestCaseSource(nameof(GetAllProjects))]
        public void Class_namespaces_are_correct(string projectFileName)
        {
            // implementation irrelevant
        }

        private static IEnumerable<string> GetAllProjects()
        {
            // implementation irrelevant
        }
    }
}

EDIT: apologies, it has become apparent that the following line was relevant to the problem, insert the following before the TestCaseSource line: [CancelAfter(30000)]

@mikkelbu
Copy link
Member

Thanks for the report @richardissimo. I cannot reproduce the problem. Do the problem still appear if you clear VS cache and the bin/obj folders ?
image

@katzenbier
Copy link

I have the same problem. Even after running git clean -Xdf and restarting Visual Studio, the error persists.
It also happens on dotnet build, so it's not a Visual Studio problem.

@katzenbier
Copy link

I wasn't able to reproduce it with @richardissimo's code.
It seems to only work if you add a CancelAfter to the test.

using NUnit.Framework;
namespace None;
public class Class1
{
    private static IEnumerable<string> TestSettings => ["1"];
    [CancelAfter(20_000),TestCaseSource(nameof(TestSettings))]
    public static void Test1(string name) { }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>12.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="NUnit" Version="4.0.1" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
    <PackageReference Include="NUnit.Analyzers" Version="4.0.0" />
  </ItemGroup>

</Project>

Triggers:
Class1.cs(6,41): error NUnit1029: The TestCaseSource provides '1' parameter(s), but the Test method expects '1' parameter(s)

@richardissimo
Copy link
Author

I apologise, I can confirm my test has a CancelAfterAttribute - I hadn't spotted that was related; but I can confirm that by removing the CancelAfter attribute, the complaint about TestCaseSource attribute goes away, so it is the combination of CancelAfter with TestCaseSource.

@katzenbier
Copy link

katzenbier commented Jan 30, 2024

I think it was this one: #678.
src/nunit.analyzers/TestCaseSourceUsage/TestCaseSourceUsesStringAnalyzer.cs
The analyzer seems to assume that any method with a TestCaseSource that has string values and uses the CancelAfterAttribute must have a CancellationToken parameter.
But the diagnostic still only reports 1 parameter (The 1 is hard-coded).

@manfred-brands
Copy link
Member

It is true that CancelAfter only works if the test actually uses and obeys the NUnit CancellationToken, either passed in as a parameter or the member in TestExecutionContext.
Although my preferred way is the extra parameter, the analyzer should see it as optional.
I'll see what I missed.

@manfred-brands
Copy link
Member

So I can confirm the error:

image

Also that it goes away if you add the CancellationToken parameter:

image

@mikkelbu
Copy link
Member

I'll get a release out as quickly as possible, but I'm busy all day tomorrow, so I guess it will be Thursday.

@mikkelbu mikkelbu added this to the Release 4.0.1 milestone Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants