test: Add unit tests for backend-api #57
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Test Workflow with Coverage | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - dev | |
| paths: | |
| - 'src/backend-api/**/*.py' | |
| - 'src/backend-api/pyproject.toml' | |
| - 'src/backend-api/pytest.ini' | |
| - 'src/processor/**/*.py' | |
| - 'src/processor/pyproject.toml' | |
| - '.github/workflows/test.yml' | |
| pull_request: | |
| types: | |
| - opened | |
| - ready_for_review | |
| - reopened | |
| - synchronize | |
| branches: | |
| - main | |
| - dev | |
| paths: | |
| - 'src/backend-api/**/*.py' | |
| - 'src/backend-api/pyproject.toml' | |
| - 'src/backend-api/pytest.ini' | |
| - 'src/processor/**/*.py' | |
| - 'src/processor/pyproject.toml' | |
| - '.github/workflows/test.yml' | |
| permissions: | |
| contents: read | |
| actions: read | |
| pull-requests: write | |
| jobs: | |
| backend_tests: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v5 | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: "3.12" | |
| - name: Install Backend Dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| cd src/backend-api | |
| pip install -e . | |
| pip install pytest pytest-cov pytest-asyncio | |
| - name: Check if Backend Test Files Exist | |
| id: check_backend_tests | |
| run: | | |
| if [ -z "$(find src/backend-api/src/tests -type f -name 'test_*.py' 2>/dev/null)" ]; then | |
| echo "No backend test files found, skipping backend tests." | |
| echo "skip_backend_tests=true" >> $GITHUB_ENV | |
| else | |
| echo "Backend test files found, running tests." | |
| echo "skip_backend_tests=false" >> $GITHUB_ENV | |
| fi | |
| - name: Run Backend Tests with Coverage | |
| if: env.skip_backend_tests == 'false' | |
| run: | | |
| cd src/backend-api | |
| PYTHONPATH=src/app pytest src/tests \ | |
| -c /dev/null \ | |
| --rootdir=. \ | |
| --cov=src/app \ | |
| --cov-report=term-missing \ | |
| --cov-report=xml:reports/coverage.xml \ | |
| --junitxml=pytest.xml \ | |
| -v | |
| - name: Generate coverage summary | |
| if: env.skip_backend_tests == 'false' | |
| run: | | |
| python -c " | |
| import xml.etree.ElementTree as ET | |
| tree = ET.parse('src/backend-api/reports/coverage.xml') | |
| root = tree.getroot() | |
| stmts = int(root.attrib.get('lines-valid', 0)) | |
| miss = stmts - int(root.attrib.get('lines-covered', 0)) | |
| cover = int(float(root.attrib.get('line-rate', 0)) * 100) | |
| with open('src/backend-api/reports/coverage-summary.txt', 'w') as f: | |
| f.write('Name Stmts Miss Cover\n') | |
| f.write(f'TOTAL {stmts} {miss} {cover}%\n') | |
| " | |
| - name: Pytest Coverage Comment | |
| if: | | |
| always() && | |
| github.event_name == 'pull_request' && | |
| github.event.pull_request.head.repo.fork == false && | |
| env.skip_backend_tests == 'false' | |
| uses: MishaKav/pytest-coverage-comment@26f986d2599c288bb62f623d29c2da98609e9cd4 # v1.6.0 | |
| with: | |
| pytest-coverage-path: src/backend-api/reports/coverage-summary.txt | |
| junitxml-path: src/backend-api/pytest.xml | |
| - name: Skip Backend Tests | |
| if: env.skip_backend_tests == 'true' | |
| run: | | |
| echo "Skipping backend tests because no test files were found." | |
| processor_tests: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v5 | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: "3.12" | |
| - name: Install Processor Dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| cd src/processor | |
| pip install -e . | |
| pip install pytest pytest-cov pytest-asyncio | |
| - name: Check if Processor Test Files Exist | |
| id: check_processor_tests | |
| run: | | |
| if [ -z "$(find src/processor/src/tests -type f -name 'test_*.py' 2>/dev/null)" ]; then | |
| echo "No processor test files found, skipping processor tests." | |
| echo "skip_processor_tests=true" >> $GITHUB_ENV | |
| else | |
| echo "Processor test files found, running tests." | |
| echo "skip_processor_tests=false" >> $GITHUB_ENV | |
| fi | |
| - name: Run Processor Tests with Coverage | |
| if: env.skip_processor_tests == 'false' | |
| run: | | |
| cd src/processor | |
| pytest src/tests \ | |
| --cov=src \ | |
| --cov-report=term-missing \ | |
| --cov-report=xml:reports/coverage.xml \ | |
| --junitxml=pytest.xml \ | |
| -v | |
| - name: Generate coverage summary | |
| if: env.skip_processor_tests == 'false' | |
| run: | | |
| python -c " | |
| import xml.etree.ElementTree as ET | |
| tree = ET.parse('src/processor/reports/coverage.xml') | |
| root = tree.getroot() | |
| stmts = int(root.attrib.get('lines-valid', 0)) | |
| miss = stmts - int(root.attrib.get('lines-covered', 0)) | |
| cover = int(float(root.attrib.get('line-rate', 0)) * 100) | |
| with open('src/processor/reports/coverage-summary.txt', 'w') as f: | |
| f.write('Name Stmts Miss Cover\n') | |
| f.write(f'TOTAL {stmts} {miss} {cover}%\n') | |
| " | |
| - name: Pytest Coverage Comment (Processor) | |
| if: | | |
| always() && | |
| github.event_name == 'pull_request' && | |
| github.event.pull_request.head.repo.fork == false && | |
| env.skip_processor_tests == 'false' | |
| uses: MishaKav/pytest-coverage-comment@26f986d2599c288bb62f623d29c2da98609e9cd4 # v1.6.0 | |
| with: | |
| pytest-coverage-path: src/processor/reports/coverage-summary.txt | |
| junitxml-path: src/processor/pytest.xml | |
| title: Processor Coverage Report | |
| unique-id-for-comment: processor | |
| - name: Skip Processor Tests | |
| if: env.skip_processor_tests == 'true' | |
| run: | | |
| echo "Skipping processor tests because no test files were found." |