Skip to content

Commit 3eafd08

Browse files
committed
[FEAT] add coverage test, update distribute workflow
1 parent a903ff6 commit 3eafd08

File tree

15 files changed

+1569
-16
lines changed

15 files changed

+1569
-16
lines changed

.github/workflows/distribute.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Distribute source to pypi package
22

33
on:
4-
push:
5-
tags:
6-
- '*'
4+
release:
5+
types:
6+
- created
77

88

99
jobs:

.github/workflows/test.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# TODO : add test job - using coverage
2-
name: test sources before publish
1+
name: Test and Coverage
2+
33
on:
44
push:
55
branches:
@@ -25,13 +25,28 @@ jobs:
2525
steps:
2626
- name: Checkout code
2727
uses: actions/checkout@v4
28+
2829
- name: Set up Python
2930
uses: actions/setup-python@v4
3031
with:
3132
python-version: ${{ matrix.python-version }}
33+
3234
- name: Setup PDM
3335
uses: pdm-project/setup-pdm@v4
36+
3437
- name: Install dependencies
3538
run: pdm install -G dev
36-
- name: Run tests
37-
run: pdm run pytest
39+
40+
- name: Run tests with coverage
41+
run: |
42+
pdm run pytest --cov=src/fastapi_fastkit --cov-report=term-missing --cov-report=xml --cov-fail-under=70
43+
44+
- name: Upload coverage to Codecov
45+
if: matrix.python-version == '3.12'
46+
uses: codecov/codecov-action@v4
47+
with:
48+
file: ./coverage.xml
49+
flags: unittests
50+
name: codecov-umbrella
51+
fail_ci_if_error: false
52+
token: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# test workspace
22
tests/temp_test_workspace/
33

4+
# Coverage reports
5+
htmlcov/
6+
.coverage
7+
coverage.xml
8+
.coverage.*
9+
coverage/
10+
411
.pdm-python
512
### VisualStudioCode template
613
.vscode/*

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ test-verbose: ## Run tests with verbose output
3030
pytest -v -s
3131

3232
test-coverage: ## Run tests with coverage report
33-
pytest --cov=src/fastapi_fastkit --cov-report=html --cov-report=term
33+
./scripts/coverage.sh
34+
35+
coverage: ## Alias for test-coverage
36+
$(MAKE) test-coverage
3437

3538
# Code quality commands
3639
lint: ## Run all linting checks
@@ -65,6 +68,7 @@ clean: ## Clean build artifacts and cache files
6568
find . -type f -name "*.pyo" -delete
6669
find . -type f -name ".coverage" -delete
6770
rm -rf htmlcov/
71+
rm -rf coverage.xml
6872

6973
build: clean ## Build the package
7074
python -m build

pdm.lock

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Changelog = "https://bnbong.github.io/FastAPI-fastkit/changelog/"
2222
[project.optional-dependencies]
2323
dev = [
2424
"pytest>=8.3.3",
25-
"pytest-cov>=5.0.0",
25+
"pytest-cov>=6.2.1",
2626
"black>=24.10.0",
2727
"pre-commit>=4.0.1",
2828
"mypy>=1.12.0",
@@ -41,8 +41,13 @@ version = { source = "file", path = "src/fastapi_fastkit/__init__.py" }
4141
distribution = true
4242

4343
[tool.pytest.ini_options]
44-
testpaths = ["test"]
44+
testpaths = ["tests"]
4545
python_files = "test_*.py"
46+
addopts = [
47+
"--strict-markers",
48+
"--strict-config",
49+
"--disable-warnings",
50+
]
4651

4752
[tool.isort]
4853
profile = "black"
@@ -65,3 +70,37 @@ docs = [
6570
"mkdocs-material>=9.6.14",
6671
"mdx-include>=1.4.2",
6772
]
73+
74+
[tool.coverage.run]
75+
source = ["src/fastapi_fastkit"]
76+
omit = [
77+
"*/tests/*",
78+
"*/test_*",
79+
"*/__pycache__/*",
80+
"*/.*",
81+
"*/venv/*",
82+
"*/.venv/*",
83+
"*/site-packages/*",
84+
"*/fastapi_project_template/*",
85+
"*/__main__.py",
86+
]
87+
88+
[tool.coverage.report]
89+
exclude_lines = [
90+
"pragma: no cover",
91+
"def __repr__",
92+
"if self.debug:",
93+
"if settings.DEBUG",
94+
"raise AssertionError",
95+
"raise NotImplementedError",
96+
"if 0:",
97+
"if __name__ == .__main__.:",
98+
"class .*\\bProtocol\\):",
99+
"@(abc\\.)?abstractmethod",
100+
]
101+
show_missing = true
102+
precision = 2
103+
fail_under = 70
104+
105+
[tool.coverage.html]
106+
directory = "htmlcov"

scripts/coverage-report.sh

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Colors for output
6+
RED='\033[0;31m'
7+
GREEN='\033[0;32m'
8+
BLUE='\033[0;34m'
9+
YELLOW='\033[1;33m'
10+
NC='\033[0m' # No Color
11+
12+
# Script options
13+
OPEN_HTML=false
14+
SHOW_MISSING=true
15+
MIN_COVERAGE=70
16+
OUTPUT_FORMAT="term"
17+
18+
# Help function
19+
show_help() {
20+
echo "FastAPI-fastkit Coverage Report Generator"
21+
echo "========================================"
22+
echo ""
23+
echo "Usage: $0 [OPTIONS]"
24+
echo ""
25+
echo "Options:"
26+
echo " -h, --help Show this help message"
27+
echo " -o, --open Open HTML report in browser (macOS only)"
28+
echo " -f, --format FORMAT Output format: term, html, xml, json, all (default: term)"
29+
echo " -m, --min NUM Minimum coverage percentage (default: 70)"
30+
echo " --no-missing Don't show missing lines in terminal output"
31+
echo ""
32+
echo "Examples:"
33+
echo " $0 # Generate terminal report"
34+
echo " $0 -f html -o # Generate HTML report and open in browser"
35+
echo " $0 -f all # Generate all report formats"
36+
echo " $0 -m 80 # Set minimum coverage to 80%"
37+
}
38+
39+
# Parse command line arguments
40+
while [[ $# -gt 0 ]]; do
41+
case $1 in
42+
-h|--help)
43+
show_help
44+
exit 0
45+
;;
46+
-o|--open)
47+
OPEN_HTML=true
48+
shift
49+
;;
50+
-f|--format)
51+
OUTPUT_FORMAT="$2"
52+
shift 2
53+
;;
54+
-m|--min)
55+
MIN_COVERAGE="$2"
56+
shift 2
57+
;;
58+
--no-missing)
59+
SHOW_MISSING=false
60+
shift
61+
;;
62+
*)
63+
echo -e "${RED}Error: Unknown option $1${NC}"
64+
show_help
65+
exit 1
66+
;;
67+
esac
68+
done
69+
70+
# Build pytest command
71+
PYTEST_ARGS="--cov=src/fastapi_fastkit --cov-fail-under=${MIN_COVERAGE}"
72+
73+
# Add output formats based on selection
74+
case $OUTPUT_FORMAT in
75+
"term")
76+
if [[ "$SHOW_MISSING" == "true" ]]; then
77+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=term-missing"
78+
else
79+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=term"
80+
fi
81+
;;
82+
"html")
83+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=html"
84+
OPEN_HTML=true
85+
;;
86+
"xml")
87+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=xml"
88+
;;
89+
"json")
90+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=json"
91+
;;
92+
"all")
93+
if [[ "$SHOW_MISSING" == "true" ]]; then
94+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=term-missing"
95+
else
96+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=term"
97+
fi
98+
PYTEST_ARGS="$PYTEST_ARGS --cov-report=html --cov-report=xml --cov-report=json"
99+
;;
100+
*)
101+
echo -e "${RED}Error: Invalid format '$OUTPUT_FORMAT'${NC}"
102+
echo "Valid formats: term, html, xml, json, all"
103+
exit 1
104+
;;
105+
esac
106+
107+
echo -e "${BLUE}🧪 Running coverage tests...${NC}"
108+
echo -e "${YELLOW}Minimum coverage threshold: ${MIN_COVERAGE}%${NC}"
109+
echo ""
110+
111+
# Run tests with coverage
112+
if pytest $PYTEST_ARGS; then
113+
echo ""
114+
echo -e "${GREEN}✅ Coverage test completed successfully!${NC}"
115+
116+
# Show generated reports
117+
echo ""
118+
echo -e "${BLUE}📊 Generated reports:${NC}"
119+
120+
if [[ "$OUTPUT_FORMAT" == "term" ]]; then
121+
echo " - Terminal output above"
122+
elif [[ "$OUTPUT_FORMAT" == "html" ]] || [[ "$OUTPUT_FORMAT" == "all" ]]; then
123+
echo " - HTML: htmlcov/index.html"
124+
fi
125+
126+
if [[ "$OUTPUT_FORMAT" == "xml" ]] || [[ "$OUTPUT_FORMAT" == "all" ]]; then
127+
echo " - XML: coverage.xml"
128+
fi
129+
130+
if [[ "$OUTPUT_FORMAT" == "json" ]] || [[ "$OUTPUT_FORMAT" == "all" ]]; then
131+
echo " - JSON: coverage.json"
132+
fi
133+
134+
if [[ "$OUTPUT_FORMAT" == "all" ]]; then
135+
echo " - Terminal output above"
136+
echo " - HTML: htmlcov/index.html"
137+
fi
138+
139+
# Open HTML report if requested
140+
if [[ "$OPEN_HTML" == "true" ]] && [[ "$OSTYPE" == "darwin"* ]] && [[ -z "$CI" ]]; then
141+
if [[ -f "htmlcov/index.html" ]]; then
142+
echo ""
143+
echo -e "${BLUE}🌐 Opening HTML coverage report in browser...${NC}"
144+
open htmlcov/index.html
145+
fi
146+
fi
147+
148+
else
149+
echo ""
150+
echo -e "${RED}❌ Coverage test failed!${NC}"
151+
echo -e "${YELLOW}Coverage is below the minimum threshold of ${MIN_COVERAGE}%${NC}"
152+
exit 1
153+
fi

scripts/coverage.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -x
5+
6+
echo "🧪 Running tests with coverage..."
7+
8+
# Run tests with coverage
9+
pytest --cov=src/fastapi_fastkit --cov-report=term-missing --cov-report=html --cov-report=xml --cov-fail-under=70
10+
11+
echo "✅ Coverage test completed!"
12+
echo "📊 Coverage report saved to:"
13+
echo " - Terminal: above output"
14+
echo " - HTML: htmlcov/index.html"
15+
echo " - XML: coverage.xml"
16+
17+
# Open HTML coverage report if running on macOS and not in CI
18+
if [[ "$OSTYPE" == "darwin"* ]] && [[ -z "$CI" ]]; then
19+
echo "🌐 Opening HTML coverage report in browser..."
20+
open htmlcov/index.html
21+
fi

src/fastapi_fastkit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.0.1"
1+
__version__ = "1.0.2"
22

33
import os
44

0 commit comments

Comments
 (0)