A unit test is a test that designed confirms the functionality of a small unit of work in a large system i.e. check the functionality of a method of that held inside a class and ensures that the method behaves as expected.
A unit test usually tests ONE specific requirement for ONE specific method. Unit tests are fit one requirement/one method rule. If your test violates the one requirement/one method rule than that means it is some kind of test, but not a unit test.
Characteristics of Unit Test
Unit tests have following attributes that further define them as a unit test:
Unit tests that test one thing (including one set of inputs) at a time are targeted.
The code you are testing should be isolated from other code in the application as well as any external dependencies or events
• Repeatable and Predictable:
A unit test should be capable of being run over and over and assuming that the code under test and the test itself have not changed, producing the same result.
Generally speaking you have no guarantee that your unit tests are going to run in any specific order. Nor should your tests expect or require this.
Benefits of using Unit Tests
• One can focus his/her entire efforts on that code portion (that he/she is testing).
• Don’t need to know the detail of other parts of the system (unless have direct dependency on them). This makes writing the test and resulting the code easier.
• If any defect causes inside the code then the test will fail, so will be easily find out where is the defect and recftor the code to fix the defect.
Unit tests are generally done by developers. So as soon as find any defect code refactoring and fixing defect done instantly.