BDD là extension của TDD, focus vào behavior từ user/business perspective thay vì technical implementation.
- Given-When-Then syntax: Given (context/preconditions — 'Given a user with $100 in account'), When (action — 'When they transfer $50'), Then (expected outcome — 'Then the balance should be $50 and recipient has $50').
- Gherkin language: human-readable scenarios viết trong .feature files — Given/When/Then/And/But; được Cucumber parse và map đến step definitions trong code.
- Lợi ích: common language giữa developers, QA, và business stakeholders; scenarios serve as living documentation; non-technical people có thể review và write scenarios.
- Trong Jest/Vitest: BDD style với describe/it — it('should deduct amount from sender balance when transfer succeeds') đọc như specification.
- Cucumber.js: feature files → step definitions (JS functions) → actual test code; overkill cho most projects trừ khi business stakeholders thực sự write scenarios.
- Practical BDD: dùng Given-When-Then naming convention trong unit tests mà không cần Cucumber — captures intent, readable as docs.
- Lưu ý: BDD không phải chỉ về syntax — phải thực sự involve business trong defining behaviors, không chỉ wrap existing tests với Given/When/Then.
BDD is an extension of TDD that focuses on behavior from the user/business perspective rather than technical implementation.
- Given-When-Then syntax: Given (context/preconditions — 'Given a user with $100 in their account'), When (action — 'When they transfer $50'), Then (expected outcome — 'Then the balance should be $50 and the recipient has $50').
- Gherkin language: human-readable scenarios written in .feature files — Given/When/Then/And/But; parsed by Cucumber and mapped to step definitions in code.
- Benefits: a common language between developers, QA, and business stakeholders; scenarios serve as living documentation; non-technical people can review and write scenarios.
- In Jest/Vitest: BDD style with describe/it — it('should deduct the amount from the sender balance when the transfer succeeds') reads like a specification.
- Cucumber.js: feature files → step definitions (JS functions) → actual test code; overkill for most projects unless business stakeholders actually write scenarios.
- Practical BDD: use the Given-When-Then naming convention in unit tests without Cucumber — captures intent, readable as documentation.
Pitfall: BDD is not just about syntax — it must genuinely involve the business in defining behaviors, not just wrap existing tests with Given/When/Then.