There are 2 cases which we need to consider while allocating resources in ATPs (Resources might be Creating users, Connection with database). They are
- Creation and deletion of resources after each test.
- Creation and deletion of resources after set of tests.
Creation and deletion of resources after each test:
If we want to create instance of particular object before execution of a test and want to clean up memory allocated to that object after execution of that test, then we use Test SetUp
and Test TearDown
attributes of NUnit. In your case object is creation of number of Users.
[SetUp]
: Function which is decorated with Test SetUp attribute contains piece of code that executes before the execution of any test.
[TearDown]
: Function which is decorated with Test TearDown attributes contains piece of code that executes after execution of any test
Implementation:
[TestClass]
public class UnitTest1
{
[SetUp]
public void SetUP()
{
// Creating Users with proper credentials
}
[TestMethod]
public void TestMethod1()
{
//Write your ATP
}
[TearDown]
public void TearDown()
{
//Clean up
}
}
Creation and deletion of resources after set of tests:
Now If we want to create instance of an object for set of tests and want to clean up memory after execution of all tests then [TestFixtureSetUp]
and [TestFixureTearDown]
to initialize an object and to clean up memory respectively. Again In your case object can be creation of set of users.
[TestFixtureSetUp]
: Function decorated with TestFixtureSetUp
will executes once before the execution of group of tests.
[TestFixtureTearDown]
: Function decorated with TestFixtureTearDown
will executes once after the execution of group of tests.
Implementation
[TestFixture]
public class Tests
{
[TestFixtureSetUp]
public void Setup()
{
//Create users with credentials
}
[Test]
public void _Test1()
{
//Test_1
}
[Test]
public void _Test2()
{
//Test2
}
[TestFixtureTearDown]
public void CleanUp()
{
//Cleanup; Here you need to add code to Delete all users
}
}
Note: I will suggest you, if you are trying to create and delete users for particular ATP then go with SetUp
and TearDown
. If you are trying same for bunch of ATPs, I would recommend you to go with TestFixtureSetUp
and TestFixtureTearDown
.
"If your test get pass or fail, SetUp and TearDown functions will execute"
References: