简体   繁体   中英

No suitable constructor was found in NUnit Parameterised tests

See the below test fixture:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

/// <summary>
/// Tests relating to Harry Potter
/// </summary>
[TestFixture("Dumbledore")]
public class HarryPotterTests
{
    public string Name;

    public HarryPotterTests(string personName)
    {
        Name = personName;  
    }

    [Test]
    public void Test()
    {
        Console.WriteLine(Name);
    }
}

What I'm trying to achieve is to see how parameterised test fixtures work. I haven't used them before so this is my first stab at it.

It looks OK to me. Constructor with a string, and passing in a string in the actual test fixture attribute. It compiles. Test simply writes it out to a console window.

The test however fails with this message:

No suitable constructor was found

Am I missing something blindly obvious?

No matter where I put a breakpoint, nothing is hit, so it is failing very early on.

I had this problem. It was caused by the constructor throwing an error, rather than any issue with the constructor parameters. The error message was misleading in my case.

I experienced this problem - running a Test class under NUnit and via the Resharper 8.

However, if I changed the TestFixture declaration from this form

[TestFixture("CategoryName")]

to this form:

[TestFixture(Category="CategoryName")]

then they worked... this also improved things via NUnit - but as it happens for my particular tests I have connectionString and Entity Framework issues which Resharper helps with and NUnit does not - but essentially I believe NUnit is happier with the latter syntax.

Your test class is perfectly valid and returns Passed when running NUnit 2.6 and .NET 4, both with the NUnit GUI and the Resharper 7 test runner.

The error you are seeing occurs when the types of the arguments in the TestFixture constructor does not match the types of the test class constructor. For example, if I add the line:

[TestFixture(10)]

I will get the following error in the NUnit GUI:

ParameterizedNunit.HarryPotterTests(10).Test:
ParameterizedNunit.HarryPotterTests does not have a suitable constructor

This particular problem is a bug in JustCode's NUnit Test Runner. Re-running this with Resharper 7's NUnit Runner and the NUnit GUI, both pass.

Check if your constructor has any logic that might be failing. It turns out I had a call in the Constructor (bad!) that should have been in TestFixtureSetUp . In Resharper this is the the default error message with parameterized test fixtures if anything throws an exception in the constructor.

相当明显,但如果测试的构造函数不公开,也会发生。

Just in case it helps someone else. In my case, I was using TestFixtureSource, and a function to build the combinations for the different TestFixtures. Turns out that the number of element in the array did not match the number of parameters for the constructor. (I forgot the -1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM