底下這段程序代碼示范了如何使用TestFixtureSetUp/TestFixtureTearDown
namespace UnitTestingExamples
{
using System;
using NUnit.Framework;
[TestFixture]
public class SomeTests
{
[TestFixtureSetUp]
public void RunBeforeAllTests()
{
Console.WriteLine( “TestFixtureSetUp” );
}
[TestFixtureTearDown]
public void RunAfterAllTests()
{
Console.WriteLine( “TestFixtureTearDown” );
}
[SetUp]
public void RunBeforeEachTest()
{
Console.WriteLine( “SetUp” );
}
[TearDown]
public void RunAfterEachTest()
{
Console.WriteLine( “TearDown” );
}
[Test]
public void Test1()
{
Console.WriteLine( “Test1” );
}
}
}
程序的輸出將是下面的結(jié)果::
TestFixtureSetUp
SetUp
Test1
TearDown
SetUp
Test2
TearDown
TestFixtureTearDown
如果Test2單獨(dú)執(zhí)行輸出的結(jié)果將是:
TestFixtureSetUp
SetUp
Test2
TearDown
TestFixtureTearDown