IT 이야기/IT Tech

gTest(Google Testing Framework) 사용하기

필넷 2011. 6. 21. 23:45
반응형

gTest에 대한 소개에 앞서 테스팅에 대해 간단히 정리해본다.

테스팅이란?


테스팅이란 소프트웨어나 시스템이 사용자가 요구하는 수준을 만족시키는지 확인하기 위한 행위로 결함을 발견하는 메커니즘이다. 즉, 소프트웨어를 실행하여 테스트를 수행하는 동적 테스팅(Dynamic Testing)뿐만아니라 개발초기에 요구분석이나 설계 단계의 산출물을 테스팅 관점에서 리뷰(Review)하고 구현단계의 코드를 인스펙션(Inspection) 하는 정적인 행위[각주:1]까지도 포함한다.

소프트웨어 개발 생명주기를 놓고볼 때, 우리가 기본적으로 인식하고 있는 동적 테스팅 영역에서 가장 처음 발생하는 테스팅이 단위테스트(혹은 유닛테스트)이다. 단위테스트는 테스트 가능한 최소 단위의 소프트웨어에서 기능을 검증하고 결함을 발견하는 행위이다.

단위테스트가 제대로 수행되지 않고 나중에 통합테스트를 하게되면 컴포넌트나 시스템 사이의 상호연동에 집중하지 못하게 된다. 즉, 발생된 결함을 특정 범위로 격리하지 못하여 시간이 갈수록 개발의 리스크가 급격히 증가하게 된다. 하지만 실제 개발 현장에서는 단위테스트가 충분히 수행되지 못하는 것이 사실이다. 단순히 개발자가 구현하고 기초적인 테스트를 수행하면서 발생하는 디버깅 활동을 단위테스트로 생각하기도 한다.

단위테스트와 디버깅은 엄연히 다른 개념이다. 단위테스트를 하기 위해서는 기능명세를 기반으로 또는 프로그램상의 논리적 흐름이나 이외의 여러가지 방법을 통해서 테스트케이스의 설계와 테스팅이 수행된다. 그리고 디버깅은 발견된 결함을 수정하는 개발활동이다.

gTest(Google Testing Framework)로 단위테스트 정복!


Google Testing Framework Wiki 문서에 다음과 같은 소개글이 있다.

Introduction: Why Google C++ Testing Framework?

  • Tests should be independent and repeatable. It's a pain to debug a test that succeeds or fails as a result of other tests. Google C++ Testing Framework isolates the tests by running each of them on a different object. When a test fails, Google C++ Testing Framework allows you to run it in isolation for quick debugging.
  • Tests should be well organized and reflect the structure of the tested code. Google C++ Testing Framework groups related tests into test cases that can share data and subroutines. This common pattern is easy to recognize and makes tests easy to maintain. Such consistency is especially helpful when people switch projects and start to work on a new code base.
  • Tests should be portable and reusable. The open-source community has a lot of code that is platform-neutral, its tests should also be platform-neutral. Google C++ Testing Framework works on different OSes, with different compilers (gcc, MSVC, and others), with or without exceptions, so Google C++ Testing Framework tests can easily work with a variety of configurations. (Note that the current release only contains build scripts for Linux - we are actively working on scripts for other platforms.)
  • When tests fail, they should provide as much information about the problem as possible. Google C++ Testing Framework doesn't stop at the first test failure. Instead, it only stops the current test and continues with the next. You can also set up tests that report non-fatal failures after which the current test continues. Thus, you can detect and fix multiple bugs in a single run-edit-compile cycle.
  • The testing framework should liberate test writers from housekeeping chores and let them focus on the test content. Google C++ Testing Framework automatically keeps track of all tests defined, and doesn't require the user to enumerate them in order to run them.
  • Tests should be fast. With Google C++ Testing Framework, you can reuse shared resources across tests and pay for the set-up/tear-down only once, without making tests depend on each other.

gTest 라이브러리는 이곳에서 다운로드 받을 수 있다. 아래는 gTest와 Eclipse를 연동하는 방법을 간단히 적는다. 이전글들과 연계해서 보면 좀 더 이해하기 쉽다.

  1. gTest 라이브러리 빌드

    • cd gtest-1.6.0
    • ./configure
    • make
    • /script/fuse_gtest_files.py . <project-dir>/contrib

      • <project-dir>은 gTest를 사용할 프로젝트의 root 디렉토리로 설정한다.
  2. Eclipse설정

    • Project Explorer의 SampleProject에서 우클릭하여 팝업메뉴 → Refresh
    • contrib 폴더에서 우클릭하여 Exclude from build
    • Project Explorer의 SampleProject에서 우클릭하여 팝업메뉴 → Properties → C/C++ Build → Settings → Tool Setting tab → G++ Compiler → Directories
    • Add... 버튼 클릭, Workspace... → SampleProject/contrib 를 선택 → OK
    • Project Explorer의 SampleProject 에서 우클릭하여 팝업메뉴 → Properties → C/C++ Build → Settings → Tool Setting tab → G++ Linker → Libraries
    • Add... 버튼 클릭, pthread입력(gTest를 위해 필요하다) → OK
  3. gTest 실행

    • DBguide.net에 아래와 좋은 글이 있어 링크한다.

    • 알아두면 편한 gTest 명령행 인자들

      • --gtest_output="xml:report.xml" → xml형식의 출력파일을 생성
      • --gtest_repeat=2 → 2번 반복
      • --gtest_break_on_failure → 테스트 실패시 디버거가 자동 호출
      • --gtest_filter=<test string> → 특정 테스트만 수행
      • -gtest_also_run_disabled_tests → 논리적 테스트 이름이나 개별 유닛 테스트 이름에 DISABLE_ 접두부가 사용된 경우 테스트가 비활성화 되지만 이 인자가 사용되면 DISABLE_ 접두부가 사용된 테스트도 활성화된다.

이 글은 스프링노트에서 작성되었습니다.


  1. 우리는 흔히 동적 테스팅(Dynamic Testing)만 테스트라고 인식하지만 이러한 행위도 테스팅의 일부분으로 정적 테스팅(Static Testing)이라고 말한다. [본문으로]
반응형