开发者

2 Questions about nUnit

I have 2 questions about functionality of nunit.

What is the difference between [TestFixtureSet开发者_JS百科Up] and [SetUp] attributes ?

I am writing a some class with tests and I see that half of my test functions need one setup, And an another half needs another set up. How can I have in one class two little different SetUp functions that are called with different functions

Thanks.


Method marked with [TestFixtureSetUp] attribute will be executed once before all tests in current test suite, and method marked with [SetUp] attribute will be executed before each test.

As for class with tests which contains tests requring different set up functions. Just split this class in two - each one with it's own SetUp function.

    [TestFixture]
    public void TestSuite1
    {
      [SetUp]
      public void SetUp1()
      {
        ...
      }

      [Test]
      public void Test1()
      {
        ...
      }
    }

   [TestFixture]
    public void TestSuite2
    {
      [SetUp]
      public void SetUp2()
      {
        ...
      }

      [Test]
      public void Test2()
      {
        ...
      }
    }

or call SetUp function explicitly

    [TestFixture]
    public void TestSuite
    {

      public void SetUp1()
      {
        ...
      }

      public void SetUp2()
      {
        ...
      }

      [Test]
      public void Test1()
      {
        SetUp1();

        ...
      }

      [Test]
      public void Test2()
      {
        SetUp2();

        ...
      }
    }


A TestFixtureSetup method is executed once before any of the test methods are executed. A Setup method is executed before the execution of each test method in the test fixture.

How can I have in one class two little different SetUp functions that are called with different functions

You can't have two different SetUp functions in a single class marked as TestFixture. If individual tests need some initialization then it makes sense to put the initialization code inside those function themselves.

I see that half of my test functions need one setup

I think then you need to factor out the tests...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜