2017-06-16 143 views
3

nUnit SetupFixture ReferenceNUnit的SetupFixture类进行测试时

我的解决方案是建立这样,使用SpecFlow小黄瓜不会被调用功能
解决方案
- 测试项目
- 特点
- 步骤
- 页面项目
- 页码

我运行nUnit te使用这样的命令ST亚军:

"C:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" ".\bin\Dev\Solution.dll"

而且我将此代码添加到上面的项目结构的步骤文件夹中。

using System; 
using NUnit.Framework; 

namespace TestsProject.StepDefinitions 
{ 
    /// <summary> 
    /// This class needs to be in the same namespace as the StepDefinitions 
    /// see: https://www.nunit.org/index.php?p=setupFixture&r=2.4.8 
    /// </summary> 
    [SetUpFixture] 
    public class NUnitSetupFixture 
    { 
     [SetUp] 
     public void RunBeforeAnyTests() 
     { 
      // this is not working 
      throw new Exception("This is never-ever being called."); 
     } 

     [TearDown] 
     public void RunAfterAnyTests() 
     { 
     } 
    } 
} 

我在做什么错了?为什么在所有测试都以nUnit开始之前不会调用[SetupFixture]

+0

您使用的是哪个版本的NUnit框架? – Chris

回答

3

使用OneTimeSetUpOneTimeTearDown属性为SetUpFixture因为你使用NUnit 3.0,而不是SetUpTearDown的属性详细here

using System; 
using NUnit.Framework; 

namespace TestsProject.StepDefinitions 
{ 
    [SetUpFixture] 
    public class NUnitSetupFixture 
    { 
     [OneTimeSetUp] 
     public void RunBeforeAnyTests() 
     { 
      //throw new Exception("This is called."); 
     } 

     [OneTimeTearDown] 
     public void RunAfterAnyTests() 
     { 
     } 
    } 
} 
+0

谢谢!我终于可以通过阅读你分享的链接并找到它来工作:“任何命名空间之外的SetUpFixture为整个程序集提供了SetUp和TearDown。” –

+0

将代码放入'namespace TestsProject'而不是'namespace TestsProject.StepDefinitions'也可以。 –

+0

是的,两者都有效。调用SetUpFixture来设置它所在的任何名称空间,并在该名称空间和下面的每个测试之前和之后运行。这使您可以为不同的命名空间提供多个灯具。 –