These attributes are used to construct the test environment
prior to running the tests (TestFixtureSetUp
and SetUp) and also to restore the
environment after the tests are completed (TestFixtureTearDown
and TearDown)
TestFixtureSetUp/TestFixtureTearDown (NUnit 2.1)
These two attributes are used inside a TestFixture to provide a
single set of functions that are performed once (TestFixtureSetUp)
prior to executing any of the tests in the fixture and once after
(TestFixtureTearDown) all tests are completed. A TestFixture can
have only one TestFixtureSetUp method and only one
TestFixtureTearDown method. If more than one of each type is defined
the TestFixture will not be run. It will compile however.
Example:
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
[TestFixtureSetUp] public void Init()
{ /* ... */ }
[TestFixtureTearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
Imports System
Imports Nunit.Framework
Namespace Nunit.Tests
<TestFixture()> Public Class SuccessTests
<TestFixtureSetUp()> Public Sub Init()
' ...
End Sub
<TestFixtureTearDown()> Public Sub Dispose()
' ...
End Sub
<Test()> Public Sub Add()
' ...
End Sub
End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;
namespace NUnitTests
{
[TestFixture]
public __gc class SuccessTests
{
[TestFixtureSetUp] void Init();
[TestFixtureTearDown] void Dispose();
[Test] void Add();
};
}
#include "cppsample.h"
namespace NUnitTests {
// ...
}
package NUnit.Tests;
import System.*;
import NUnit.Framework.TestFixture;
/** @attribute NUnit.Framework.TestFixture() */
public class SuccessTests
{
/** @attribute NUnit.Framework.TestFixtureSetUp() */
public void Init()
{ /* ... */ }
/** @attribute NUnit.Framework.TestFixtureTearDown() */
public void Dispose()
{ /* ... */ }
/** @attribute NUnit.Framework.Test() */
public void Add()
{ /* ... */ }
}
|
SetUp/TearDown (NUnit 2.0)
These two attributes are used inside a TestFixture to provide a
common set of functions that are performed prior (SetUp) and after
(TearDown) a test method is called. A TestFixture can have only
one SetUp method and only one TearDown method. If more than one
of each type is defined the TestFixture will not be run. It will
compile however.
Example:
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
[SetUp] public void Init()
{ /* ... */ }
[TearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
Imports System
Imports Nunit.Framework
Namespace Nunit.Tests
<TestFixture()> Public Class SuccessTests
<SetUp()> Public Sub Init()
' ...
End Sub
<TearDown()> Public Sub Dispose()
' ...
End Sub
<Test()> Public Sub Add()
' ...
End Sub
End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;
namespace NUnitTests
{
[TestFixture]
public __gc class SuccessTests
{
[SetUp] void Init();
[TearDown] void Dispose();
[Test] void Add();
};
}
#include "cppsample.h"
namespace NUnitTests {
// ...
}
package NUnit.Tests;
import System.*;
import NUnit.Framework.TestFixture;
/** @attribute NUnit.Framework.TestFixture() */
public class SuccessTests
{
/** @attribute NUnit.Framework.SetUp() */
public void Init()
{ /* ... */ }
/** @attribute NUnit.Framework.TearDown() */
public void Dispose()
{ /* ... */ }
/** @attribute NUnit.Framework.Test() */
public void Add()
{ /* ... */ }
}
|
SetUp/TearDown Inheritance
These two attributes are inherited from base classes. Therefore,
if a base class has defined a SetUp method that method will be called
prior to execution of test methods in the derived class. If you
wish to add more SetUp/TearDown functionality in a derived class
you need to mark the method with the appropriate attribute and then
call the base classes method.
|