Skip to content

Commit 0919dc5

Browse files
committed
feat(tests): add GitHub Actions workflow for automated testing and coverage reporting
1 parent 50c187e commit 0919dc5

File tree

5 files changed

+131
-14
lines changed

5 files changed

+131
-14
lines changed

.github/workflows/pr-test.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: PR Test & Coverage Report
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main # 필요시 변경
7+
8+
jobs:
9+
test:
10+
name: Run Tests & Coverage
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout Code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: '3.12.6'
21+
22+
- name: Install Dependencies
23+
run: |
24+
python -m pip install -r requirements.txt
25+
python -m pip install coverage junitparser
26+
27+
- name: Run Tests (by Makefile)
28+
run: |
29+
make test
30+
31+
- name: Combine Coverage Report (XML)
32+
run: |
33+
coverage xml -o coverage.xml
34+
35+
- name: Parse Coverage and Write Summary
36+
run: |
37+
TOTAL=$(xmllint --xpath 'string(/coverage/@line-rate)' coverage.xml)
38+
PERCENTAGE=$(echo "$TOTAL * 100" | bc)
39+
echo "| Metric | Value |" >> $GITHUB_STEP_SUMMARY
40+
echo "|--------|-----|" >> $GITHUB_STEP_SUMMARY
41+
echo "| 📊 Line Coverage | ${PERCENTAGE}% |" >> $GITHUB_STEP_SUMMARY
42+
43+
- name: Publish Test Results Summary
44+
uses: test-summary/action@v2
45+
with:
46+
paths: "test-results/*.xml"
47+
if: always()
48+
49+
- name: Upload Coverage HTML Report
50+
uses: actions/upload-artifact@v4
51+
with:
52+
name: coverage-html
53+
path: htmlcov/
54+
55+
- name: Upload JUnit XML (for archive)
56+
uses: actions/upload-artifact@v4
57+
with:
58+
name: junit-xml-reports
59+
path: test-results/
60+
61+
- name: Add Coverage Comment to PR
62+
uses: peter-evans/create-or-update-comment@v4
63+
with:
64+
issue-number: ${{ github.event.pull_request.number }}
65+
body: |
66+
## 📝 Coverage Report
67+
| Metric | Value |
68+
|--------|-----|
69+
| 📊 Line Coverage | ${PERCENTAGE}% |
70+
if: github.event_name == 'pull_request'

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,5 @@ $RECYCLE.BIN/
358358

359359
## env-related files
360360
.env*
361+
362+
test-results/

Makefile

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
1-
.PHONY: test test-ch01 test-ch04 test-ch06 test-ch07 clean
1+
.PHONY: test test-all test-ch01 test-ch04 test-ch06 test-ch07 clean clean-coverage clean-results combine-coverage merge-junit
22

3-
test-all: test-ch01 test-ch04 test-ch06 test-ch07
3+
CHAPTERS = ch01 ch04 ch06 ch07
4+
TEST_RESULTS_DIR = test-results
5+
6+
test: test-all
7+
8+
clean: clean-results clean-coverage
9+
find . -type d -name "__pycache__" -exec rm -r {} +
10+
find . -type d -name ".pytest_cache" -exec rm -r {} +
11+
find . -type f -name "*.pyc" -delete
12+
13+
clean-results:
14+
rm -rf $(TEST_RESULTS_DIR)
15+
16+
clean-coverage:
17+
coverage erase
18+
rm -f $(TEST_RESULTS_DIR)/.coverage*
19+
20+
test-all: clean-results clean-coverage $(TEST_RESULTS_DIR) test-ch01 test-ch04 test-ch06 test-ch07 combine-coverage merge-junit
421

522
test-ch01:
6-
cd chapter01 && pytest && cd ..
23+
cd chapter01 && \
24+
COVERAGE_FILE=../$(TEST_RESULTS_DIR)/.coverage.ch01 coverage run -m pytest -v \
25+
--junitxml=../$(TEST_RESULTS_DIR)/ch01-results.xml
726

827
test-ch04:
9-
cd chapter04 && pytest && cd ..
28+
cd chapter04 && \
29+
COVERAGE_FILE=../$(TEST_RESULTS_DIR)/.coverage.ch04 coverage run -m pytest -v \
30+
--junitxml=../$(TEST_RESULTS_DIR)/ch04-results.xml
1031

1132
test-ch06:
12-
cd chapter06 && pytest && cd ..
33+
cd chapter06 && \
34+
COVERAGE_FILE=../$(TEST_RESULTS_DIR)/.coverage.ch06 coverage run -m pytest -v \
35+
--junitxml=../$(TEST_RESULTS_DIR)/ch06-results.xml
1336

1437
test-ch07:
15-
cd chapter07 && pytest && cd ..
38+
cd chapter07 && \
39+
COVERAGE_FILE=../$(TEST_RESULTS_DIR)/.coverage.ch07 coverage run -m pytest -v \
40+
--junitxml=../$(TEST_RESULTS_DIR)/ch07-results.xml
1641

17-
# 모든 테스트의 alias
18-
test: test-all
42+
combine-coverage:
43+
coverage combine $(TEST_RESULTS_DIR)
44+
coverage report
45+
coverage html
1946

20-
# 캐시 파일 정리
21-
clean:
22-
find . -type d -name "__pycache__" -exec rm -r {} +
23-
find . -type d -name ".pytest_cache" -exec rm -r {} +
24-
find . -type f -name "*.pyc" -delete
47+
merge-junit:
48+
command -v python3 > /dev/null && python3 -m pip show junitparser > /dev/null || python3 -m pip install junitparser
49+
python3 -m junitparser merge \
50+
$(TEST_RESULTS_DIR)/ch01-results.xml \
51+
$(TEST_RESULTS_DIR)/ch04-results.xml \
52+
$(TEST_RESULTS_DIR)/ch06-results.xml \
53+
$(TEST_RESULTS_DIR)/ch07-results.xml \
54+
$(TEST_RESULTS_DIR)/merged-results.xml
55+
56+
$(TEST_RESULTS_DIR):
57+
mkdir -p $(TEST_RESULTS_DIR)

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ arrow = "^1.3.0"
1515
pytest = "^8.3.4"
1616
beautifulsoup4 = "^4.12.3"
1717
pytest-cov = "^6.0.0"
18+
junitparser = "^3.2.0"
1819

1920

2021
[tool.poetry.group.linter.dependencies]

0 commit comments

Comments
 (0)