𝗪𝗵𝗮𝘁 𝗧𝗲𝘀𝘁 𝗚𝗼𝗲𝘀 𝗪𝗵𝗲𝗿𝗲? 𝗔 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗚𝘂𝗶𝗱𝗲 𝘁𝗼 𝗧𝗲𝘀𝘁 𝗟𝗮𝘆𝗲𝗿 𝗗𝗲𝗰𝗶𝘀𝗶𝗼𝗻𝘀
Every test answers a question. Teams often ask the right question at the wrong layer.
A unit test proving a full checkout flow is too high-level. A BDD scenario proving a regex pattern is too low-level.
This makes test suites slow and fragile. You add more tests to fix it. The suite slows down more. Confidence stays low.
Do not maximize the number of tests. Put each test where it gives the most confidence for the least cost.
Use these four layers:
- Unit tests: Check logic in isolation.
- Integration tests: Check component connections.
- E2E tests: Check user workflows.
- BDD scenarios: Check team agreements.
Choose the cheapest layer to answer your question.
- Use unit tests for calculations and validation rules.
- Use integration tests for API calls and database queries.
- Use E2E tests for critical paths like sign-up or payment.
- Use BDD for business rules stakeholders must review.
Avoid these mistakes:
- Low-level checks in E2E tests.
- Mocking everything in unit tests.
- Technical details in BDD.
A healthy suite does not repeat the same check three times. It places each assertion where it gives the most signal.
Write tests where they earn their cost.
Source: https://dev.to/krinosystems/what-test-goes-where-a-practical-guide-to-test-layer-decisions-5dk8