백엔드 Back-end/테스트 Test

Q. 깃허브 풀리퀘스PullRequest 때 테스트를 자동화하는 방법은?

Tap to restart 2023. 2. 25. 18:00
반응형

A. 깃허브GitHub 워크플로우workflows를 활용하면 쉽게 테스트를 자동화할 수 있다.

 

pytest를 실행하는 예

깃허브 저장소에 .github 폴더를 만들고 그 아래 workflows를 만든다.
그리고 아래 내용을 담은 yml 파일을 만든다. 이름은 아무 거나 가능하다. tests.yml로 만들자.

name: Test
on:
  pull_request:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.6'
      - run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
          pytest

위 내용을 설명하면
on: pull_request 설정으로 main 브랜치로 PR을 연 경우에만 해당 워크플로우가 실행된다. (참고: Triggering a workflow)
runs-on: ubuntu-20.04은 우분투 20.04로 가상머신을 설정한 것이다. 윈도우, 우분투, 맥OS 중 가상 머신을 선택할 수 있다. (참고: Choosing GitHub-hosted runners)
uses: actions/checkout@v3는 다른 사람이 만들어 놓은 checkout@v3이란 액션을 사용하겠다는 뜻이다. 해당 액션을 사용하면 이 tests.yml 파일이 있는 저장소 소스 코드를 가져오게 된다. 
uses: actions/setup-python@v4도 checkout@v3과 마찬가지로 actions/setup-python@v4 액션을 사용하겠다는 뜻이다. 해당 액션을 사용하면 원하는 파이썬 버전을 설치할 수 있다. 파이썬 버전 3.6으로 설치된다. 
다음은 run: 을 이용해서 명령어를 실행하는 것이다. requirements.txt에 포함된 파이썬 팩키지들을 설치하고 pytest를 실행하게 된다.
 
풀리퀘스트를 보면 실행된 것을 확인할 수 있다.

초록색 표시이면 통과된 것이다. 
 
저장소 Actions를 눌러보면 깃허브 워크플로우가 실행된 결과를 확인할 수 있다.
실제로 테스트 코드가 실행된 것을 확인할 수 있다.

 
위 예는 테스트할 때 데이터베이스가 필요 없는 경우이다. 데이터베이스가 필요하다면 MySQL, PostgreSQL 등 설치하고 테스트를 진행하면 된다.

 

MySQL 설치 예

아래 예는 MySQL과 redis를 설치하는 경우다.

name: Run tests

on:
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-20.04
    services:
      mysql:
        image: mysql
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_DATABASE: tests
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
      redis:
        image: redis
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.8'

    - name: Install dependencies
      working-directory: requirements
      run: |
        sudo apt install libcurl4-openssl-dev libssl-dev mysql-client
        python -m pip install --upgrade pip
        python -m pip install --upgrade -r requirements.txt

    - name: Run tests
      run: pytest

 

위 예처럼 할 경우 깃허브 머신에 설치된 MySQL user는 root, password는 '' 이다.

테스트 설정을 따로 추가하고 데이터베이스 설정을 password는 ''로 하면 정상 작동한다.

 

아래는 장고 테스트 때 conf.settings 밑에 test.py란 테스트 설정을 따로 둘 경우 pytest.ini예이다.

[pytest]
DJANGO_SETTINGS_MODULE = conf.settings.test
python_files = test_*.py

아래처럼 데이터베이스 설정을 하면 된다.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'app',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

 

반응형