Basics of Unit Testing and Test Driven Development (TDD)

What is Unit Testing?

  • It’s a software testing approach.
  • Testing will be done for individual unit/components of software.
  • Unit is smallest part of software, which accept one or some inputs and returns an output.
  • Example, A method, which accepts the two inputs length and width as input, return the area of rectangle as output. We can consider this method as a unit. This unit will be the small part of project which can be testable.

Note: Unit can be a method, procedure, program, function etc.

  • It’s a base/first level of testing. This will be done before integration testing.
  • Done by developer at the development stage of project.

Unit Testing Types

There are two types in unit testing,

  1. Manual
  2. Automated

Unit testing are commonly automated, but somewhere it done manually. As such, unit testing automation is preferred.

Commonly used Unit Testing Tools

Below are the commonly used unit testing tools, on the other hand lots of unit testing tools available.

  1. NUnit – used for .net applications.
  2. Junit – used for java applications.
  3. PHPUnit – used for PHP applications.
  4. EMMA – Java based code coverage tool.
  5. JMockit – automated testing toolkit for Java

Unit Testing Workflow

  1. Need to create new test cases
  2. Review and Rewrite the test cases
  3. Baseline – it’s a point when to end writing test cases and start the execution.
  4. Execute the test cases.

Advantages of Unit Testing

  1. Developer will understand the complete functionality of the code/unit/component, since developer covers all possible cases in writing unit testing.
  2. We can execute tests only for some part/modules of projects, don’t need to wait whole project or other developer gets to complete their side works.
  3. This allows developer can do multiple code refinement which ensure the unit works perfectly.  
  4. To help developers to reuse the code.
  5. To help developers to identify the bugs at early development stage.
  6. Proper testing cases done in development stage saves both time and money.

Mistakes Developer makes while writing Unit Testing:

  1. Write minimal unit test cases.
  2. Concentrate on code logic and implementation only instead of unit testing.
  3. Intentions to complete the project quickly, this will reduce unit test cases counts.

These kinds of mistakes raise issues/defects in next level of testing, which is integration testing, system testing. So, which takes time and cost to fix issues.

What is Integration Testing?

  • It’s a type of testing where all the modules in the software are integrated and tested as a group.
  • Testing the user interaction/data communications between the modules.
  • Integration testing will be performed to by combined all units/modules, once unit testing has been done for individual modules.

Difference Between Unit Testing and Integration Testing

Unit TestingIntegration Testing
Each module tested separately. All modules are combined and tested.
This is base/first level of testing.Integration testing is performed after unit testing is done.
White Box testing.Black box testing.
Developer will do the unit testing.Tester will do the integration testing.
Defect is identified at development stage itself.Defect is identified at testing stage.
Can be tested by each part, without other to be completed their parts.Can be tested only after everyone complete their parts.
Less Costly.Costly compared to unit testing.
Tester/Developer will know the internal design of software.Tester/Developer doesn’t know the internal design of software.

What is Test Driven Development (TDD)?

  • This is test-first development approach.
  • First write the test code, then write minimal code to pass the test, run this test code might fails due to tests codes are written even before the development code. Need to enhance and refactor the development code to pass the test.

Test Driven Development (TDD) Workflow

  1. Write Test Code
  2. Run the Test Code
  3. Check the failed Tests
  4. Make Development code to pass tests
  5. Repeat step b.

Advantages of TDD

  1. This leads developer should completely understand the requirements and think the way to write the development code before start to write actual code.
  2. Since tests are written first, ensure the good quality of codes, also this will improve the overall product quality.
  3. You can automate the tests using TDD, this will save time compared to using other manual testing process.
  4. Chances to raise defects will be less
  5. Regression testing time will be reduced.

Advertisements

Leave a Reply