NUnit
NUnit is a testing unit framework designed to support test execution for .NET. It allows you to run tests that were written in C# without the need to initialize the test classes that you have written.
What does this mean?
The default unit testing template defines one method for setting up the test, one method for executing the test, and one test for cleaning up once the test has been executed (whether the test succeeded or not). Consider the code template below:
Example : C# NUnit Test Expand source
[TestFixture]
public class IOSWebTest
{
// you have configured an access key as environment variable,
// use the line below. Otherwise, specify the key directly.
private string accessKey = Environment.GetEnvironmentVariable("SEETEST_IO_ACCESS_KEY");
private string testName = "iOS Safari Web Test";
protected IOSDriver<IOSElement> driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
[SetUp()]
public void SetupTest()
{
dc.SetCapability("testName", testName);
dc.SetCapability("accessKey", accessKey);
dc.SetCapability(MobileCapabilityType.PlatformName, "iOS");
dc.SetCapability("autoDismissAlerts", true);
dc.SetCapability(MobileCapabilityType.BrowserName, "safari");
driver = new IOSDriver<IOSElement>(new Uri("https://cloud.seetest.io:443/wd/hub"), dc);
}
[Test()]
public void TestiOSApp()
{
driver.Navigate().GoToUrl("https://amazon.com");
}
[TearDown()]
public void TearDown()
{
driver.Quit();
}
}
As you can see, each method is marked with an annotation that defines its role in the test workflow:
- [SetUp()] - the method that prepares all the parameters needed for the test
- [Test()] - the method where the test steps are defined
- [TearDown()] - the method that cleans up after the test has finished running
Without NUnit, you would have to create an instance of the test class and run inside a static main method. With NUnit, you do not need to write create a class test instance and run it in a main method.
Getting Started with NUnit
To use NUnit, install the required NUnit packages using Visual Studio Package Manager Console by running these commands:
Install-Package NUnit
Install-Package NUnit3TestAdapter
These packages are added to your project. They are also added to the packages.config file in your project. This makes the project importable, allowing it to be transferred to other team members and even checked into a git repository.
To learn more about NUnit, see the NUnit Documentation.