Skip to content

Latest commit

Β 

History

History
121 lines (86 loc) Β· 4.89 KB

2022-03-10-test-driven-development-2.md

File metadata and controls

121 lines (86 loc) Β· 4.89 KB

Test Driven Development-2

λ³΄ν†΅μ˜ TDD Cycle

  1. ν…ŒμŠ€νŠΈ μž‘μ„± (Write a test)

    μƒκ°ν•˜κ³  μžˆλŠ” 연산을 μ–΄λ–»κ²Œ μ½”λ“œλ‘œ μž‘μ„±ν•  지 κ³ λ―Όν•΄λ³Έλ‹€.
    ν˜„μž¬ 이야기λ₯Ό μ“°κ³  μžˆλ‹€κ³  μƒκ°ν•˜κ³ , μž‘μ„±ν•  μ½”λ“œκ°€ 가지고 μžˆμ—ˆμœΌλ©΄ ν•˜λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ””μžμΈν•œλ‹€. μ •ν™•ν•œ 연산을 μœ„ν•΄ ν•„μš”ν•œ λͺ¨λ“  것듀을 μƒμƒν•˜κ³  이야기에 ν¬ν•¨μ‹œν‚¨λ‹€.

  2. μž‘μ„±λœ ν…ŒμŠ€νŠΈ λŒμ•„κ°€κ²Œ ν•˜κΈ° (Make it run)

    λΉ λ₯΄κ²Œ ν…ŒμŠ€νŠΈλ₯Ό λŒμ•„κ°€κ²Œ ν•˜λŠ” 것(getting bar to go to green) 이 λ‹€λ₯Έ μš”μ†Œλ“€λ„ μ’Œμ§€μš°μ§€ν•œλ‹€.
    λ§Œμ•½ ν•΄κ²° 방법이 κ°„λ‹¨ν•˜κ³  λͺ…ν™•ν•˜λ‹€λ©΄, ν…ŒμŠ€νŠΈλ₯Ό μž‘μ„±ν•΄λΌ. ν•΄κ²° 방법이 κ°„λ‹¨ν•˜κ³  λͺ…ν™•ν•˜μ§€λ§Œ μ‹œκ°„μ΄ 걸릴 것 κ°™λ‹€λ©΄, 적어 놓고 μ€‘μš”ν•œ 문제둜 λŒμ•„κ°„λ‹€.
    이 μ›λ¦¬λŠ” κ²½ν—˜ μžˆλŠ” κ°œλ°œμžλ“€μ—κ²Œλ„ μ–΄λ €μšΈ 수 μžˆλ‹€. 그듀은 쒋은 개발 기술(Good engineering)λ“€μ˜ κ·œμΉ™λ“€λ§Œ μ•Œκ³  μžˆλ‹€.
    λΉ λ₯΄κ²Œ ν…ŒμŠ€νŠΈλ₯Ό λŒμ•„κ°€κ²Œ ν•˜λŠ” 것은 λͺ¨λ“  찝찝함(sins) 을 λ’€λ‘œ 미뀄둔닀. μ•„μ£Ό μž κΉλ™μ•ˆλ§Œ.

  3. μ½”λ“œ μˆ˜μ • (Make it right)

    μ‹œμŠ€ν…œμ€ λŒμ•„κ°„λ‹€(λ¬΄μ‹œν•œ 찝찝함을 λ’€λ‘œ 미뀄둔 채). 이제 μ˜¬λ°”λ₯Έ 개발둜 λŒμ•„κ°ˆ μ‹œκ°„μ΄λ‹€. λͺ¨λ“  μ€‘λ³΅λœ μ½”λ“œλ₯Ό μ—†μ• κ³  ν…ŒμŠ€νŠΈκ°€ λŒμ•„κ°€κ²Œ λ§Œλ“ λ‹€.

μ΅œμ’… λͺ©ν‘œλŠ” λ™μž‘ν•˜λŠ” κΉ¨λ—ν•œ μ½”λ“œ(직역, clean code)이닀. κΉ¨λ—ν•œ μ½”λ“œλŠ” μ΄λ”°κΈˆμ”© 졜고의 κ°œλ°œμžλ“€λ„ μž‘μ„±ν•˜κΈ° νž˜λ“€κ³ , λ‚˜κ°™μ€ λŒ€λ‹€μˆ˜μ˜ κ°œλ°œμžλ“€μ—κ²Œμ„œλŠ” λŒ€λΆ€λΆ„ 거의 λΆˆκ°€λŠ₯ν•œ 일이닀.
κ·Έλž˜μ„œ μš°λ¦¬λŠ” μ½”λ“œλ₯Ό 잘게 μͺΌκ°œκ³  μ£Όλ„κΆŒμ„ κ°€μ Έμ•Ό ν•œλ‹€(μ˜μ—­, divide and conquer). λ¨Όμ € μš°λ¦¬λŠ” μ–΄λ–€ λ¬Έμ œμ— μžˆμ–΄μ„œ 'λŒμ•„κ°„λ‹€'(that works) λΆ€ν„° ν•΄κ²°ν•  것이닀. κ·Έ 후에 'κΉ¨λ—ν•œ μ½”λ“œ'λ₯Ό ν•΄κ²°ν•œλ‹€.
이 과정은 'κΉ¨λ—ν•œ μ½”λ“œ'λ₯Ό λ¨Όμ € ν•΄κ²°ν•œ ν›„ λͺ¨λ“  것듀을 κ²°μ •λœ λ””μžμΈμ— κ²°ν•©ν•˜λ©΄μ„œ 'λŒμ•„κ°„λ‹€'λ₯Ό ν•΄κ²°ν•˜λŠ” Architecture-driven development와 λ°˜λŒ€μ˜ 과정이닀.


λΉ λ₯΄κ²Œ ν…ŒμŠ€νŠΈλ₯Ό μ„±κ³΅μ‹œν‚€λŠ” 팁

  1. 속여라 (Fake it)

    μ΅œμ’… μ½”λ“œλ₯Ό 가지기 μ „κΉŒμ§€ μƒμˆ˜(constant)λ₯Ό λ°˜ν™˜ν•˜κ³  점차 λ³€μˆ˜(variable)λ“€λ‘œ λ³€ν™˜μ‹œν‚¨λ‹€.

      @Test
      public void testMultiplication() {
        Dollar five = Dollar(5);
        assertEquals(new Dollar(10), five.times(2));
      }
    
      // Constant
      int times() {
        return 10;
      }
    
      // Variables
      private int amount;
    
      public Dollar(int amount) {
        this.amount = amount;
      }
    
      int times(int multiplier) {
        return amount *= multiplier;
      }
  2. λͺ…ν™•ν•œ κ΅¬ν˜„μ„ μ‚¬μš©ν•΄λΌ (Use Obvious Implementation)

    μž‘μ„±ν•˜λŠ” ν…ŒμŠ€νŠΈκ°€ 문제 없이 진행 되고, λ‹€μŒ implementation이 λͺ…ν™•ν•  κ²½μš°μ—λŠ” λ°”λ‘œ κ΅¬ν˜„ν•œλ‹€. (λ¬Όλ‘  ν…ŒμŠ€νŠΈλ₯Ό 계속 돌리며 λ‚΄κ²Œ λͺ…ν™•ν•œ κ΅¬ν˜„μ΄ μ»΄ν“¨ν„°μ—κ²Œλ„ λͺ…ν™•ν•œμ§€ ν™•μΈν•˜λ©΄μ„œβ˜ΊοΈ) λ§Œμ•½, ν…ŒμŠ€νŠΈκ°€ μ‹€νŒ¨ν•˜λ©΄ λ‹€μ‹œ Faking implementation 으둜 λŒμ•„κ°€μ„œ λ¦¬νŒ©ν† λ§ν•œλ‹€.

    @Test
    public void testArea() {  
        Square s = new Square(2.0);
        assertEquals(4.0, s.area());
    }
    
    private double side;
    
    public Square(double side) {
      this.side = side;
    }
    
    public double area() { 
      return side * side;
    }
  3. μ‚Όκ°μΈ‘λŸ‰ (Triangulation)

    λ§Œμ•½ 두 기지ꡭ μ‚¬μ΄μ˜ 거리λ₯Ό μ•Œκ³  있고 λΌλ””μ˜€ μ‹ ν˜Έκ°€ μ˜€λŠ” λ°©ν–₯을 μ•Œκ³  μžˆλ‹€λ©΄, μš°λ¦¬λŠ” μ‹ ν˜Έμ˜ λ²”μœ„μ™€ λ°©ν–₯을 계산할 수 μžˆλŠ” μΆ©λΆ„ν•œ 정보λ₯Ό 가지고 μžˆλ‹€λŠ” λœ»μ΄λ‹€. 이 계산을 μ‚Όκ°μΈ‘λŸ‰μ΄λΌκ³  ν•œλ‹€.

    Picture: 'Basic Overview: Tracking Radio Signals'




    λΉ„μœ μ μœΌλ‘œ μš°λ¦¬λŠ” 두 개 μ΄μƒμ˜ μ˜ˆμ‹œκ°€ μžˆμ„ κ²½μš°μ—λ§Œ μ½”λ“œλ₯Ό λ³΄νŽΈν™”(generalize)μ‹œν‚¨λ‹€. κ·Έ μ „ ν…ŒμŠ€νŠΈμ™€ μ½”λ“œ μ‚¬μ΄μ˜ 쀑볡은 λ¬΄μ‹œν•œλ‹€. λ‘λ²ˆμ§Έ μ˜ˆμ‹œκ°€ 더 λ³΄νŽΈν™”λœ ν•΄κ²° 방법을 ν•„μš”λ‘œ ν•  λ•Œλ§Œ μ½”λ“œλ₯Ό λ³΄νŽΈν™” μ‹œν‚¨λ‹€.

      // Fake it, no generalizing
      public boolean equals(Object object) {
        return true;
      }
    
      @Test
      public void testEquality() {
        //First example
        assertTrue(new Dollar(5).equals(new Dollar(5)));
      }
    
      // Writing the second example..
    
      @Test
      public void testEquality() {
        //First example
        assertTrue(new Dollar(5).equals(new Dollar(5)));
        //Second example
        assertFalse(new Dollar(5).equals(new Dollar(6)));
      }
    
      //Now we generalize the code
      public boolean equals(Object object) {
        Dollar dollar = (Dollar) object;
        return amount == dollar.amount;
      }