UNIT TESTING
What is Unit Testing?
defined as a type of software testing where individual units/ components of a software are tested.
Why is it Important?
- Unit Tests fix bug early in
development cycle and save costs. - It helps
understand the developers the code base and enable them to make changes quickly. - Good unit tests serve as project documentation
- Unit tests help with code re-use. Migrate both your code and your tests to your new project. Tweak the code till the tests run again.
MYTH:
It requires time, and I am always overscheduled
My code is rock solid! I do not need unit tests.
UNIT TEST != INTEGRATION TEST
UNIT TEST is easy to implement
Covers a single piece of code (usually an object or a function) tested the behavior without external resources
INTEGRATION TEST is complex to implement
demonstrate that different parts of a system work together in the real-life environment. They validate complex scenarios (we can think of integration tests as a user performing some high-level operation within our system), and usually require external resources, like databases or web servers, to be present

The kind of process testing
UNIT TEST PATTERN
The AAA Phrases
Arrange, Act, Assert
TDD and BDD
TDD
Test Driven Development
RULES:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test
than is sufficient to fail, and compilation failures are failures. - You are not allowed to write any more production code than is sufficient to pass the one failing
unit test.

BDD
Behavior Driven Development
TDD == BDD
Testing Framework:
I picked JEST:
- built-in assertion method
- multi-environment, it can use in node and browser
- can watch and run untested code or code changes only
- built-in coverage
Example:
-
Terdapat dua array A[] dan B[] berisi angka integer
-
Buatlah fungsi untuk melakukan check apakah array B adalah subarray dari array A!
Problems:
Write the test first
describe('#subArrayOf, when passed "A = [1, 2, 0, 5, 8, 1, 3] and B = [3, 0, 5, 1]"', () => {
it('should returns true', () => {
// Arrange
// Act
// Assert
})
})
DEMO!
INTEGRATION TEST
We need to mock our data
Mocking framework:
- Supertest
- Pretender
- Jest Mock (usually just mock function call)
- Sinon
- Test Double
- Enzyme (for react)
- React Test Renderer
- Any suggestion
Misc:
Code Coverage:
- Test must cover many code that we wrote
- Higher point is better
Framework:
Misc:
The end
Unit Testing
By ikhsanalatsary
Unit Testing
- 65