2015-09-22 47 views
2

在我的测试下一个流发生:如何参数TestFixtureSetUp(NUnit的)

  1. 我做一些动作(例如购买产品)的所有测试前,在每个测试我检查运行
  2. 然后一个断言

我使用NUnit框架来运行测试,因此我使用[TestFixtureSetUp]来标记一组在所有测试之前完成的操作。然后我使用[Test]或[TestCase()]来运行测试。

通常情况下,我需要检查相同的东西,但执行不同的流程。所以我必须参数化[TestFixtureSetUp]。我能以某种方式做到这一点吗?

所以我想在我的所有测试都依赖于参数之前执行一次这样的操作。

如果可以用不同的框架或不同的流结构做的请告诉我),我的代码

例子:

[TestFixtureSetUp] //This will be done once before all tests 
public void Buy_Regular_One_Draw_Ticket(WayToPay merchant) 
{ 
      //here I want to do some actions and use different merchants to pay. 

      //So how can I send different parameters to this method? 

} 
+0

一个例子将大大有助于理解,你实际上试图实现什么 – drkthng

+0

当然)谢谢你的线索) –

回答

3

家伙的解决方案是下一篇:构造该类在[TestFixtureSetUp]之前运行,因此现在[TestFixtureSetUp]中所做的所有操作都是在类的构造函数中进行的。

而且我们有能力向构造函数发送参数!为此我们使用[TestFixture()]。

整个代码是下一个:

[TestFixture(WaysToPay.Offline)] 
[TestFixture(WaysToPay.Neteller)] 
public class DepositTests 
{ 
     //Constructor takes parameters from TestFixture 
     public DepositTests(WaysToPay merchant) 
     { 
      //Do actions before tests considering your parameters 
     } 

     [Test] 
     public void Your_test_method() 
     { 
      //do your verification here 
     } 
    } 

使用这种方法,而不是使用[TestFixtureSetUp]你可以让你的测试更灵活。所以行为与[TestFixtureSetUp]可以获得参数相同。

相关问题