What is Unit Testing?
Unit testing is where the program is broken down into a series of units, or functions, or areas - each of these is tested individually/standalone in a lot of detail. This allows us to check that each function works as it should. So if we give one of our methods/functions a set of inputs we can then verify that we get the expected output for each example.We will need to check our functions with not only predictable values, but also borderline values and just as importantly totally unexpected values (e.g. wrong datatype, size, empty, irrelevant...)
Only the characteristics of that unit need to be tested, as everything else in the application will be covered by the other unit tests.
It is usually an automated process, once the tests are written they will run automatically when the tests are configured to run.
Benefits of Unit Testing
- Identify failures in our code BEFORE it gets integrated with the larger application
- Allows you to continue verifying that your method still works as expected in all (tested) cases while your refactor/ change the logic in the methods body
- If your approaching development from a unit test perspective, you'll likely be writing code that is easier to test - more modular, clear, standalone methods - this is better code.
- Prevent future changes from breaking functionality.
- They help you really understand the design of your code
- They give you instant feedback, and that green tick when they all pass is so satisfying!
- Faster to develop more robust code
- They can help with code reuse
- Forces better code documentation
Here are some more reasons why you should test, from the SO community:
Disadvantages of Unit Testing
- When your just getting started, they can be time consuming while you get used to them. Ultimately they will save time in the long run, but it doesn't always feel like that.
- Learning curve. Although the principle of unit testing is very simple, when you actually sit down to write unit tests for the first time, it is often hard to know that and how you should be testing each module. A solution to this is to look at example tests on the internet. For example all decent Node.js projects and modules on GitHub will have a test directory, that you can read or run.
- Trying to implement unit tests to legacy code/ code not written nicely is sometimes close to impossible. The solution to this, is don't wirte bad code in the first place and write tests first or during development.
As you can see the advantages massively outweigh the very weak disadvantages.
Unit testing confirms the code your writing is awesome!
A good unit test is:
- Able to be fully automated
- Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
- Can be run in any order if part of many other tests
- Runs in memory (no DB or File access, for example)
- Consistently returns the same result (You always run the same test, so no random numbers, for example. save those for integration or range tests)
- Runs fast
- Tests a single logical concept in the system
- Readable
- Maintainable
- Trustworthy (when you see its result, you don’t need to debug the code just to be sure)
Links for Further Reading
Unit testing in AngularJS
Unit testing in PHP
http://code.tutsplus.com/articles/the-beginners-guide-to-unit-testing-what-is-unit-testing--wp-25728
Unit testing in Android
Unit testing in NodeJs
Unit testing in Swift