Nunit framework Attributes

There are many attributes available in the Nunit framework, attributes are annotations declared in the method.

In this tutorial, we will take few of the attributes that are frequently used in Test Automation.

[OneTimeSetUp] - Run before the test run
[SetUp] - Run before each method
[Test] - Treated as Test case
[TearDown] - Run after each method
[OneTimeTearDown] - Run after all the tests are executed
[TestFixture] - Nunit see the class with Test Fixture / SetUpFixture
[SetUpFixture] - It provides SetUp and TearDown for the entire assembly.
[Category("Smoke")] - To execute a groups of tests
[Test, Description("Login test")] - The Description attribute is used to apply descriptive text to a Test, TestFixture and Assembly. The text will appear in the XML output.
[Ignore("To be ignored till build is available")]
[Ignore("To be ignored till build is available",Until = "2020-10-31 12:00:00Z")] - The test will be ignored and will give the warning in the report.
[Test, Timeout(1000)] - Throws an error if the method exceeds the time

Example for each Test Attribute

 
namespace SeleniumNunit.test
{
    [TestFixture]
    class Login : Hooks
    {
        [SetUp]
        public void SetUp()
        {
            Console.WriteLine("Setup method ...");
        }
        [Test]
        public void testMethod()
        {
            Console.WriteLine("Test method ... ");
        }
        [Test]
        public void loginMethod()
        {
            Console.WriteLine("Login method ... ");
        }
        [TearDown]
        public void TearDown()
        {
            Console.WriteLine("Teardown method ... ");
        }
    }
}

 

One time setup and One time tear down is placed in a separate file with SetupFixture and can be extended with the TestFixture

 
namespace SeleniumNunit.utils
{
   
    [SetUpFixture]
    class Hooks
    {
        public IWebDriver driver;
        [OneTimeSetUp]
        public void OneTimeSetUpMethod()
        {
           Console.WriteLine("OneTimeSetupMethod ...");
        }
        [OneTimeTearDown]
        public void OneTimeTearDownMethod()
        {
            Console.WriteLine("OneTimeTearDownMethod ...");
        }
    }
}
 

Related Tutorials