Unit testing
Why do we need tests?
- Helps to design
- Simplifies refactoring
- Documents code behavior
- Helps to keep us sane
Types of tests
Unit tests – testing individual parts of functionality by providing input data for every condition
What should be tested?
- Business logic
- Single component
- Everything that contains conditions
What should not be tested?
- Frameworks and libraries
- Databases and network requests
- Trivial code (getter, setter)
- Code that has not determined results
Types of tests
Integration tests – testing several modules together to make sure those interact as expected
Functional tests – tests whether or not the flow of an application is performing as designed from start to finish
TDD and BDD
Test Driven Development
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved so that the tests pass.
TDD Cycle
Behavior-driven development
In software engineering, behavior-driven development (BDD) is an agile software development process that encourages collaboration among developers, quality assurance testers, and customer representatives in a software project. It encourages teams to use conversation and concrete examples to formalize a shared understanding of how the application should behave.
TDD
BDD
Focus
Reader/Writer
Specificity
Speed
Coverage
Maintainability
Portability
code quality
value
writers: developers, readers: developers, testers
writer: product owner, reader: developers, testers, stakeholders, business owner, product owner
high specificity
when test fails, it unclear what exactly went wrong
fast
slow
100% code coverage is rare/difficult to achieve with Unit Test
100% code coverage is no uncommon
changes to the functionality changes to Unit Tests
change infrequently
highly specific to the code
not coupled with code
In BDD you will come across a better specification since communication between the software developer and the product owner is fast and easy.
TDD may lack the ability to specify the exact behavior and makes a process of functional code changes more complex, but you achieve higher quality with software code.
JEST
JEST
Matchers
- .toBe(value)
- .toEqual(value)
- .toBeNull(value)
- .toBeUndefined(value)
- .toBeDefined (value)
- .toBeNaN(value)
- .toBeTruthy(value)
- .toBeFalsy(value)
- .toBeGreaterThan(value)
- .toBeGreaterThanOrEqual(value)
- .toBeLessThan(value)
- .toBeLessThanOrEqual(value)
- .toBeCloseTo(value)
- .toMatch(value)
- .toContain(value)
Setup
- .beforeEach()
- .afterEach()
- .beforeAll()
- .afterAll()
Thank you!
Unit testing
By Pavel Razuvalau
Unit testing
- 399