Skip to content

Commit 4300d8a

Browse files
committed
FastAPI + SQLAlchemy + PyTest coverage
1 parent f40777d commit 4300d8a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: "FastAPI + SQLAlchemy PyTest coverage"
3+
date: "2024-03-20"
4+
---
5+
6+
If you try to run [pytest-cov](https://pypi.org/project/pytest-cov/) with FastAPI and SQLAlchemy, you'll get a bunch of uncovered lines since the default configuration won't work.
7+
8+
An example line that wouldn't be covered in this senario would be `items.scalars().first()`. First, add the [gevent](https://pypi.org/project/gevent/) package to your environment, and then add the following lines to your `pyproject.toml`:
9+
10+
```toml
11+
[tool.coverage.run]
12+
concurrency = ["gevent"]
13+
```
14+
15+
If you don't have a script for running tests in `pyproject.toml`, you can use something like
16+
17+
```toml
18+
test-cov = "python3 -m pytest --cov=./ --cov-report=xml"
19+
```
20+
21+
Original issue: https://github.com/nedbat/coveragepy/issues/1012

0 commit comments

Comments
 (0)