Skip to content

Commit 7c3f930

Browse files
authored
Setup unit tests using pytest (#54)
Add pytest-style unit tests based on docker_test.py. Add pytest configuration. Add pytest CI job. Add pyyaml as a dependency for testing. Add dlite-python dependency for testing. Avoid using DLite for Python 3.12 Use pytest in favor of docker_test.py: Implement a `--live-backend` CLI option for pytest to determine whether or not to use a real MongoDB backend. Compartmentalize logging.
1 parent 8cac361 commit 7c3f930

File tree

13 files changed

+508
-306
lines changed

13 files changed

+508
-306
lines changed

.github/utils/docker_test.py

Lines changed: 0 additions & 224 deletions
This file was deleted.

.github/utils/requirements_docker_test.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

.github/workflows/ci_tests.yml

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141

4242
services:
4343
mongo:
44-
image: mongo:6
44+
image: mongo:7
4545
ports:
4646
- "27017:27017"
4747
env:
@@ -52,7 +52,8 @@ jobs:
5252
ENTITY_SERVICE_MONGO_URI: mongodb://localhost:27017
5353
ENTITY_SERVICE_MONGO_USER: root
5454
ENTITY_SERVICE_MONGO_PASSWORD: root
55-
DOCKER_TEST_PORT: 8000
55+
ENTITY_SERVICE_HOST: localhost
56+
ENTITY_SERVICE_PORT: 8000
5657

5758
steps:
5859
- name: Checkout ${{ github.repository }}
@@ -64,15 +65,20 @@ jobs:
6465
python-version: "3.10"
6566

6667
- name: Build Docker image
67-
run: docker build --pull -t entity-service --target ${{ matrix.docker_target }} .
68+
run: |
69+
docker build \
70+
--pull \
71+
--tag entity-service \
72+
--target ${{ matrix.docker_target }} \
73+
.
6874
6975
- name: Run Docker container
7076
run: |
7177
docker run --rm -d \
7278
--env ENTITY_SERVICE_MONGO_URI \
7379
--env ENTITY_SERVICE_MONGO_USER \
7480
--env ENTITY_SERVICE_MONGO_PASSWORD \
75-
--env PORT=${DOCKER_TEST_PORT} \
81+
--env PORT=${ENTITY_SERVICE_PORT} \
7682
--name "entity-service" \
7783
--network "host" \
7884
--volume "${PWD}:/app" \
@@ -83,19 +89,12 @@ jobs:
8389
run: |
8490
python -m pip install -U pip
8591
pip install -U setuptools wheel flit
86-
pip install -U -r .github/utils/requirements_docker_test.txt
87-
pip install -U -e .
88-
89-
- name: Fill MongoDB with test data
90-
run: ./.github/utils/docker_test.py add-testdata
92+
pip install -U -e .[testing]
9193
9294
- name: Run tests
93-
env:
94-
DOCKER_TEST_HOST: localhost
9595
run: |
9696
{
97-
./.github/utils/docker_test.py run-tests &&
98-
cat logs/dlite_entities_service.log
97+
pytest -vv --live-backend
9998
} || {
10099
echo "Failed! Here's the Docker logs for the service:" &&
101100
docker logs entity-service &&
@@ -105,3 +104,39 @@ jobs:
105104
106105
exit 1
107106
}
107+
108+
pytest:
109+
runs-on: ubuntu-latest
110+
111+
strategy:
112+
fail-fast: false
113+
matrix:
114+
python_version: ["3.10", "3.11", "3.12"]
115+
116+
steps:
117+
- name: Checkout ${{ github.repository }}
118+
uses: actions/checkout@v4
119+
120+
- name: Setup Python ${{ matrix.python_version }}
121+
uses: actions/setup-python@v4
122+
with:
123+
python-version: ${{ matrix.python_version }}
124+
125+
- name: Install test dependencies
126+
run: |
127+
python -m pip install -U pip
128+
pip install -U setuptools wheel flit
129+
pip install -U -e .[testing]
130+
131+
- name: Run pytest
132+
run: pytest -vv --cov-report=xml
133+
134+
- name: Upload coverage
135+
if: github.repository_owner == 'SINTEF' && matrix.python_version == '3.10'
136+
uses: codecov/codecov-action@v3
137+
with:
138+
fail_ci_if_error: true
139+
env_vars: OS,PYTHON
140+
env:
141+
OS: ubuntu-latest
142+
PYTHON: ${{ matrix.python_version }}

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ repos:
6666
additional_dependencies:
6767
- pydantic>=2
6868
- types-requests
69+
- types-pyyaml
70+
args: [--explicit-package-bases]

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ WORKDIR /app
55
COPY dlite_entities_service dlite_entities_service/
66
COPY pyproject.toml LICENSE README.md ./
77

8+
# Install dependencies
89
RUN python -m pip install -U pip && \
910
pip install -U pip setuptools wheel flit && \
1011
pip install -U uvicorn && \
11-
pip install -U -e .
12+
pip install -U -e . && \
13+
# Create log directory and file (if not existing already)
14+
mkdir -p logs && \
15+
touch -a logs/dlite_entities_service.log
1216

1317
FROM base as development
1418

0 commit comments

Comments
 (0)