Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 885
Arran Avatar asked Jul 11 '12 14:07

Arran


2 Answers

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
like image 67
Anders Gustafsson Avatar answered Sep 24 '22 10:09

Anders Gustafsson


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.

like image 26
Arran Avatar answered Sep 21 '22 10:09

Arran