JUnit vs NUnit: Framework Comparison
By Gurudatt S A, Community Contributor - June 30, 2022
Unit testing is quintessential for a Software Development Lifecycle to check if the single independent block of code is working as expected. For creating and executing unit tests you need an apt Unit Testing Framework. This Unit Testing Framework provides annotations/attributes using which one can initialize drivers/data, tear down the objects, and Execute Tests.
This article discusses two popular Unit Testing Frameworks available for Java and for .Net and compares them.
What is JUnit Framework?
JUnit is a Unit Testing Framework for Java. JUnit was initially developed for the purpose of testing source code and evolved to be one of the main Unit Testing tools for Web, API, and DataBase testing (Using libraries like Selenium, RestAssured, etc.)
JUnit can be used by either adding
The below code, the example shows how to quickly run a Selenium test using JUnit Annotations
import java.io.File; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.JUnit.After; import org.JUnit.Before; import org.JUnit.Test; public class SeleniumTestngTest { WebDriver driver; @Before public void initDriver() { String path = System.getProperty("user.dir") + File.separator + "driver" + File.separator + "chromedriver-2"; System.setProperty("webdriver.chrome.driver", path); WebDriver driver=new ChromeDriver(); driver.manage().window().maximize(); } @Test public void firstTest () { driver.get("https://www.bstackdemo.com/"); } @After public void tearDown() { driver.close(); driver.quit(); }
What is NUnit Framework?
NUnit is a unit testing tool ported initially from JUnit for .Net Framework and is completely an Open Source Project. NUnit was released in the early 2000s, and though the initial NUnit was ported from JUnit, the recent .Net version 3 is completely rewritten from scratch.
To run the NUnit test we need to add attributes to our methods. An example, the attribute [Test], which indicates the Test method. The Attributes in NUnit are similar to JUnit/TestNG Annotations.
Below are the Nuget Packages required by NUnit
- NUnit
- NUnit3TestAdapter
- Microsoft.NET.Test.Sdk
Below code, an example shows how to run Selenium test using NUnit
using System; using OpenQA.Selenium; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.UI; using NUnit.Framework; using System.Collections.Generic; using OpenQA.Selenium.Chrome; namespace SingleTest { [TestFixture] public class AttributeTest { IWebDriver driver; [OneTimeSetUp] public void oneTimeSetup() { Dictionary<string, object> browserStackOptions = new Dictionary<string, object>(); browserStackOptions.Add("userName", "gurudattanaap_jG"); browserStackOptions.Add("accessKey", "9yV8DdY2CwNizFW"); ChromeOptions options = new ChromeOptions(); options.AddAdditionalOption("bstack:options", browserStackOptions); driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), options); } [SetUp] public void setup() { driver.Manage().Window.Maximize(); driver.Url = "https://bstackdemo.com/"; } [Test] public void test1() { DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver); fluentWait.Timeout = TimeSpan.FromSeconds(5); fluentWait.PollingInterval = TimeSpan.FromMilliseconds(250); fluentWait.Until(mydriver => mydriver.Title == "StackDemo"); } [Test] public void test2() { DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver); fluentWait.Timeout = TimeSpan.FromSeconds(5); fluentWait.PollingInterval = TimeSpan.FromMilliseconds(250); fluentWait.Until(mydriver => mydriver.Title == "StackDemo1"); } } }
JUnit Vs NUnit: Feature Differences
As both the frameworks are very similar in nature, we are comparing them based on different annotations and attributes used by each framework.
Criteria | NUnit | JUnit |
---|---|---|
Programming Language | .Net | Java |
Test Declaration | [Test] | @Test |
Grouping Tests by Class | [TestFixture] | @BeforeClass@AfterClass |
Setup and Teardown | [Setup] [TearDown] | @Before @After |
One-time setup/ teardown before all tests execution | [OneTimeSetUp] [OneTimeTearDown] | @BeforeClass@AfterClass |
Skip a test | [Ignore(“reason”)] | @Ignore |
Group test by Category | [Category()] | @Category |
Test Data setup/Cleanup before executing test | [TestFixtureSetup] [TestFixtureTearDown] | @BeforeClass @Before @AfterClass @After |
Execution in Isolation | Configurable | @Isolated |
Read More: Best Practices for Unit Testing
Conclusion
JUnit is a very popular Unit testing framework for Java and NUnit, which is ported from JUnit and has become a popular Unit testing framework in the .Net world. Both provide the ability for developers and testers to write Unit, Integration, and End to End testing using popular libraries like Selenium, RESTAssured, etc.
However, no matter which Unit Testing framework you choose, it is important to test the application on a real device cloud for more accurate test results. By testing under real user conditions you can identify the bottlenecks in the real user experience and rectify them in time before release.