Skip to content

Commit 69899ef

Browse files
committed
test: create test and CI
1 parent 9ea038a commit 69899ef

File tree

7 files changed

+78
-1
lines changed

7 files changed

+78
-1
lines changed

.github/workflows/ci.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
6+
permissions:
7+
contents: read
8+
pull-requests: write
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
18+
- name: Set up Docker Compose
19+
run: |
20+
docker-compose up -d
21+
22+
- name: Run tests (inside Docker)
23+
run: |
24+
docker-compose exec fastapi_server pytest /code/test/ --junitxml=/code/pytest_report.xml
25+
26+
- name: Install Node.js and xml2js
27+
uses: actions/setup-node@v3
28+
with:
29+
node-version: '16'
30+
31+
- name: Install xml2js
32+
run: |
33+
npm install xml2js
34+
35+
- name: Report test results
36+
if: failure()
37+
uses: actions/github-script@v6
38+
with:
39+
script: |
40+
const fs = require('fs');
41+
const xml2js = require('xml2js');
42+
const core = require('@actions/core');
43+
const parser = new xml2js.Parser();
44+
const xml = fs.readFileSync('src/pytest_report.xml', 'utf8');
45+
parser.parseString(xml, (err, result) => {
46+
if (err) {
47+
core.setFailed('Failed to parse XML');
48+
return;
49+
}
50+
const failures = result.testsuite.testcase.filter(tc => tc.failure);
51+
let commentBody = '## :x: Tests failed\n';
52+
failures.forEach(tc => {
53+
commentBody += `### ${tc.$.classname}::${tc.$.name}\n`;
54+
commentBody += `\`\`\`\n${tc.failure[0]._}\n\`\`\`\n`;
55+
});
56+
github.rest.issues.createComment({
57+
issue_number: context.payload.pull_request.number,
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
body: commentBody
61+
});
62+
});

src/poetry.lock

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

src/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fastapi = {extras = ["all"], version = "^0.115.0"}
4141
fastapi-pagination = {extras = ["sqlalchemy"], version = "^0.12.27"}
4242
asyncer = "^0.0.8"
4343
httpx = "^0.27.2"
44+
pytest = "^8.3.3"
4445

4546
[tool.ruff.per-file-ignores]
4647
"__init__.py" = [ "F401",]

src/pytest_report.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="1" time="0.584" timestamp="2024-09-25T12:29:57.738866+00:00" hostname="7f2848394f11"><testcase classname="test.unit.test_example" name="test_root_endpoint" time="0.040" /></testsuite></testsuites>

src/test/feature/__init__.py

Whitespace-only changes.

src/test/unit/__init__.py

Whitespace-only changes.

src/test/unit/test_example.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from fastapi.testclient import TestClient
2+
from app.main import app
3+
4+
client = TestClient(app)
5+
6+
7+
def test_root_endpoint():
8+
expected_response = {"message": "Hello World"}
9+
10+
response = client.get("/")
11+
12+
assert response.status_code == 200
13+
assert response.json() == expected_response

0 commit comments

Comments
 (0)