From 4300d8a862557bfc3d8fa7ebcad47a5a6b573e32 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Wed, 20 Mar 2024 21:09:51 -0700 Subject: [PATCH] FastAPI + SQLAlchemy + PyTest coverage --- .../fastapi-sqlalchemy-pytest-coverage.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 site/content/posts/fastapi-sqlalchemy-pytest-coverage.md diff --git a/site/content/posts/fastapi-sqlalchemy-pytest-coverage.md b/site/content/posts/fastapi-sqlalchemy-pytest-coverage.md new file mode 100644 index 0000000..0bfb154 --- /dev/null +++ b/site/content/posts/fastapi-sqlalchemy-pytest-coverage.md @@ -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