단위 테스트 보고서 예시
단위 테스트 보고서 예시
다양한 언어와 테스트 프레임워크에서 단위 테스트 보고서를 구성하는 지침으로 사용할 수 있는 예시 모음이에요. 각 예시는 복사해서 프로젝트에 맞게 적용할 수 있는 실제 동작하는 작업 구성을 옆에서 설명해 주는 방식으로 정리했어요.
단위 테스트 보고서는 테스트 프레임워크가 JUnit XML 형식 출력을 생성하고, CI/CD 작업이 그 결과를 artifacts로 업로드해야 동작해요.
출처: 문서
본문
이 예시들을 서로 다른 언어와 테스트 프레임워크에서 단위 테스트 보고서를 구성하는 지침으로 사용하세요. 단위 테스트 보고서는 테스트 프레임워크가 JUnit XML 형식 출력을 생성하고 CI/CD 작업이 결과를 artifacts로 업로드해야 해요.
다음 예시들은 .gitlab-ci.yml 파일에 추가할 개별 작업 구성을 보여줘요. 모든 예시는 다음을 사용해요.
- 테스트가 실패해도 보고서를 업로드하는
artifacts:when: always. - JUnit XML 파일 위치를 지정하는
artifacts:reports:junit. - 필요할 때
before_script에서 패키지 설치.
각 예시는 복사해서 프로젝트에 맞게 적용할 수 있는 실제 동작하는 작업이에요. 다음을 해야 할 수 있어요.
- 환경에 맞게
image:사양을 추가하거나 수정. - 의존성에 맞게 패키지 설치 명령 수정.
- 프로젝트 구조에 맞게 파일 경로 변경.
- 테스트 설정에 맞게 테스트 명령 업데이트.
설정 지침과 문제 해결은 단위 테스트 보고서를 참고하세요.
도구별 JUnit 출력 구성
| 언어 | 도구 | JUnit 출력 플래그 |
|---|---|---|
| .NET | JunitXML.TestLogger |
--logger:"junit;LogFilePath=report.xml" |
| C/C++ | GoogleTest | --gtest_output="xml:report.xml" |
| C/C++ | CUnit | CUnitCI.h 매크로로 자동 |
| Flutter/Dart | junitreport |
| tojunit -o report.xml |
| Go | gotestsum |
--junitfile report.xml |
| Helm | helm-unittest |
-t JUnit -o report.xml |
| Java | Gradle | build/test-results/test/에서 자동 |
| Java | Maven | target/surefire-reports/ 및 target/failsafe-reports/에서 자동 |
| JavaScript | jest-junit |
--reporters=jest-junit |
| JavaScript | karma-junit-reporter |
--reporters junit |
| JavaScript | mocha-gitlab-reporter |
--reporter mocha-gitlab-reporter |
| PHP | PHPUnit | --log-junit report.xml |
| Python | pytest |
--junitxml=report.xml |
| Ruby | rspec_junit_formatter |
--format RspecJunitFormatter --out report.xml |
| Rust | cargo2junit |
| cargo2junit > report.xml |
.NET
JunitXML.TestLogger NuGet 패키지를 사용해 .NET으로 JUnit XML 보고서를 생성하세요.
Test:
stage: test
script:
- 'dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
artifacts:
when: always
paths:
- ./**/*test-result.xml
reports:
junit:
- ./**/*test-result.xml
이 예시는 저장소 루트 폴더에 솔루션이 있고, 하위 폴더에 하나 이상의 프로젝트 파일이 있다고 가정해요. 테스트 프로젝트당 결과 파일 하나가 생성되고 각 파일은 artifacts 폴더에 배치돼요. 포맷 인수는 테스트 위젯에서 테스트 데이터의 가독성을 높여줘요.
C/C++
GoogleTest
내장 XML 출력을 사용해 GoogleTest로 JUnit XML 보고서를 생성하세요.
cpp:
stage: test
script:
- gtest.exe --gtest_output="xml:report.xml"
artifacts:
when: always
reports:
junit: report.xml
서로 다른 아키텍처(x86, x64, 또는 arm)용으로 생성된 여러 gtest 실행 파일이 있다면 각 테스트의 파일 이름이 고유한지 확인하세요. 그러면 결과가 함께 집계돼요.
CUnit
CUnitCI.h 매크로를 사용해 CUnit으로 JUnit XML 보고서를 생성하세요.
cunit:
stage: test
script:
- ./my-cunit-test
artifacts:
when: always
reports:
junit: ./my-cunit-test.xml
Flutter 또는 Dart
junitreport 패키지를 사용해 Flutter 또는 Dart로 JUnit XML 보고서를 생성하세요.
test:
stage: test
script:
- flutter test --machine | tojunit -o report.xml
artifacts:
when: always
reports:
junit:
- report.xml
이 예시는 junitreport 패키지를 사용해 flutter test 출력을 JUnit 보고서 XML 형식으로 변환해요.
Go
gotestsum을 사용해 Go로 JUnit XML 보고서를 생성하세요.
golang:
stage: test
script:
- go install gotest.tools/gotestsum@latest
- gotestsum --junitfile report.xml --format testname
artifacts:
when: always
reports:
junit: report.xml
Helm
Helm Unittest 플러그인을 사용해 Helm으로 JUnit XML 보고서를 생성하세요.
helm:
image: helmunittest/helm-unittest:latest
stage: test
script:
- '-t JUnit -o report.xml -f tests/*[._]test.yaml .'
artifacts:
when: always
reports:
junit: report.xml
-f tests/*[._]test.yaml 플래그는 helm-unittest가 tests/ 디렉터리에서 .test.yaml 또는 _test.yaml로 끝나는 파일을 찾도록 구성해요.
Java
Gradle
내장 테스트 보고를 사용해 Gradle로 JUnit XML 보고서를 생성하세요.
java:
stage: test
script:
- gradle test
artifacts:
when: always
reports:
junit: build/test-results/test/**/TEST-*.xml
테스트 작업이 여러 개 정의된 경우 gradle은 build/test-results/ 아래에 여러 디렉터리를 생성해요. 그 경우 glob 매칭을 활용해 build/test-results/test/**/TEST-*.xml 경로를 정의할 수 있어요.
Maven
Surefire와 Failsafe 테스트 보고서를 사용해 Maven으로 JUnit XML 보고서를 생성하세요.
java:
stage: test
script:
- mvn verify
artifacts:
when: always
reports:
junit:
- target/surefire-reports/TEST-*.xml
- target/failsafe-reports/TEST-*.xml
JavaScript
Jest
jest-junit npm 패키지를 사용해 Jest로 JUnit XML 보고서를 생성하세요.
javascript:
image: node:latest
stage: test
before_script:
- 'yarn global add jest'
- 'yarn add --dev jest-junit'
script:
- 'jest --ci --reporters=default --reporters=jest-junit'
artifacts:
when: always
reports:
junit:
- junit.xml
단위 테스트가 있는 .test.js 파일이 없을 때 작업을 통과시키려면 script: 섹션의 jest 명령 끝에 --passWithNoTests 플래그를 추가하세요.
Karma
karma-junit-reporter npm 패키지를 사용해 Karma로 JUnit XML 보고서를 생성하세요.
javascript:
stage: test
script:
- karma start --reporters junit
artifacts:
when: always
reports:
junit:
- junit.xml
Mocha
Mocha 구성 예시는 mocha-gitlab-reporter를 참고하세요.
PHP
PHPUnit을 사용해 PHP로 JUnit XML 보고서를 생성하세요.
phpunit:
stage: test
script:
- composer install
- vendor/bin/phpunit --log-junit report.xml
artifacts:
when: always
reports:
junit: report.xml
phpunit.xml 구성 파일의 XML로도 이 옵션을 구성할 수 있어요.
Python
pytest를 사용해 Python으로 JUnit XML 보고서를 생성하세요.
pytest:
stage: test
script:
- pytest --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xml
Ruby
rspec_junit_formatter gem을 사용해 RSpec으로 JUnit XML 보고서를 생성하세요.
ruby:
image: ruby:3.0.4
stage: test
before_script:
- apt-get update -y && apt-get install -y bundler
script:
- bundle install
- bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml
artifacts:
when: always
paths:
- rspec.xml
reports:
junit: rspec.xml
Rust
cargo2junit를 사용해 Rust로 JUnit XML 보고서를 생성하세요.
run unittests:
image: rust:latest
stage: test
before_script:
- cargo install --root . cargo2junit
script:
- cargo test -- -Z unstable-options --format json --report-time | bin/cargo2junit > report.xml
artifacts:
when: always
reports:
junit:
- report.xml
cargo test에서 JSON 출력을 가져오려면 nightly 컴파일러를 활성화해야 해요. 도구는 현재 디렉터리에 설치돼요.
더 알아보기
보고서 파일 형식 요구 사항, 테스트 결과 보기, 스크린샷 첨부 방법은 단위 테스트 보고서 문서에서 자세히 다루고 있어요. 이 예시를 프로젝트에 적용하기 전에 그 문서를 함께 읽어보시면 좋아요.