A unit is a smallest testable component of the code. Each unit’s typical characteristic is it has one or more inputs and a single output. Unit testing is the first level of testing in the development life cycle and it is performed before the integration testing.
Unit tests are automated to perform the testing faster. It also can be done manually. In most of the product development organizations, developers write unit tests as a preventive approach to fix the bugs before it is release to Quality Assurance (QA) team.
Here, are some best practices that can be followed to perform unit testing.
Create testable code
Designing code plays an important role in development. The well designed code is easier to maintain, optimize and reuse. Writing code in the module pattern helps keeping the code clean and organized. It is much easier and faster to write unit tests for such well designed code.
Create/Write unit tests
Write tests for the core features/units that are critical to business.
Some of the unit test examples are:
- A function that changes the state of product to “Out of Stock”.
- A function that validates each field of the form.
- A function that displays validation messages
- A function that sorts a list of names
When the feature is in developments, unit test writing is a parallel activity as at that time, a developer knows all the small components that are required to implement user story. Usually, this is how the application component is divided – Story Features Units
Select a good testing tool for unit testing
Perform integration tests
Integration testing is equally part of the testing process where end-to-end tests are written and verifying the state of app/UI before the release to production.
You need to use Grunt for test and build process that enables the developers to install testing frameworks like Jasmine or Karma in a single command, and then execute all tests within a project when files change, or during the build process.
Some recommended integration testing tools are – Karma and CasperJS.
Some of the integration testing examples are:
- A user searches for restaurant location, clicks the search result and is taken to the restaurant details page.
- User opens the cart, removes an item, and sees the number of items and the subtotal decrease.
It is a time to perform or to flesh out integration tests when the user stories are written. High level testing is performed at this time.
- Make high level test cases for integration tests at the time where the user stories are being developed.
- When writing integration tests, please remember that they should be application-level so that changes in the umderlying implementations do not affect them.
- Break down application components into units and identify critical units and write unit tests for these units.
- Perform negative as well as positive testing.
- Consider testing tools for unit testing and integration testing.