16. Testing & Quality
Why is it important to write tests?
JuniorAnswer: Tests ensure that the code works as expected, prevent regressions (bugs introduced after changes), document the behavior of the code, and make refactoring safer. They also help in designing better architecture by encouraging modularity.
What is a Unit Tests?
JuniorAnswer: A type of software testing where individual units or components of a software are tested in isolation. In PHP, PHPUnit is the standard tool.
What kind of Unit Tests frameworks did you use?
JuniorAnswer: The primary framework for PHP is PHPUnit. I've also used Pest, which is built on top of PHPUnit and offers a more expressive, functional syntax.
What is static analyzer tools?
JuniorAnswer: Static analysis tools analyze code without executing it. They help in finding potential bugs, security vulnerabilities, code style violations, and other issues early in the development process.
How the Tests Coverage is measured?
JuniorAnswer: Test coverage (or Code Coverage) is a metric that measures the percentage of code lines executed by your test suite. It is measured by tracking which parts of the code are hit during test execution, typically using tools like Xdebug or PCOV.
What is Code Coverage?
JuniorAnswer: A metric that measures the percentage of code lines executed by your test suite.
What is Integration Testing?
JuniorAnswer: Testing how different modules or components of your application work together as a group.
What static analyzer tools did you use?
MiddleAnswer: I have used PHPStan for finding bugs and improving code quality, Psalm for advanced static analysis (especially for large codebases), and PHP_CodeSniffer (PHPCS) for maintaining code style consistency.
What is the difference between unit, integration and feature tests?
MiddleAnswer:
- Unit tests: Test isolated components (e.g., a single method) in isolation from dependencies (often using mocks).
- Integration tests: Test interaction between multiple components (e.g., how a service interacts with a repository or a database).
- Feature tests: Test a full user story or an API endpoint from start to finish, often involving multiple layers of the application.
What method of unit tests cover measure did you use?
MiddleAnswer: Commonly used methods include Line Coverage (percentage of executed lines), Branch Coverage (checking all paths through conditions), and Path Coverage. PHPUnit usually reports line coverage using drivers like Xdebug or PCOV.
What is Mocking?
MiddleAnswer: Creating a fake version of an object that simulates the behavior of the real object, used to isolate the unit of code being tested.
TDD vs BDD
MiddleAnswer:
- TDD (Test-Driven Development): Writing tests before writing the actual code. Focuses on implementation.
- BDD (Behavior-Driven Development): Writing tests based on expected behavior and user stories (e.g., Behat). Focuses on communication.
What is Mutation Testing?
SeniorAnswer: A type of testing where small changes (mutations) are introduced into the code to check if the existing tests fail. This measures the effectiveness of the tests.