“I don’t have time to write tests because I am too busy debugging.”
Why
- T E S T S A R E G O O D D O C U M E N TAT I O N
- TESTS REDUCE BUGS
- TESTING FORCES YOU TO THINK
- TESTS REDUCE FEAR
The goal of unit testing is:
- to isolate each part of the program, and
- show that the individual parts are correct
Types of Software Testing
The Concept of Unit Testing
- A unit test is code written by a developer that tests as small a piece of functionality (the unit) as possible.
- One function may have multiple unit tests according to the usage and outputs of the function.
- Tests ensure
- The code meets expectations and specifications: Does what it says it should do.
- The code continues to meet expectations over time: Avoiding regression.
Structure of A Unit Test
- Setup
- Prepare an input
- Call a method
- Check an output
- Tear down
Unit Testing Tools
- Testing Frameworks
- BDD vs TDD
- Mocking Frameworks
- Assertion Libraies
- Test Runner
- Automation
Unit Test Should Not:
- Access the network
- Hit a database
- Use the file system
- Call other non-trivial components
Unit Testing with Mocks
- Every App has Layers such Front end, business layer, Data Access Layer, Data Layer
- Unit Testing tests one layer
- A Mock allows a dependency to be imitated so the Unit test can be isolated.
Executing Tests:
- Manulally
- i want to run only this test, so open terminal run your test runner and provide him with your test suite to run for you
- Automatically
- Build server compiles and runs tests as part of nightly build operation. (travis-ci get updated with new push to repo run test against these new changes of code, and notify with errors or passing of pushed code)
Unit Test Best Practices
- Consistent
- Atomic - not depend on any thing - MOCK
- Single Responsibility - Test behavior, not methods -
- No conditional logic or loops
- No exception handling
- Self-descriptive - Informative Assertion messages - By reading the assertion message, one should know why the test failed and what to do -
FIX the build