Skip to content

Commit

Permalink
FastAPI + SQLAlchemy + PyTest coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Xithrius committed Mar 21, 2024
1 parent f40777d commit 4300d8a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions site/content/posts/fastapi-sqlalchemy-pytest-coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "FastAPI + SQLAlchemy PyTest coverage"
date: "2024-03-20"
---

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.

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`:

```toml
[tool.coverage.run]
concurrency = ["gevent"]
```

If you don't have a script for running tests in `pyproject.toml`, you can use something like

```toml
test-cov = "python3 -m pytest --cov=./ --cov-report=xml"
```

Original issue: https://github.com/nedbat/coveragepy/issues/1012

0 comments on commit 4300d8a

Please sign in to comment.