nunit.analyzers icon indicating copy to clipboard operation
nunit.analyzers copied to clipboard

Analyzer for TestCaseAttribute should also work for generic TestCases with deduced types

Open mikkelbu opened this issue 7 years ago • 1 comments

The analyzer should also handle the following (from nunit\src\NUnitFramework\tests\Internal\DeduceTypeArgsFromArgs.cs). Currently, it gives a warning for 5, The value of the argument at position 0 cannot be assigned to the argument t1 and similar for the second argument.

    [Category("Generics")]
    [TestFixture(100.0, 42)]
    [TestFixture(42, 100.0)]
    public class DeduceTypeArgsFromArgs<T1, T2>
    {
        T1 t1;
        T2 t2;

        public DeduceTypeArgsFromArgs(T1 t1, T2 t2)
        {
            this.t1 = t1;
            this.t2 = t2;
        }

        [TestCase(5, 7)]
        public void TestMyArgTypes(T1 t1, T2 t2)
        {
            Assert.That(t1, Is.TypeOf<T1>());
            Assert.That(t2, Is.TypeOf<T2>());
        }
    }

mikkelbu avatar Jun 13 '18 19:06 mikkelbu

As T1 can, in this case, be either a doubleor an int. That would mean that if the TestCase(5, 7.0) then in on case it is wrong, in the other it isn't.

manfred-brands avatar Jul 26 '24 10:07 manfred-brands

@mikkelbu This also no longer raises an error. Type parameter checking was added in #638 for #632.

Like with #53 it does not check the type of the TestFixture attribute.

Should the analyzer check type TestFixture attributes and where should the diagnostics be raised? On the [TestFixture("Hello", "World"] or the [TestCase(5, 7)]?

Before doing that, what is the practical use-case where you allow a TestFixture type parameter, but have constants for TestCase?

manfred-brands avatar Jan 27 '25 01:01 manfred-brands

@manfred-brands As far as I can remember (this is more a guess than actual recollection) when I overtook the Analyzers project, then I ran the existing analyzers against all the NUnit Framework tests and created a lot of issues - like this and #53 - when the analyzers failed on valid tests. I didn't think much about whether the tests made practical sense or not (most of them did).

I'll close this issue and #53 as I don't think we should spend time on this. If somebody has some real use-cases, then we can reconsider.

mikkelbu avatar Feb 13 '25 09:02 mikkelbu