Skip to content

Commit c276041

Browse files
authored
Merge pull request #3 from alephmelo/add-testing-infra
Add testing infra
2 parents c43a66a + d8f38c6 commit c276041

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ FastAPI Docker is a template to facilitate development as well as productionizat
1212

1313
## Customize
1414
### Settings Files
15-
The `envs` folder contains the files so you can use different settings depending on the environment you're running the API. It relies on the environment variable `MODE`, which evals to `production` or `development` in order to read the correct `.env` file.
15+
The `envs` folder contains the files so you can use different settings depending on the environment you're running the API. It relies on the environment variable `MODE`, which evals to `production`, `development` or `testing` in order to read the correct `.env` file.
1616

1717
### Environment Variables
1818
- `PORT`=`80`
@@ -27,7 +27,7 @@ $ cookiecutter https://github.com/alephmelo/fast-api-docker-template.git
2727
```
2828

2929
### Build
30-
This will build both `dev` and `prod` images.
30+
This will build `dev`, `prod` and `test` images.
3131
```bash
3232
$ make build
3333
```
@@ -43,6 +43,11 @@ dev_1 | INFO: Waiting for application startup.
4343
dev_1 | INFO: Application startup complete.
4444
```
4545

46+
### Test
47+
```bash
48+
$ make tests
49+
```
50+
4651
## Deployment
4752
Just push your built image to whatever cloud provider or manually start the service. It's tagged locally as `<app_name>/<app_name>:latest-prod`.
4853

@@ -57,4 +62,5 @@ INFO: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
5762
```
5863

5964
## Release History
65+
- 0.2.0 - Add testing infrastructure.
6066
- 0.1.0 - Initial version.

{{cookiecutter.app_name}}/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ COPY requirements.txt requirements.txt
66

77
RUN pip install --upgrade pip && pip install -r requirements.txt
88

9+
10+
# development image.
911
FROM base as development
1012

1113
COPY requirements_dev.txt requirements_dev.txt
1214

1315
RUN pip install -r requirements_dev.txt
1416

17+
18+
# testing image.
19+
FROM base as testing
20+
21+
RUN pip install pytest && pip install requests
22+
23+
24+
# production image.
1525
FROM base as production
1626

1727
WORKDIR /production

{{cookiecutter.app_name}}/Makefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
phony: build dev up-dev bash prod up-dev up-prod
1+
phony: build dev test tests up-dev bash prod up-dev up-prod
22

33

4-
build: dev prod
4+
build: dev prod test
55

66
dev:
77
docker-compose build dev
88

99
prod:
1010
docker-compose build prod
1111

12+
test:
13+
docker-compose build test
14+
15+
tests:
16+
docker-compose run test
17+
1218
up-dev:
1319
docker-compose up dev
1420

{{cookiecutter.app_name}}/docker-compose.yml

+16
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,19 @@ services:
2424
target: production
2525
context: .
2626
dockerfile: Dockerfile
27+
28+
test:
29+
image: {{cookiecutter.app_name}}/{{cookiecutter.app_name}}:latest-test
30+
working_dir: /home/{{cookiecutter.app_name}}
31+
entrypoint: pytest
32+
build:
33+
target: testing
34+
context: .
35+
dockerfile: Dockerfile
36+
ports:
37+
- 8000:8000
38+
volumes:
39+
- .:/home/{{cookiecutter.app_name}}
40+
41+
environment:
42+
- MODE=testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dbpath="localhost:5432"

{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from fastapi.testclient import TestClient
2+
3+
from {{cookiecutter.app_name}}.{{cookiecutter.main_file_name}} import app
4+
5+
client = TestClient(app)
6+
7+
8+
def test_read_main():
9+
response = client.get("/")
10+
assert response.status_code == 200
11+
assert response.json() == {"message": "Hello World, testing"}

0 commit comments

Comments
 (0)