# TDD(Test Driven Development)



# TDD : ํ…Œ์ŠคํŠธ ์ฃผ๋„ ๊ฐœ๋ฐœ

'ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐœ๋ฐœ์„ ์ด๋Œ์–ด ๋‚˜๊ฐ„๋‹ค.'

์šฐ๋ฆฌ๋Š” ๋ณดํ†ต ๊ฐœ๋ฐœํ•  ๋•Œ, ์„ค๊ณ„(๋””์ž์ธ)๋ฅผ ํ•œ ์ดํ›„ ์ฝ”๋“œ ๊ฐœ๋ฐœ๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋œ๋‹ค.



ํ•˜์ง€๋งŒ TDD๋Š” ๊ธฐ์กด ๋ฐฉ๋ฒ•๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•œ ์ดํ›„์— ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฆฌํŒฉํ† ๋ง ์ ˆ์ฐจ๋ฅผ ๋ฐŸ๋Š”๋‹ค.



์ž‘๊ฐ€๊ฐ€ ์ฑ…์„ ์“ฐ๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์ƒ๊ฐํ•ด๋ณด์ž.

์ฑ…์„ ์“ฐ๊ธฐ ์ „, ๋ชฉ์ฐจ๋ฅผ ๋จผ์ € ๊ตฌ์„ฑํ•œ๋‹ค.
์ดํ›„ ๋ชฉ์ฐจ์— ๋งž๋Š” ๋‚ด์šฉ์„ ๋จผ์ € ๊ตฌ์ƒํ•œ ๋’ค, ์ดˆ์•ˆ์„ ์ž‘์„ฑํ•˜๊ณ  ๊ณ ์ณ์“ฐ๊ธฐ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

๋ชฉ์ฐจ ๊ตฌ์„ฑ : ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ
์ดˆ์•ˆ ์ž‘์„ฑ : ์ฝ”๋“œ ๊ฐœ๋ฐœ
๊ณ ์ณ ์“ฐ๊ธฐ : ์ฝ”๋“œ ์ˆ˜์ •(๋ฆฌํŒฉํ† ๋ง)

๋ฐ˜๋ณต์ ์ธ '๊ฒ€ํ† '์™€ '๊ณ ์ณ์“ฐ๊ธฐ'๋ฅผ ํ†ตํ•ด ์ข‹์€ ๊ธ€์ด ์™„์„ฑ๋œ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ์†Œํ”„ํŠธ์›จ์–ด์— ์ ์šฉํ•œ ๊ฒƒ์ด TDD!

์†Œํ”„ํŠธ์›จ์–ด ๋˜ํ•œ ๋ฐ˜๋ณต์ ์ธ ํ…Œ์ŠคํŠธ์™€ ์ˆ˜์ •์„ ํ†ตํ•ด ๊ณ ํ’ˆ์งˆ์˜ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ํƒ„์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

# ์žฅ์ 

์ž‘์—…๊ณผ ๋™์‹œ์— ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์˜ค๋ฅ˜ ํŒŒ์•…์ด ๊ฐ€๋Šฅํ•จ ( ์‹œ์Šคํ…œ ๊ฒฐํ•จ ๋ฐฉ์ง€ )

์งง์€ ๊ฐœ๋ฐœ ์ฃผ๊ธฐ๋ฅผ ํ†ตํ•ด ๊ณ ๊ฐ์˜ ์š”๊ตฌ์‚ฌํ•ญ ๋น ๋ฅด๊ฒŒ ์ˆ˜์šฉ ๊ฐ€๋Šฅ. ํ”ผ๋“œ๋ฐฑ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ์ง„ํ–‰ ์ƒํ™ฉ ํŒŒ์•…์ด ์‰ฌ์›€

์ž๋™ํ™” ๋„๊ตฌ๋ฅผ ์ด์šฉํ•œ TDD ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•จ

(์ž๋ฐ”๋Š” JUnit, C์™€ C++์€ CppUnit ๋“ฑ)

๊ฐœ๋ฐœ์ž๊ฐ€ ๊ธฐ๋Œ€ํ•˜๋Š” ์•ฑ์˜ ๋™์ž‘์— ๊ด€ํ•œ ๋ฌธ์„œ๋ฅผ ํ…Œ์ŠคํŠธ๊ฐ€ ์ œ๊ณตํ•ด์คŒ <br>
`๋˜ํ•œ ์ด ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์—…๋ฐ์ดํŠธ ๋˜๋ฏ€๋กœ ๋ฌธ์„œ ์ž‘์„ฑ๊ณผ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋งค์šฐ ์ข‹์Œ`

# ๋‹จ์ 

๊ธฐ์กด ๊ฐœ๋ฐœ ํ”„๋กœ์„ธ์Šค์— ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์„ค๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜๋ฏ€๋กœ ์ƒ์‚ฐ ๋น„์šฉ ์ฆ๊ฐ€

ํ…Œ์ŠคํŠธ์˜ ๋ฐฉํ–ฅ์„ฑ, ํ”„๋กœ์ ํŠธ ์„ฑ๊ฒฉ์— ๋”ฐ๋ฅธ ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ ์„ ํƒ ๋“ฑ ์ถ”๊ฐ€๋กœ ๊ณ ๋ คํ•  ๋ถ€๋ถ„์˜ ์ฆ๊ฐ€



# ์ ์ˆ˜ ๊ณ„์‚ฐ ํ”„๋กœ๊ทธ๋žจ์„ ํ†ตํ•œ TDD ์˜ˆ์ œ ์ง„ํ–‰


์ค‘๊ฐ„๊ณ ์‚ฌ, ๊ธฐ๋ง๊ณ ์‚ฌ, ๊ณผ์ œ ์ ์ˆ˜๋ฅผ ํ†ตํ•œ ์„ฑ์ ์„ ๋‚ด๋Š” ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์ž

์ ์ˆ˜ ์ดํ•ฉ 90์  ์ด์ƒ์€ A, 80์  ์ด์ƒ์€ B, 70์  ์ด์ƒ์€ C, 60์  ์ด์ƒ์€ D, ๋‚˜๋จธ์ง€๋Š” F๋‹ค.


TDD ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•œ๋‹ค.

35 + 25 + 25 = 85์ ์ด๋ฏ€๋กœ ๋“ฑ๊ธ‰์ด B๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ assertEquals์˜ ์ธ์ž๊ฐ’์„ "B"๋กœ ์ฃผ๊ณ , ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด๋ณด์ž


public class GradeTest {
    
    @Test
    public void scoreResult() {
        
        Score score = new Score(35, 25, 25); // Score ํด๋ž˜์Šค ์ƒ์„ฑ
        SimpleScoreStrategy scores = new SimpleScoreStrategy();
        
        String resultGrade = scores.computeGrade(score); // ์ ์ˆ˜ ๊ณ„์‚ฐ
        
        assertEquals("B", resultGrade); // ํ™•์ธ
    }
    
}


ํ˜„์žฌ๋Š” Score ํด๋ž˜์Šค์™€ computeGrade() ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์ง€ ์•Š์€ ์ƒํƒœ๋‹ค. (ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ๋งŒ ์กด์žฌ)

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์— ๋งž์ถฐ์„œ ์ฝ”๋“œ ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜์ž



์šฐ์„  ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  Score ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค


public class Score {
    
    private int middleScore = 0;
    private int finalScore = 0;
    private int homeworkScore = 0;
    
    public Score(int middleScore, int finalScore, int homeworkScore) {
        this.middleScore = middleScore;
        this.finalScore = finalScore;
        this.homeworkScore = homeworkScore;
    }
    
    public int getMiddleScore(){
        return middleScore;
    }
    
    public int getFinalScore(){
        return finalScore;
    }
    
    public int getHomeworkScore(){
        return homeworkScore;
    }
    
}


์ด์ œ ์ ์ˆ˜ ๊ณ„์‚ฐ์„ ํ†ตํ•ด ์„ฑ์ ์„ ๋ฟŒ๋ ค์ค„ computeGrade() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.


์šฐ์„  ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ž


public interface ScoreStrategy {
    
    public String computeGrade(Score score);
    
}

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ ธ์™€ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค


public class SimpleScoreStrategy implements ScoreStrategy {
    
    public String computeGrade(Score score) {
        
        int totalScore = score.getMiddleScore() + score.getFinalScore() + score.getHomeworkScore(); // ์ ์ˆ˜ ์ดํ•ฉ
        
        String gradeResult = null; // ํ•™์  ์ €์žฅํ•  String ๋ณ€์ˆ˜
        
        if(totalScore >= 90) {
            gradeResult = "A";
        } else if(totalScore >= 80) {
            gradeResult = "B";
        } else if(totalScore >= 70) {
            gradeResult = "C";
        } else if(totalScore >= 60) {
            gradeResult = "D";
        } else {
            gradeResult = "F";
        }
        
        return gradeResult;
    }
    
}


์ด์ œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€์„œ, ์‹ค์ œ๋กœ ํ†ต๊ณผํ•  ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด๋ณธ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด์ž

์ด๋•Œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ, ์ค‘๋ณต ์ œ๊ฑฐ, ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ํ†ตํ•œ ๋ฆฌํŒฉํ† ๋ง ์ž‘์—…์„ ํ†ตํ•ด ์™„์„ฑ๋„ ๋†’์€ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•˜์ž!


ํ†ต๊ณผ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ •๋ณด๋ฅผ ๋„ฃ๊ณ  ์‹คํ–‰ํ•˜๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ์—๋Ÿฌ ์—†์ด ์ œ๋Œ€๋กœ ์‹คํ–‰๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.





๊ตณ์ด ํ•„์š”ํ•˜๋‚˜์š”?

๋”ฑ๋ด๋„ ๊ท€์ฐฎ์•„ ๋ณด์ธ๋‹ค. ์ €๋ ‡๊ฒŒ ํ™•์ธ ์•ˆํ•ด๋„ ๊ฒฐ๊ณผ๋ฌผ์„ ์•Œ ์ˆ˜ ์žˆ์ง€ ์•Š๋ƒ๊ณ  ๋ฐ˜๋ฌธํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์˜ˆ์‹œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด์˜€์„ ๋ฟ, ์‹ค์ œ ์‹ค๋ฌด ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ์ด ํ•„์š”ํ•˜๊ณ , ์›ํ•˜๋Š” ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์€ ํ•„์ˆ˜์ ์ธ ๋ถ€๋ถ„์ด๋‹ค.

TDD๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ๊ณ„์—์„œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์„ค๊ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์ดˆ๊ธฐ ๋น„์šฉ์ด ํ™•์‹คํžˆ ๋” ๋“ค๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐœ๋ฐœ ๊ณผ์ •์— ์žˆ์–ด์„œ '์ดˆ๊ธฐ ๋น„์šฉ'๋ณด๋‹ค '์œ ์ง€๋ณด์ˆ˜ ๋น„์šฉ'์ด ๋” ํด ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜์ž

๋˜ํ•œ ์•ˆ์ „์„ฑ์ด ํ•„์š”ํ•œ ์†Œํ”„ํŠธ์›จ์–ด ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ฐœ๋ฐœ ์ดˆ๊ธฐ ๋‹จ๊ณ„๋ถ€ํ„ฐ ํ™•์‹คํ•˜๊ฒŒ ๋‹ค์ ธ๋†“๊ณ  ๊ฐ€๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

์œ ์ง€๋ณด์ˆ˜ ๋น„์šฉ์ด ๋” ํฌ๊ฑฐ๋‚˜ ๋น„ํ–‰๊ธฐ, ๊ธฐ์ฐจ์— ํ•„์š”ํ•œ ์†Œํ”„ํŠธ์›จ์–ด ๋“ฑ ์•ˆ์ „์„ฑ์ด ์ค‘์š”ํ•œ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ ํ˜„์žฌ ์‹ค๋ฌด์—์„œ๋„ TDD๋ฅผ ํ™œ์šฉํ•œ ๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ๋‹ค.

์ตœ์ข… ์ˆ˜์ • : 12/17/2022, 7:23:59 AM