MicrosoftTeams-image
MicrosoftTeams-image

UNIT TESTING TOOLS

Unit testing tools are used easily to simplify the unit testing process with secure documentation and design and can decrease the count of bugs produced. These tools are used to test the source code of an application by the developers. In Eclipse, all unit testing tools are found as plug-in. Through unit testing, we can achieve performance and compatibility. 

TOOLS USED FOR UNIT TESTING: 

JUnit: 

JUnit is a framework used for unit testing of java programming languages. It is an open-source unit testing framework. The main purpose of using Junit testing tool is that it can be used for test driven development. There are annotations in JUnit which helps the developers to find the test method. 

FEATURES OF JUnit TESTING:

1.Easy to develop the code that enhances the quality of the code.

2. Annotations are used for the test methods. 

3.Time consumed for running test cases are very less. 

4.Assertions are provided for expecting the results. 

5.Less complex and shows the test progress is shown in the progress bar as green when successful run and red when test fails. 

EXAMPLE: 

import org.junit.runner.JUnitCore; 

import org.junit.runner.Result; 

import org.junit.runner.notification.Failure; 

public class TestRunner { 

   public static void main(String[] args) { 

      Result result = JUnitCore.runClasses(TestJunit.class); 

      for (Failure failure : result.getFailures()) { 

         System.out.println(failure.toString()); 

      } 

      System.out.println(result.wasSuccessful()); 

   } 

NUnit: 

NUnit is a unit testing framework used for all .NET languages. It is the most popularly used open-source testing tool and initially ported from JUnit. NUnit is a part of larger software design paradigm called as Extreme Programming. NUnit has Graphical User Interface (GUI). Multiple tests can run simultaneously and tests will run continuously by providing result immediately. 

FEATURES OF NUnit TESTING:

1.Tests can run parallelly  .

2.Supports multiple platforms.

 3.Supports data driven tests. 

4.Every test case can be added to one or more categories, to allow for selective running .

5.Tests can be run from a console runner within Visual Studio through Test Adapter or through third party runners. 

EXAMPLE: 

namespace Bank 

  using NUnit.Framework; 

  [TestFixture] 

  public class AccountTest 

  { 

    [Test] 

    public void TransferFunds() 

    { 

      Account source = new Account(); 

      source.Deposit(200m); 

      Account destination = new Account(); 

      destination.Deposit(150m); 

      source.TransferFunds(destination, 100m); 

      Assert.AreEqual(250m, destination.Balance); 

      Assert.AreEqual(100m, source.Balance); 

    } 

  } 

TestNG: 

TestNG is Testing Next Generation which is an automation testing. TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use. TestNG is an open-source automated testing framework It is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc. You can generate a proper report, and you can easily come to know how many test cases are passed, failed, skipped and you can also execute the failed test cases separately using TestNG . 

FEATURES OF TestNG TESTING:

1.Flexible runtime configuration. 

2.Contains Annotations. 

3.Run time and compile time configuration test codes are separated .

4.Supports multi-threaded testing and dependent test methods, parallel testing, load testing, and partial failure. 

5.It has Flexible plug-in API. 

EXAMPLE: 

import org.testng.Assert; 

import org.testng.annotations.Test; 

public class TestEmployeeDetails { 

   EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic(); 

   EmployeeDetails employee = new EmployeeDetails(); 

   @Test 

   public void testCalculateAppriasal() { 

      employee.setName(“Rajeev”); 

      employee.setAge(25); 

      employee.setMonthlySalary(8000); 

      double appraisal = empBusinessLogic.calculateAppraisal(employee); 

      Assert.assertEquals(500, appraisal, 0.0, “500”); 

   } 

   // Test to check yearly salary 

   @Test 

   public void testCalculateYearlySalary() { 

      employee.setName(“Rajeev”); 

      employee.setAge(25); 

      employee.setMonthlySalary(8000); 

      double salary = empBusinessLogic.calculateYearlySalary(employee); 

      Assert.assertEquals(96000, salary, 0.0, “8000”); 

   } 

PHPUnit: 

PHPUnit is testing framework used for PHP Programming languages. PHPUnit is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. It is used to enhance the PHP developers’ productivity during development. 

FEATURES OF PHPUnit TESTING: 

1.Simplifies integration and reduces uncertainty in the unit.

2. Usage of assertions, annotations, mocks and stubs. 

3.Time of development is reduced.

4. It empowers you to write higher quality PHP code and to develop. 

5.It can output test results in a number of different formats, including JUnit XML, Test Anything Protocol, JSON, and TestDox. 

EXAMPLE: 

<?php 

class CalculatorTest extends \PHPUnit\Framework\TestCase 

public function testAdd() 

   { 

    $calculator = new App\Calculator; 

    $calculator->setOperands([5,20]); 

    $this->assertEquals(25, $calculator->add()); 

   } 

Scroll to Top