NUnit framework is an open–source unit testing framework in C#. It is an updated version of JUnit. The .Net Foundation has developer this type of unit test framework. It is used for the development and execution of unit tests in the .Net programming language.
Purpose of using NUnit Framework:
In the NUnit framework, each module is tested independently to ensure that the objective is met. The NUnit Framework contains a different type of attribute that is used during the unit tests. They are used to define Test – Fixtures, Test methods, Expected Exception, and Ignore methods.?
- Download and install Visual Studio for development. Based on the requirement, you can download the Community/Professional/Enterprise edition of Visual Studio 2019.
- Based on the OS, we can select the packages for the NUnit testing framework in the Visual Studio.
- Once the installation is complete, we will need to restart the machine before Visual Studio 2019 can be used.
- In NUnit, first, we need to create a new project in Visual Studio. Go to Visual Studio ? New ? Project.
- In the created project, will have the basic test with NUnit annotations used in the source code.
- You need to reference the nunit.framework.dll. If you use NuGet, this will be done for you automatically while installing NUnit.
- After installing the NUnit, we need one more dll that needs to be installed in the project.
NUnit Test Adapter for Visual Studio
- The NUnit Test Adapter allows you to run NUnit tests in Visual Studio.
- If you are not adding this component in the project, you may not be able to find the tests in test Explorer.
- Navigate to Tools? NuGet Package Manager? Manage NuGet Packages for Solution and search for “NUnit & NUnit Test Adapter”.
- You can find both the Libraries in project reference.
- Setup Selenium WebDriver with Visual Studio in C#:
1.Navigate to Tools ? NuGet Package Manager ? Manager NuGet Packages for Solution and search for ‘Selenium’.
- After clicking the ‘Install’ button, click ‘Okay’ and then click ‘ I Accept’.
- After the installation of Selenium WebDriver, you can start automation testing using NUnit in the selenium C# framework.
- As we are making use of local Selenium WebDriver, we should download the Selenium WebDriver for Chrome and install/copy it to the location where the Chrome web browser (Chrome.exe) is located.
List of Attributes/Annotation(Order of Execution):
- OneTimeSetUp – If we use this attribute, then the execution starts from this section.
- SetUp – Setup executes after ‘OneTimeSetUp’
- Test – Test Executes after ‘SetUp’
- TearDown – Test Executes after ‘Test’
- OneTimeTearDown –This attribute executes at last.
- If we do not use ‘OnetimeSetUp’ & ‘OneTimeTearDown’, then the Execution starts from ‘SetUp’ and the End of ‘TearDown’.
Find the below usage of each Attribute/Annotations used in NUnit framework,
- The TestFixture attribute is a sign that a class contains test methods. After you mention this attribute to a class in your project, the Test Runner application will scan it for test methods.
- The Test attribute indicates that a method within a test fixture should run by the Test Runner application. The method must be public, return void, and can’t be run when the Test Fixture is run.
- Now, let us check our test. Go to the Visual Studio Test Explorer.
- If we check the Left side of Visual Studio, it will open a pane called Test Explorer with all the test scenarios as follows.
- It is used when you want to execute a piece of code in each test case. It identifies a method to be executed each time before a Method or Test is executed.
- Next, the execution moves to ‘Test’ Attribute.
- If we check the sequence of execution, the method that used TearDown attribute will be executed last after the execution of the Test case.
Here we see the below a few more attributes:
- By using this attribute, we need to install ‘MSTest.TestFramework’ package. You can see the error before installing that package.
- Here, you can see the exception method that has the custom exception.
- The Ignore attribute is required to indicate that a test should not be run on a method. You can see the output in Test Detail summary.
- It will run just once before any of the tests in the class. So, if you run the class, the method annotated with [OneTimeSetUp] will get executed just once before the first test, and then never again.
- It will run once after all the tests are completed. Common usage for these annotations is opening and closing connections before all the tests.
- Annotations used in NUnit help in speeding up test development & execution as tests can be executed with numerous input values.
- TDD is primarily useful as unit tests are instrumental in finding issues/bugs during the early stages of development. NUnit test framework can be used with Selenium if you plan to use TDD (Test Driven Development) for the test activity.
- Provides you the ability to run your test cases in parallel.
- Using NUnit, you can execute test cases from console runner by either a third-party automation testing tool or by the NUnit Test Adapter inside the Visual Studio.