Saturday, September 14, 2019

NUnit the ease of Setup and TearDown Attributed methods

In NUnit there are two attributes [SetUp] and [TearDown] which eases the whole process of testing many test methods. The Setup attributed method will run before each unit test is run, and the TearDown attibuted method will run after each unit test. This eases the whole process of running all the test cases at once. A Sample NUnit test fixture wuold look like this:

[TestFixture]
class BhrPassengerServiceTest
{
[SetUp]
public void Before_Each()
{
using (var context = new ApplicationDbContext())
{
context.Database.ExecuteSqlCommand("DELETE FROM TableOne");
context.Database.ExecuteSqlCommand("DELETE FROM TableTwo");
context.Database.ExecuteSqlCommand("DELETE FROM TableThree");

context.Areas.Add(AreaOne);
context.Areas.Add(AreaTwo);
context.SaveChanges();
}
TimeService.Init();
}

[TearDown]
public void Teardown()
{
}

[Test]
public void MyClassToBeTested_TestScenarioOne_ShouldGetThisResult()
                {
                }

                 private IList<string>  HelperMethodOneUsedWithInTestMethod()
                 {
                 }
         }

No comments:

Post a Comment