Junior — Middle
What are the main operations to perform when writing unit tests?
sobes.tech AI
Answer from AI
The main operations when writing unit tests in PHP include:
- Preparing test data — creating necessary objects and values for the test.
- Calling the tested method or function with the prepared data.
- Checking the result — comparing the actual result with the expected one using assertions.
- Cleanup — if necessary, releasing resources or resetting state.
Example using PHPUnit:
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase {
public function testAdd() {
$calculator = new Calculator();
$result = $calculator->add(2, 3);
$this->assertEquals(5, $result);
}
}