Key to Successful Unit Testing – How Developers Test Their Own Code?


Purpose of the Article
Black box testers don’t care about unit testing. Their main goal is to validate the application against the requirements without going into the implementation details. But as a curiosity or out of the box thinking, have you ever wondered how developers test their own code? What method they use to test before releasing code for testing? How dev-testing is important in agile process? The answer to all this is unit testing. I want to educate you on the importance of unit testing so that development and testing teams can work more collaboratively to design, test and release a excellent application.

Who knows in the future some of you may even switch to white box testing and use these code validation and improvement techniques!

What is Unit Testing?

Unit testing is not a new concept. It’s been there since the early days of the programming. Usually developers and sometime white box testers write unit tests to improve code quality by verifying each and every unit of the code used to implement functional requirements (aka test driven development TDD or test-first development).

Most of us might know the classic definition of unit testing –
“Unit testing is the method of verifying smallest piece of testable code against its purpose.” If the purpose or requirement failed then unit test has failed.

In simple words, unit testing means – writing a piece of code (unit test) to verify the code (unit) written for implementing requirements.

Unit testing in SDLC:

Unit Testing in SDLC

Importance of Writing Unit Tests

Unit testing is used to design robust software components that help maintain code and eliminate the issues in code units. We all know the importance of finding and fixing the defects in early stage of software development cycle. Unit testing serves the same purpose. Unit testing is the integral part of the agile software development process. When nightly build run unit test suite should run and report should be generated. If any of the unit test has failed then QA team should not accept that build for verification. If we set this as a standard process, many defects would be caught in the early development cycle, saving much testing time.

I know many developers hate to write unit tests. They either ignore or write bad unit test cases due to tight scheduled or lack of seriousness (yea they write empty unit tests, so 100% of them pass successfully ;-)). It’s important to write good unit tests or don’t write them at all. It’s even more important to provide sufficient time and supportive environment for real benefits.

Benefits:

  • Testing can be done in the early phases of the software development life cycle when other modules may not be available for integration
  • Fixing an issue in unit testing can fix many other issues occurring in later development and testing stages
  • Cost of fixing a defect found in unit testing is very less than the one found in the system or acceptance testing
  • Code coverage can be measured
  • Fever bugs in the system and acceptance testing
  • Code completeness can be demonstrated using unit tests. This is more useful in agile process. Testers don’t get the functional builds to test until integration is completed. Code completion cannot be justified by showing that you have written and checked in the code. But running unit test can demonstrate code completeness.
  • Expect robust design and development as developers write test cases by understanding the specifications first.
  • Easily identify who broke the build
  • Saves development time: Code completion may take more time but due to decreased defect count overall development time can be saved.

Unit Testing Cycle:

Unit testing cycle

------------

[Image credit Typemock.com]

What Makes a Good Unit Test?

Well, I’m not the right person to tell what makes a good unit test, but based on my observations on various projects I can definitely tell the characteristics of a good unit test. Bad unit test does not add value to the project. Instead, project cost increases significantly writing and managing bad unit tests.

How to write good unit tests?

  • Unit test should be written to verify single unit of code and not the integration.
  • Small and isolated unit tests with clear naming would make it very easy to write and maintain.
  • Changing other part of the software should not affect on unit test, if those are isolated and written for a specific unit of code.
  • It should run quickly
  • Unit test should be reusable

Unit Testing Frameworks

Unit testing frameworks are mostly used to help write unit tests quickly and easily. Most of the programming languages does not support unit testing with inbuilt compiler. Third party open source and commercial tools can be used to make unit testing even more fun.

List of popular unit testing tools for different programming languages:

  1. Java framework – JUnit
  2. PHP framework – PHPUnit
  3. C++ frameworks – UnitTest++ and Google C++
  4. .NET framework – NUnit
  5. Python framework – py.test

Unit Test Example

Video tutorial explaining how to write unit test using PHPUnit testing framework:

Misconceptions and Truths

  • It takes more time to write code with unit test cases, and we don’t have time for that – In reality, unit testing would save your development time in the long run.
  • Unit testing will find all bugs – It won’t, as intent of unit test is not to find bugs but develop robust software components which will have fewer defects in later stages of SDLC.
  • 100% code coverage means 100% test coverage – This does not guarantee that code is error-free.

While unit testing offers many advantages, there are also limitations involved with using it. Rigorous discipline and consistency is required throughout the software development process to overcome limitations and get the intended benefits.

Your Comments are Most Welcomed!
As a black box tester what are your observations about unit testing on your team? Does anyone have better idea for successful unit testing?


The Best Software Testing Training You'll Ever Get!

software testing QA training

14 comments ↓

#1 Devendra Jagtap on 11.06.12 at 1:03 pm

writing empty test reminds me the same story from my group, it’s true :)
sometime it becomes very hard to handle both – code completion deadlines and press to write unit tests

#2 rajeev kumar on 11.06.12 at 1:31 pm

How to test a function that its return correct result or not?

#3 Vijay on 11.06.12 at 8:27 pm

@ Devendra – that’s reality and hence a step by step approach to introduce unit testing with support from management and stakeholders can make it a success.

@Rajeev – To verify function, unit tests are written to match test output with expected result. E.g if you are verifying calculator function for addition of two numbers, and provide input values as 10+20. The output is compared with expected result to decide whether test is passed or failed.

This is very simple example. But combination of all such unit tests can make a strong test suite which can be used repetitively to verify the function for any related code changes in later stages of SDLC.

#4 Tomek Kaczanowski on 11.06.12 at 9:51 pm

Nice introduction to unit tests. However, there is so much more to say about them! :)

#5 mamata on 11.07.12 at 4:32 am

I want to learn selenium, can u send some basics like how to start what are the prerequisites in java.

#6 MALLI on 11.07.12 at 12:05 pm

its very useful to beginners and experienced people those who wants grow more in testing career

#7 Balamuraly on 11.08.12 at 6:29 am

Gr8 article. A small missing – Integration/system testing is missing in the diagram of SDLC

#8 Nice article on 11.08.12 at 8:47 am

it sounds very interesting to know about unit testing, I have been asking myself why unit testing is so important but now i understand

#9 Vijay on 11.10.12 at 10:57 am

@ Tomek – Agree. It’s very vast topic to cover. I think I managed to cover all important aspects to think positively towards unit testing.

@ Mamata – Read these docs and start recording simple tests: http://seleniumhq.org/docs/

#10 jithu on 11.12.12 at 5:21 am

good,very helpful

#11 musaddique on 11.19.12 at 8:16 pm

nice…it will definetly help me….

#12 Prapagaran on 08.25.13 at 1:44 pm

It is a Good article to learn the Importance Of Unit testing…

#13 Rajasekar on 08.25.13 at 1:45 pm

Good Article for Unit Testing…

#14 Vidushi Mishra on 12.01.14 at 9:27 am

hi, I want to start my career in testing. I read the material available on your website. kindly help me to know what more i should do. I also want to know that i don’t have any language experience (java, php, python,.net,c,c++). Or I can say I dnt know any language. so without knowing any language can I start my career in testing by reading study material available on web? Or should I also join any language course from tranning institution.

Leave a Comment