Skip to main content

리포지토리에 대한 코드 검사 설정

테스트 검사 보고서를 업로드하여 끌어오기 요청에 대한 검사 결과를 직접 확인하여 검토자가 병합하기 전에 테스트되지 않은 코드를 식별할 수 있도록 지원합니다.

누가 이 기능을 사용할 수 있나요?

관리자 역할이 있는 리포지토리 소유자, 조직 소유자 및 사용자

GitHub Team 또는 GitHub Enterprise Cloud

참고

GitHub Code Quality은(는) 현재 공개 미리 보기에 있으며 변경될 수 있습니다. 공개 미리 보기에서 Code Quality은(는) 요금이 청구되지 않지만 Code Quality 스캔은 GitHub Actions 분을 소모합니다.

다음 절차에서는 테스트 스위트에서 Cobertura XML 커버리지 보고서를 생성하고, 이를 GitHub에 업로드한 다음, 풀 리퀘스트에서 커버리지 결과를 확인합니다.

사전 요구 사항

  • Code Quality 는 리포지토리에 대해 사용하도록 설정되어 있습니다.
  • 리포지토리에는 GitHub Actions에서 실행되는 테스트 모음이 있습니다.
  • 테스트 프레임워크는 Cobertura XML 형식으로 검사 보고서를 생성할 수 있습니다.

1단계: Cobertura XML 검사 보고서 생성

Cobertura XML 형식으로 검사 보고서를 출력하도록 테스트 프레임워크를 구성합니다. 코드 검사는 이 형식을 생성할 수 있는 모든 프로그래밍 언어에서 작동합니다.

  1. 아래 표에서 해당 언어의 커버리지 도구를 식별하세요.
  2. 테스트를 실행할 때마다 Cobertura XML 파일이 생성되도록 CI 워크플로에 적절한 명령 또는 구성을 추가합니다.
언어프레임워크/도구Cobertura XML을 생성하는 방법
Pythonpytest + pytest-covpytest --cov=. --cov-report=xml
JavaJaCoCo
cover2cover.py 스크립트 또는 JaCoCo-to-Cobertura Gradle/Maven 플러그 인 사용
자바스크립트/타입스크립트이스탄불/ nycnyc report --reporter=cobertura
RubySimpleCov
SimpleCov::Formatter::CoberturaFormatter 추가
Gogo test + gocover-coberturago test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml

프레임워크가 위에 나열되지 않은 경우 해당 설명서에서 Cobertura 출력 지원을 확인합니다. 많은 도구가 직접 지원하거나 다른 형식에서 Cobertura XML로 변환할 수 있습니다.

2단계: 검사 보고서 업로드

테스트에서 Cobertura XML 보고서를 생성한 후, 풀 리퀘스트에 커버리지 결과가 표시되도록 이를 GitHub에 업로드하세요.

  1. 리포지토리의 CI 워크플로 파일(예: .github/workflows/ci.yml)을 엽니다.

  2. 테스트를 실행하고 검사 보고서를 생성하는 단계 다음에 다음 단계를 추가합니다.

    YAML
    - name: Upload coverage report
      if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
      uses: actions/upload-code-coverage@v1
      with:
        file: COVERAGE-FILE-PATH.xml
        language: LANGUAGE
        label: LABEL
    
  3. 다음 값을 바꿉니다.

    • COVERAGE-FILE-PATH.xml: Cobertura XML 보고서의 경로(예: coverage.xml 또는 target/site/jacoco/cobertura.xml)입니다.
    • LANGUAGE: 적용되는 코드의 기본 언어입니다(예: Python, Java, JavaScript).
    • LABEL: 이 검사 보고서를 식별하는 선택적 레이블(예: code-coverage/pytest)입니다.
  4. 워크플로 변경 내용을 커밋하고 푸시합니다.

전체 워크플로 예제

이 예제에서는 pytest-cov 사용하여 Python 테스트를 실행하고 검사 보고서를 업로드합니다.

YAML
name: Code Coverage

This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.

permissions:
  contents: read
  code-quality: write
jobs:
  test:
    runs-on: ubuntu-latest
    steps:

The code-quality: write permission is required to upload coverage data. No other elevated permissions are needed.

      - uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.

      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest pytest-cov

Replace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.

      - name: Run tests with coverage
        run: pytest --cov=. --cov-report=xml

Adapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.

      - name: Upload coverage report
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        uses: actions/upload-code-coverage@v1
        with:
          file: coverage.xml
          language: Python
          label: code-coverage/pytest

This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the github-code-quality[bot] bot posts a coverage summary directly on the pull request.

# This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.
name: Code Coverage

# Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

# The `code-quality: write` permission is required to upload coverage data. No other elevated permissions are needed.
permissions:
  contents: read
  code-quality: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.
      - uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      # Replace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.
      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest pytest-cov

      # Adapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.
      - name: Run tests with coverage
        run: pytest --cov=. --cov-report=xml

      # This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the `github-code-quality[bot]` bot posts a coverage summary directly on the pull request.
      - name: Upload coverage report
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        uses: actions/upload-code-coverage@v1
        with:
          file: coverage.xml
          language: Python
          label: code-coverage/pytest

3단계: 끌어오기 요청에 대한 검사 결과 보기

  1. 구성한 워크플로를 트리거하는 끌어오기 요청(또는 기존 요청으로 푸시)을 엽니다.
  2. 워크플로가 완료되면 풀 리퀘스트에서 github-code-quality[bot]의 댓글을 확인하세요. 주석에는 다음이 포함됩니다.
    • 기본 브랜치와 비교한 풀 리퀘스트 브랜치의 전체 커버리지 비율입니다.
    • 적용 범위를 얻었거나 손실된 파일을 보여 주는 파일별 분석입니다.

다음 단계