简体   繁体   English

在NUnit Parameterised测试中找不到合适的构造函数

[英]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. 带有字符串的构造函数,并在实际测试fixture属性中传入一个字符串。 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. 我遇到了这个问题 - 在NUnit下运行Test类通过Resharper 8运行。

However, if I changed the TestFixture declaration from this form 但是,如果我这个表单中更改了TestFixture声明

[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. 然后他们工作......这也通过NUnit改进了一些东西 - 但是当我的特定测试发生时,我有Resharper帮助的connectionString和Entity Framework问题而NUnit没有 - 但实际上我相信NUnit对后一种语法更满意。

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. 您的测试类完全有效,并在运行NUnit 2.6和.NET 4时返回Passed ,它们都使用NUnit GUI和Resharper 7测试运行器。

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. TestFixture构造函数中的参数类型与测试类构造函数的类型不匹配时,会出现错误。 For example, if I add the line: 例如,如果我添加该行:

[TestFixture(10)]

I will get the following error in the NUnit GUI: 我将在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. 这个特殊问题是JustCode的NUnit Test Runner中的一个错误。 Re-running this with Resharper 7's NUnit Runner and the NUnit GUI, both pass. 使用Resharper 7的NUnit Runner和NUnit GUI重新运行它,两者都通过了。

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 . 事实证明我在构造函数中有一个调用(坏!)应该在TestFixtureSetUp In Resharper this is the the default error message with parameterized test fixtures if anything throws an exception in the constructor. 在Resharper中,如果在构造函数中抛出异常,则这是带参数化测试装置的默认错误消息。

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

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. 就我而言,我使用的是TestFixtureSource,以及一个为不同的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) (我忘了-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NUnit-未找到合适的构造函数 - NUnit- No Suitable Constructor was Found 解决NUnit“找不到合适的构造函数”消息 - Resolving NUnit “No suitable constructor was found” message C# NUnit 即使使用 TestFixture 也找不到合适的构造函数 - C# NUnit No suitable constructor found even when using TestFixture N 层架构 WebApi:NUnit - 未找到合适的构造函数 - N-Tier Architecture WebApi: NUnit - No suitable constructor was found OneTimeSetUp:使用 nunit 和 autofac 时找不到合适的构造函数 - OneTimeSetUp: No suitable constructor was found when using nunit and autofac C# NUnit 报告奇怪的错误:“找不到合适的构造函数” - C# NUnit reports weird error: "No suitable constructor was found" Nunit 测试给出结果 OneTimeSetUp:找不到合适的构造函数 - Nunit test gives result OneTimeSetUp: No suitable constructor was found 并非所有测试都在包含 arrays 的参数化 NUnit TestFixture 中运行 - Not all tests are run in parameterised NUnit TestFixture containing arrays 如何修复找不到合适的构造函数 - How to fix No suitable constructor found 如何使用参数化构造函数对从基类继承的类执行Nunit测试 - How to perform Nunit testing for a class inherting from a base class with parameterised constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM