我有以下的测试夹具类和超越我的原因NUnit的决定运行所有测试类的解决此一个,但不是这一个
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using MyProject;
using NUnit.Framework;
namespace MyProject.Test
{
[TestFixture]
public class MyProjectTests
{
private const string Description = "This is a test Description generated through UNIT Tests";
private ManualWorkflowSchedulerService scheduler;
private WorkflowRuntime workflowRuntime;
[SetUp]
public void Init()
{
// set up workflow scheduler and runtime
this.workflowRuntime = new WorkflowRuntime();
this.scheduler = new ManualWorkflowSchedulerService(true); // run synchronously
this.workflowRuntime.AddService(this.scheduler);
this.workflowRuntime.StartRuntime();
// create Test Case Sources
object[] insertScenarios =
{
new object[] { typeof(RaiseScenario), this.workflowRuntime, Description, true, true, string.Empty },
new object[] { typeof(RaiseScenario), this.workflowRuntime, Description, true, false, "New Reason" }
};
}
/// <summary>
/// The insert tests.
/// </summary>
/// <param name="runtime">
/// The runtime.
/// </param>
/// <param name="description">
/// The description.
/// </param>
/// <param name="outstandingWorkDocUploaded">
/// The Doc One Uploaded.
/// </param>
/// <param name="DocTwoUploaded">
/// The Doc Two Uploaded.
/// </param>
/// <param name="shortageReason">
/// The shortage Reason.
/// </param>
[Test, TestCaseSource("insertScenarios")]
public void TestInsert(
WorkflowRuntime runtime,
string description,
bool DocOneUploaded,
bool DocTwoUploaded,
string Reason)
{
var message = Business.InsertBoatHandoverOutsideCrew(runtime, description, DocOneUploaded, DocTwoUploaded, Reason);
Assert.AreNotEqual(0, message.Id);
}
}
}
测试项目的结构被分成它的组成部分,即解决方案的每个子项目有测试项目中它自己的目录。 这并不适用于所有其他项目的人编码在.net 3.5,但现在这个项目的测试中被忽略了一个问题。
仍然不明白为什么你的测试夹具应NUnit的被忽略(根据发布的代码片段)。 是代码片段失去了一些东西?
正如Wiktor的指出,
所述SOURCENAME参数表示用于提供测试情况下,辐射源的名称。 它具有以下特征:它可以是一个字段,属性或方法。 它可以是一个实例或静态成员。 它必须返回一个IEnumerable或实现IEnumerable类型。 通过枚举返回的个别项目必须与出现的属性的方法的签名一致。
然而,随着代码片段上面列出,你应该标记为无效特定的测试不被忽略(使用NUnit v2.5.10上FWK 4.0)。
namespace AJack
{
[TestFixture]
public class ParameterizedTestsDemo
{
private object[][] _inputs;
public ParameterizedTestsDemo()
{
Console.Out.WriteLine("Instantiating test class instance");
_inputs = new[]{ new object[]{1,2,3},
new object[]{4,5,6} };
}
[TestFixtureSetUp]
public void BeforeAllTests()
{
Console.Out.WriteLine("In TestFixtureSetup");
object[] localVarDoesNotWork = { new object[]{1,2,3},
new object[]{4,5,6} };
/*this will NOT work too
_inputs = new[]{ new object[]{1,2,3},
new object[]{4,5,6} }; */
}
[TestCaseSource("localVarDoesNotWork")]
public void WillNotRun(int x, int y, int z)
{
Console.Out.WriteLine("Inputs {0}, {1}, {2}", x,y,z);
}
[TestCaseSource("PropertiesFieldsAndMethodsWork")]
public void TryThisInstead(int x, int y, int z)
{
Console.Out.WriteLine("Inputs {0}, {1}, {2}", x, y, z);
}
private object[] PropertiesFieldsAndMethodsWork
{
get {
Console.Out.WriteLine("Getting test input params");
return _inputs;
}
}
}
}
如果设置在Console.Out.WriteLines跟踪点,并附加一个调试器,你会看到当组件加载器(测试树构造)的跟踪点是打
Test Class constructor
Retrieve test case inputs from property/field/method
当你运行测试,
Test Class constructor
InTestFixtureSetup
所以,问题的关键是, 你必须分配在测试类构造函数实例字段为此工作。 你不能使用设置方法,因为当参数测试输入都解决了,他们不叫。 此外,当它不能解析的投入,你应该会看到像异常的红色
AJack.ParameterizedTestsDemo.WillNotRun:
System.Exception : Unable to locate AJack.ParameterizedTestsDemo.localVarDoesNotWork
如果你把测试用例出这应该工作SetUp
// create Test Case Sources
public object[] insertScenarios =
{
new object[] { typeof(RaiseScenario), this.workflowRuntime, Description, true, true, string.Empty },
new object[] { typeof(RaiseScenario), this.workflowRuntime, Description, true, false, "New Reason" }
};
/// <summary>
/// The init.
/// </summary>
[SetUp]
public void Init()
{
// set up workflow scheduler and runtime
this.workflowRuntime = new WorkflowRuntime();
this.scheduler = new ManualWorkflowSchedulerService(true); // run synchronously
this.workflowRuntime.AddService(this.scheduler);
this.workflowRuntime.StartRuntime();
}
我从来没有见过一个测试用例其中空隙接受参数,你打算这样做呢? 我想这就是为什么你在这个类的测试运行不到风度。
[Test, TestCaseSource("insertScenarios")]
public void TestInsert()
{
WorkflowRuntime runtime = //some value;
string description = //some value;
bool DocOneUploaded = //some value;
bool DocTwoUploaded = //some value;
string Reason = //some value;
var message = Business.InsertBoatHandoverOutsideCrew(runtime, description, DocOneUploaded, DocTwoUploaded, Reason);
Assert.AreNotEqual(0, message.Id);
}
如果你真的想你的测试用例超出这个值,使用外指定它们作为vaiable,你可以在设定Init()
例如:
private WorkflowRuntime RunTime;
[Setup]
public void Init()
{
RunTime = new WorkflowRuntime();
}
[Test]
public void TestInsert()
{
//RunTime can now be accessable here.
}
你可以申请,如果条件的话,这如果条件对[TestFixtureSetUp]属性适用于,如果条件可以使用Assert.Ignore(“”)。