TestFixtureSetUp and TestFixtureTearDown in Unit Testing using NUnit: Part 4

We have been learning Unit Testing using NUnit together. Read first three parts of this series below,

How to do Unit Test using NUnit : Part 1

Test SetUp and TearDown in Unit Testing using NUnit : Part 2

Ignore Test in Unit Testing using NUnit: Part 3

In Part 2 , we discussed about TestSetUp and TearDown. If notice TestSetUP and TearDown is related to tests. They both get executed each time a test get executed. So if there are 5 tests in Test Fixture then TestSetUp and TearDown will get executed 5 times. Sometime this is good and needed for certain requirements. However doing heavy tasks like cleaning up whole database etc. each time for a test may not be a good idea. You may want to perform certain task for whole test fixture than for each and individual tests.

You can execute certain code before execution of all the tests in test fixture by using attributing that code with [TestFixtureSetUp]. This is used to create setup for all tests at one instant.

So you can create TestFixtureSetup or setup for whole test as following,

clip_image002

Basically we use TestFixtureSetUp to setup for all test. On the other hand you can clean up for all tests once using TestFixtureTearDown.

clip_image004

Using TestFixtureSetUp and TestFixtureTearDown are used to set up test and clean up test. I hope you find this post useful. Thanks for reading.

Ignore Test in Unit Testing using NUnit: Part 3

We have been learning Unit Testing using NUnit together. Read first two part of this series below,

How to do Unit Test using NUnit : Part 1

Test SetUp and TearDown in Unit Testing using NUnit : Part 2

You may come across a scenario when test is itself broken. While executing test you need to ignore certain broken kind of test. Thankfully NUnit provides a very simple solution to ignore a test. By using ignore attribute you can ignore a test.

clip_image002

You can ignore a unit test as following

clip_image004

Let us go back to unit test we wrote in previous part of this series and modify test as following,


[Test]
 [Ignore("Ignore this test it is broken")]
 public void IsValidProductPrice()
 {

 p.ProductPrice = 100;
 if (p.ProductPrice > 0)
 {
 result = true;
 }

Assert.IsTrue(result, "Product Price is valid");

 }

On execution of test you will find that Test Runner has ignored this test. Test runner will skip test attributed with ignore.

clip_image002[6]

In this post we learnt how easily a test can be ignored in NUnit. I hope you find this post useful. Thanks for reading.