build: Merging Dev changes to Main branch #9
This file contains 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: Tests | |
on: | |
push: | |
branches: | |
- main # Trigger on push to the main branch | |
pull_request: | |
branches: | |
- main # Trigger on pull requests to the main branch | |
types: | |
- opened | |
- ready_for_review | |
- reopened | |
- synchronize | |
jobs: | |
backend_tests: | |
name: Backend Tests | |
runs-on: ubuntu-latest # Use the latest Ubuntu runner | |
steps: | |
- uses: actions/checkout@v4 # Checkout the repository | |
# Set up Python environment for Backend | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.11" # Set Python version | |
- name: Install Backend Dependencies | |
run: | | |
python -m pip install -r requirements.txt | |
python -m pip install coverage pytest-cov | |
python -m pip install azure-keyvault-secrets | |
- name: Run Backend Tests with Coverage | |
run: | | |
if python -m pytest --cov=. --cov-report=xml --cov-report=html --cov-report=term-missing --junitxml=coverage-junit.xml; then | |
echo "Tests completed, checking coverage." | |
# Only fail if coverage does not meet criteria | |
if [ -f coverage.xml ]; then | |
COVERAGE=$(python -c "import xml.etree.ElementTree as ET; tree = ET.parse('coverage.xml'); root = tree.getroot(); coverage = root.find('coverage').get('lines').split('%')[0]; print(float(coverage))") | |
if (( $(echo "$COVERAGE < 80" | bc -l) )); then | |
echo "Coverage is below 80%, failing the job." | |
exit 1 | |
fi | |
fi | |
else | |
echo "No tests found, skipping coverage check." | |
fi | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: backend-coverage | |
path: | | |
coverage.xml # Correct path to backend coverage | |
coverage-junit.xml # Correct path to backend JUnit report | |
htmlcov/ # Correct path to backend HTML coverage report | |
frontend_tests: | |
name: Frontend Tests | |
runs-on: ubuntu-latest # Use the latest Ubuntu runner | |
steps: | |
- uses: actions/checkout@v4 # Checkout the repository | |
# Set up Node.js environment for Frontend | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '20' # Set the Node.js version | |
- name: Cache npm dependencies | |
uses: actions/cache@v3 | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Navigate to frontend directory | |
run: cd frontend | |
- name: Install Frontend Dependencies | |
run: | | |
cd frontend # Change to the frontend directory | |
npm install # Install dependencies from frontend/package.json | |
- name: Run Frontend Tests with Coverage | |
run: | | |
cd frontend # Change to the frontend directory | |
if npm run test -- --coverage; then | |
echo "Tests completed, checking coverage." | |
# Check coverage report and ensure it meets threshold | |
if [ -f frontend/coverage/lcov-report/index.html ]; then | |
COVERAGE=$(cat frontend/coverage/lcov-report/index.html | grep -oP 'total: \K[0-9]+(\.[0-9]+)?') | |
if (( $(echo "$COVERAGE < 80" | bc -l) )); then | |
echo "Coverage is below 80%, failing the job." | |
exit 1 | |
fi | |
fi | |
else | |
echo "No tests found, skipping coverage check." | |
fi | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: frontend-coverage | |
path: | | |
frontend/coverage/ # Correct path to frontend coverage | |
frontend/coverage/lcov-report/ # Correct path to frontend lcov report |