
PHP ์จ๋ผ์ธ ๊ฐ์
>PHP - ์ต์๊ธ
๐ PHP ์ต์๊ธ - 6์ฃผ์ฐจ: ํ ์คํธ ์๋ํ (๋จ์/ํตํฉ/E2E ํ ์คํธ)- 01 PHPUnit์ ์ด์ฉํ ๋จ์ ํ ์คํธ

์ฃผ์ ํ๋ก๊ทธ๋จ ์คํ
- ํ์ : 10.0
- ๋ผ์ด์ ์ค: free
- ์ด์์ฒด์ :
- ํ์ผ ํฌ๊ธฐ: 0
ํผ๋๋ฐฑ ๋ฐ ๋ค์ด๋ก๋
- ์ฌ์ฉ์ ํ์ : 10.0
- ๋ค์ด๋ก๋ ์: 0
- ์กฐํ์: 2
์ ์กฐ์ฌ ๋ฐ ๋ฑ๋ก ์ ๋ณด
- ์ ์์ฌ: LUZENSOFT
- ๋ฑ๋ก์ผ: 2025-10-19 13:01:17
- ์ค๋ช
๋จ์ ํ ์คํธ์ ์ค์์ฑ ๋ฐ PHPUnit ์๊ฐ
#์ํํธ์จ์ด #๊ฐ๋ฐ์์ #ํ ์คํธ(#Test)๋ ์ฝ๋์ #ํ์ง๊ณผ #์์ ์ฑ(#Stability)์ ๋ณด์ฅํ๋ ํต์ฌ ํ๋์ด์์. ํนํ, #๋จ์ #ํ ์คํธ(#Unit_Test)๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ์ฅ ์์ ๋จ์(์ฃผ๋ก #ํด๋์ค ๋๋ #๋ฉ์๋)๊ฐ ์์๋๋ก ์ ํํ ๋์ํ๋์ง ๊ฒ์ฆํ๋ ๊ณผ์ ์ด์์. ์ด๋ #๋ฒ๊ทธ๋ฅผ ์ฌ์ ์ ๋ฐ๊ฒฌํ๊ณ , #๋ฆฌํฉํ ๋ง(#Refactoring) ์ ๊ธฐ์กด ๊ธฐ๋ฅ์ด ์์๋์ง ์๋๋ก ๋ณดํธํ๋ ์ญํ ์ ํด์.
#PHP ํ๊ฒฝ์์๋ #PHPUnit์ด ์ฌ์ค์์ #ํ์ค #ํ ์คํ #ํ๋ ์์ํฌ(#Testing_Framework)๋ก ์ฌ์ฉ๋๊ณ ์์ด์. #Java์ #JUnit ๊ณ์ด์ ๋ฐ๋ฅด๋ฉฐ, #๊ฐ๋ฐ์๊ฐ #ํ ์คํธ๋ฅผ #์๋ํํ๊ณ #๊ฒฐ๊ณผ๋ฅผ ์ฝ๊ฒ ํ์ธํ ์ ์๋๋ก ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํด์.
PHPUnit ์ค์น ๋ฐ ๊ธฐ๋ณธ ์ค์
#PHPUnit์ ์ผ๋ฐ์ ์ผ๋ก #Composer๋ฅผ ํตํด ํ๋ก์ ํธ์ ์ถ๊ฐํด์.
1. Composer๋ฅผ ์ด์ฉํ ์ค์น
Bash
composer require --dev phpunit/phpunit
--dev
์ต์ ์ #PHPUnit์ด #๊ฐ๋ฐ(#Development) ํ๊ฒฝ์์๋ง ํ์ํ๋ฉฐ #์ด์(#Production) ํ๊ฒฝ์๋ ํฌํจ๋์ง ์๋๋ก ์ง์ ํด์.
2. ์ค์ ํ์ผ (phpunit.xml)
ํ๋ก์ ํธ ๋ฃจํธ ๋๋ ํ ๋ฆฌ์ phpunit.xml
ํ์ผ์ ์์ฑํ์ฌ #ํ
์คํธ์ #๊ฒฝ๋ก, #๋ถํธ์คํธ๋ฉ ํ์ผ, #์ฝ๋ #์ปค๋ฒ๋ฆฌ์ง ์ค์ ๋ฑ์ ์ง์ ํ ์ ์์ด์.
XML
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Application">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
๋จ์ ํ ์คํธ ํด๋์ค ์์ฑ ์์น ๋ฐ ๊ตฌ์กฐ
#PHPUnit์ ์ฌ์ฉํ์ฌ ํ ์คํธ๋ฅผ ์์ฑํ ๋๋ ํน์ #๊ท์น์ ๋ฐ๋ผ์ผ ํด์.
1. ํ ์คํธ ํด๋์ค ๊ตฌ์กฐ
์์: ๋ชจ๋ #ํ ์คํธ #ํด๋์ค๋
PHPUnitFrameworkTestCase
๋ฅผ #์์๋ฐ์์ผ ํด์.ํ์ผ/ํด๋์ค ๋ช ๋ช : ํ ์คํธ ๋์ ํด๋์ค ์ด๋ฆ ๋ค์
Test
๋ฅผ ๋ถ์ด๊ณ ,tests/
๋๋ ํ ๋ฆฌ์ ์ ์ฅํ๋ ๊ฒ์ด ๊ด๋ก์์ (์:Calculator
ํด๋์ค ->CalculatorTest.php
).ํ ์คํธ ๋ฉ์๋: ํ ์คํธ ๋์ ๋ฉ์๋ ์ด๋ฆ ์์
test
๋ฅผ ๋ถ์ด๊ฑฐ๋,#@test
#์ด๋ ธํ ์ด์ (#Annotation)์ ๋ถ์ฌ์ผ #PHPUnit์ด ์ด๋ฅผ #ํ ์คํธ๋ก ์ธ์ํด์.
2. AAA ํจํด (Arrange-Act-Assert)
์ข์ #๋จ์ #ํ ์คํธ๋ ์ผ๋ฐ์ ์ผ๋ก AAA ํจํด์ ๋ฐ๋ผ ์ธ ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ๋ผ์.
Arrange (์ค๋น): ํ ์คํธ์ ํ์ํ ์ด๊ธฐ #์ํ(#State), #๊ฐ์ฒด, #์ ๋ ฅ #๋ฐ์ดํฐ๋ฅผ ์ค์ ํด์.
Act (์คํ): ํ ์คํธ ๋์ ์ฝ๋(๋ฉ์๋)๋ฅผ ์คํํด์.
Assert (๋จ์ธ/๊ฒ์ฆ): ์คํ ๊ฒฐ๊ณผ๊ฐ ์์๊ณผ ์ผ์นํ๋์ง #PHPUnit์ #๋จ์ธ(#Assertion) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์ฆํด์.
3. ๋จ์ธ(Assertion) ๋ฉ์๋ ์์
#PHPUnit์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆํ๊ธฐ ์ํ ๋ค์ํ #๋จ์ธ #๋ฉ์๋๋ฅผ ์ ๊ณตํด์.
๋จ์ธ ๋ฉ์๋ | ์ค๋ช |
---|---|
| ๋ ๊ฐ์ด ๊ฐ์์ง ๊ฒ์ฆํด์. |
| ์กฐ๊ฑด์ด |
| ๋ฐฐ์ด์ #์์ #๊ฐ์๋ฅผ ๊ฒ์ฆํด์. |
| ๊ฐ์ฒด๊ฐ ํน์ #ํด๋์ค์ #์ธ์คํด์ค์ธ์ง ๊ฒ์ฆํด์. |
| ๋ณ์๊ฐ |
4. ์ฝ๋ ์์
์๋ฅผ ๋ค์ด, ๊ฐ๋จํ ๋ง์
๊ธฐ๋ฅ์ ๊ฐ์ง Calculator
ํด๋์ค์ ๋ํ ํ
์คํธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํด์.
src/Calculator.php
PHP
<?php
class Calculator {
public function add(int $a, int $b): int {
return $a + $b;
}
}
tests/CalculatorTest.php
PHP
<?php
use PHPUnitFrameworkTestCase;
class CalculatorTest extends TestCase {
public function testAdditionOfTwoNumbers(): void {
// Arrange (์ค๋น)
$calculator = new Calculator();
$expected = 5;
// Act (์คํ)
$result = $calculator->add(2, 3);
// Assert (๊ฒ์ฆ)
$this->assertEquals($expected, $result, "2 + 3์ 5์ฌ์ผ ํฉ๋๋ค.");
}
public function testNegativeNumberAddition(): void {
// Arrange
$calculator = new Calculator();
// Act & Assert (๋จ์ํ)
$this->assertEquals(0, $calculator->add(-5, 5));
}
}
ํ ์คํธ ์คํ ๋ฐ ์ถ๊ฐ ๊ธฐ๋ฅ
1. ํ ์คํธ ์คํ
ํฐ๋ฏธ๋์์ #PHPUnit #์คํ ํ์ผ์ ํธ์ถํ์ฌ ํ ์คํธ๋ฅผ ์คํํด์.
Bash
# Composer ์ค์น ์
./vendor/bin/phpunit
# ํน์ ํ
์คํธ ํ์ผ๋ง ์คํ
./vendor/bin/phpunit tests/CalculatorTest.php
2. Mock ๊ฐ์ฒด (Mock Object)
#๋จ์ #ํ ์คํธ๋ ํ ์คํธ ๋์ ๋จ์ ์ธ์ **์ธ๋ถ #์์กด์ฑ(#Dependency)**์ผ๋ก๋ถํฐ #๋ ๋ฆฝ์ ์ด์ด์ผ ํด์. ์๋ฅผ ๋ค์ด, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ #์ธ๋ถ #API ํธ์ถ์ด ํ์ํ ๊ฒฝ์ฐ, ์ค์ ์์กด์ฑ ๋์ #Mock #๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ #ํ๊ฒฝ์ #๊ฒฉ๋ฆฌํด์.
#Mock ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ ๋ฉ์๋๊ฐ ํธ์ถ๋์๋์ง, ์ด๋ค #์ธ์(#Argument)๋ก ํธ์ถ๋์๋์ง ๋ฑ์ ํ์ธํ๊ณ , ์ ํด์ง #๊ฒฐ๊ณผ๊ฐ์ #๋ฐํํ๋๋ก ์ค์ ํ ์ ์์ด์.
3. ๋ฐ์ดํฐ ์ ๊ณต์ (Data Provider)
๋์ผํ #ํ ์คํธ #๋ก์ง์ ์ฌ๋ฌ #์ ๋ ฅ #๋ฐ์ดํฐ #์ธํธ๋ก ๋ฐ๋ณต ์คํํด์ผ ํ ๋ #๋ฐ์ดํฐ #์ ๊ณต์(#Data_Provider) ๊ธฐ๋ฅ์ ์ฌ์ฉํด์. ์ด๋ #ํ ์คํธ #์ฝ๋์ #์ค๋ณต์ ์ค์ด๊ณ ํ ์คํธ ์ผ์ด์ค๋ฅผ ๋ช ํํ๊ฒ ๊ด๋ฆฌํ ์ ์๊ฒ ํด์ค์.
PHP
/**
* @dataProvider additionProvider
*/
public function testAdd(int $a, int $b, int $expected): void {
$calculator = new Calculator();
$this->assertEquals($expected, $calculator->add($a, $b));
}
public function additionProvider(): array {
return [
[0, 0, 0],
[1, 1, 2],
[10, 5, 15],
[-1, 1, 0],
];
}
#PHPUnit์ #ํ ์คํธ #์๋ํ์ ๊ธฐ๋ณธ์ด๋ฉฐ, ๊ฒฌ๊ณ ํ #PHP #์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ํ์์ ์ธ ๋๊ตฌ์์. ์ด๋ฅผ ํตํด ๊ฐ๋ฐ์๋ ์ฝ๋ ๋ณ๊ฒฝ์ ๋ํ ํ์ ์ ๊ฐ์ง๊ณ ๊ฐ๋ฐํ ์ ์์ด์.
๊ธฐ๊ฐ,์๋๋ณ ์ถ๊ฐ50%ํ ์ธ, KT์ด๊ณ ์IP, ๋ธ๋ก๊ทธ,์ง์์ธ,์ผํ,์ง๋ ๋ง์ผํ ๊ฐ๋ฅ
https://xn--299ao67b9qbmsf04c.net/