[Unit Testing] Getting Started with Unit Testing
Unit Testing:
- Is a software development process in which the smallest testable parts of an application are individually and independently tested.
- Simply verifies that individual units of code (mostly functions) work as expected.
- Is a component of test-driven development (TDD).
- Usually performed by the developer.
- Performed using White Box Testing method.
- Is meant to give the developer more confidence and trust in the code.
- Encourages modularity - write programs that do one thing, and do it well! (Single Responsibility Principal)
- Involves faking dependencies and methods in order to isolate the scope of the System Under Test (SUT)
Simple Example
The most simple example is testing the result of a method is within our expectation. For example, we we have a combineNumbers method below:
To test this, we simply compare the result of this method with our expected result.
When you run the tests, you will be informed that these tests have passed.
When to do Unit Testing?
This should be done as often as possible.
In the bigger picture of unit testing, this should be done before we do Integration Testing.
Benefits
- Anyone can look at the unit test and gain a basic understanding of the unit API, including the specifications and use case; documentation purpose.
- Design Aid: gives you a clearer perspectie on the ideal API design.
- Allows programmers to refactor code at a later date, and make sure the module still works correctly. (ie. Regression Testing and Continuous Delivery Aid)
- Can test parts of the project without waiting for others to be completed.
- Increases confidence in changing/maintaining code.
- Force code to be modular; making code easier to re-use.
- Cost of fixing detect during unit testing is lesser in comparison to detects found at higher levels; compare cost (time, effort, destruction, humiliation) of defect found in acceptance test, or during production.
- Debugging is easy; you have a smaller scope to look at.
- Codes are more reliable.
Limitations
- Can't be expected to catch every error in the program.
- Can't catch integration errors or broad system level errors.
Resources
https://searchsoftwarequality.techtarget.com/definition/unit-testinghttps://medium.com/@lazlojuly/how-to-get-started-with-unit-testing-part-1-7f490bbf560a
http://softwaretestingfundamentals.com/unit-testing/
https://www.guru99.com/unit-testing-guide.html
https://medium.com/javascript-scene/what-every-unit-test-needs-f6cd34d9836d
Comments
Post a Comment