특징

  1. 한 개 단위로 function, method, class 들을 테스트 할 수 있다.
  2. 이러한 단위들이 기대했던 결과를 생산하는지에 대한 확실함을 만들 수 있다.
  3. 네트워크, GPS, 데이터베이스 등 외부 의존성에 대한 기능이 제대로 작동하는지에 대한 검사를 제공한다.
  4. 오류들을 시연하고 오류에 대한 대응 처리가 제대로 작동하는지에 대해 점검할 수 있다.
  5. 가장 작은 단위의 테스트이기 때문에 수백 수천개의 테스트도 매우 빠르게 실행할 수 있다.
  6. 하지만 개별 단위의 코드가 전부 작동한다고 해서 그걸 합쳤을 때도 여전히 정상적으로 작동한다는 점을 보장하지는 않는다는 것을 명심해야 한다.

좋은 테스트를 만들기 위한 3단계

  1. Setup

    1. 실질적으로 테스트를 할 객체를 생성한다.

      main() {
         test('The calculator returns 8 when adding 6 and 2', () {
               // 1st step: setup -> create the calculator object
           final calculator= Calculator();
         });
      }
      
  2. Side Effects

    1. 실제로 테스트를 진행할 함수 등 사이드이펙트를 낳을 수 있는 요소를 생성한다.

      main() {
        test('The calculator returns 8 when adding 6 and 2', () {
              // 1st step: setup -> create the calculator object
          final calculator= Calculator();
      
              // 2nd step: side effect -> collect the result you want to test
              final result = calculator.add(6,2);
        });
      }
      
  3. Expectation

    1. 요소를 실행하고 기댓값과 비교 절차를 수행한다.

      main() {
           test('The calculator returns 8 when adding 6 and 2', () {
                 // 1st step: setup -> create the calculator object
             final calculator= Calculator();
      
                 // 2nd step: side effect -> collect the result you want to test
                 final result = calculator.add(6,2);
      
                 // 3rd step: expectation -> compare the result against and expected value
                 expect(result,8);
           });
      }
      

이같은 요소는 가독성과 유지보수성을 위해 따라야 하는 중요한 단계다.

테스트 함수