Arr*_*ran 11 c# nunit unit-testing
见下面的测试夹具:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是查看参数化测试装置的工作原理.我之前没有使用它们,所以这是我第一次尝试它.
它对我来说很好看.带有字符串的构造函数,并在实际测试fixture属性中传入一个字符串.它汇编.测试只是将其写入控制台窗口.
然而,测试失败并显示以下消息:
No suitable constructor was found
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗?
无论我在哪里设置断点,都没有任何打击,所以它很早就失败了.
Nij*_*Nij 14
我遇到了这个问题 - 在NUnit下运行Test类并通过Resharper 8运行.
但是,如果我从这个表单中更改了TestFixture声明
[TestFixture("CategoryName")]
Run Code Online (Sandbox Code Playgroud)
以这种形式:
[TestFixture(Category="CategoryName")]
Run Code Online (Sandbox Code Playgroud)
然后他们工作......这也通过NUnit改进了一些东西 - 但是当我的特定测试发生时,我有Resharper帮助的connectionString和Entity Framework问题而NUnit没有 - 但实际上我相信NUnit对后一种语法更满意.
您的测试类完全有效,并在运行NUnit 2.6和.NET 4时返回Passed,它们都使用NUnit GUI和Resharper 7测试运行器.
当TestFixture
构造函数中的参数类型与测试类构造函数的类型不匹配时,会出现错误.例如,如果我添加该行:
[TestFixture(10)]
Run Code Online (Sandbox Code Playgroud)
我将在NUnit GUI中收到以下错误:
ParameterizedNunit.HarryPotterTests(10).Test:
ParameterizedNunit.HarryPotterTests does not have a suitable constructor
Run Code Online (Sandbox Code Playgroud)
这个特殊问题是 JustCode 的 NUnit Test Runner 中的一个错误。使用 Resharper 7 的 NUnit Runner 和 NUnit GUI 重新运行此程序,均通过。