2012-08-22 67 views
1

我有以下的测试夹具类和理由超越我NUnit的决定运行所有测试类的解决此一个,但不是这样的一个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中编写的所有其他项目来说都没有问题,但是这个项目的测试现在被忽略了。

+1

没有严格的答案,但是你自己格式化这些评论? – ChrisBint

+0

这是默认的GhostDoc输出,yeuck – mtijn

+0

有没有可能'insertScenarios'不可用?你使它成为'Init()'的局部变量。 –

回答

1

仍然不明白为什么NUnit应该忽略你的测试夹具(基于发布的代码片段)。代码片段是否遗漏了某些内容?

正如所指出的Wiktor的,

的SOURCENAME参数表示用于 提供测试用例的源的名称。它具有以下特点:它可能是一个 字段,属性或方法。它可以是实例或静态 成员。它必须返回一个IEnumerable或一个实现了IEnumerable的类型 。由枚举器返回的各个项目必须是 ,与 属性出现的方法的签名相兼容。

但是,通过上面列出的代码片段,您应该获得标记为Invalid not Ignored的特定测试(在Fwk 4.0上使用NUnit v2.5.10)。

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 
0

我从来没有见过一个测试用例,其中的void需要参数,你是否打算这么做? 我认为这就是为什么你在这个课堂上的考试不会运行。

[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. 
    } 
+0

这被称为“参数化测试”,测试方法需要参数,“TestCaseSource”属性用于指定应该提供参数的方法。 http://nunit.org/index.php?p=testCaseSource&r=2.0 –

+0

这就是我使用的http://www.nunit.org/index.php?p=testCaseSource&r=2.5我有多种输入场景并希望有一个设置,我写了一个很多的火 – Deviland

+0

哦对不起,现在我已经学到了一些新的东西。 –

3

如果你把测试用例出这应该工作的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(); 

} 
+0

不能在静态成员,这就是为什么它在init(),所以我可以解决这个问题。 – Deviland

+0

它不一定是静态的,编辑答案。 –

+0

是正确的,但这并不意味着我在初始化程序中使用了它。我从来没有尝试过(在测试中),但初始化insertScenarios的构造函数呢? (对不起有纠正我的坏英文有时我打字比我想象的更快) – Deviland

0

你可以申请,如果条件的话,这如果条件对[TestFixtureSetUp]属性适用于,如果赖斯你可以使用Assert.Ignore(“”)。