From e078ea84933fa73e09fe2cb10effcd254fb931ca Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Tue, 12 Apr 2022 18:20:38 -0400 Subject: [PATCH 1/5] Swap Codecov badge for custom coverage badge --- README.md | 5 +---- docs/index.md | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bffa26b..458585f 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,8 @@ _Docker images and utilities to power your Python APIs and help you ship faster. [![GitHub Container Registry](https://img.shields.io/badge/github%20container%20registry-inboard-success)](https://github.com/br3ndonland/inboard/pkgs/container/inboard) [![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://black.readthedocs.io/en/stable/) - +[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen?logo=pytest&logoColor=white)](https://coverage.readthedocs.io/en/latest/) [![builds](https://github.com/br3ndonland/inboard/workflows/builds/badge.svg)](https://github.com/br3ndonland/inboard/actions) -[![hooks](https://github.com/br3ndonland/inboard/workflows/hooks/badge.svg)](https://github.com/br3ndonland/inboard/actions) -[![tests](https://github.com/br3ndonland/inboard/workflows/tests/badge.svg)](https://github.com/br3ndonland/inboard/actions) -[![codecov](https://codecov.io/gh/br3ndonland/inboard/branch/develop/graph/badge.svg)](https://codecov.io/gh/br3ndonland/inboard) [![Mentioned in Awesome FastAPI](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/mjhea0/awesome-fastapi) diff --git a/docs/index.md b/docs/index.md index f7fc351..d9d3fd4 100644 --- a/docs/index.md +++ b/docs/index.md @@ -8,11 +8,8 @@ _Docker images and utilities to power your Python APIs and help you ship faster. [![GitHub Container Registry](https://img.shields.io/badge/github%20container%20registry-inboard-success)](https://github.com/br3ndonland/inboard/pkgs/container/inboard) [![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://black.readthedocs.io/en/stable/) - +[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen?logo=pytest&logoColor=white)](https://coverage.readthedocs.io/en/latest/) [![builds](https://github.com/br3ndonland/inboard/workflows/builds/badge.svg)](https://github.com/br3ndonland/inboard/actions) -[![hooks](https://github.com/br3ndonland/inboard/workflows/hooks/badge.svg)](https://github.com/br3ndonland/inboard/actions) -[![tests](https://github.com/br3ndonland/inboard/workflows/tests/badge.svg)](https://github.com/br3ndonland/inboard/actions) -[![codecov](https://codecov.io/gh/br3ndonland/inboard/branch/develop/graph/badge.svg)](https://codecov.io/gh/br3ndonland/inboard) [![Mentioned in Awesome FastAPI](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/mjhea0/awesome-fastapi) From 71d2ede57c1b9ff8c3f5f908ffe1918f2c8249df Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Tue, 12 Apr 2022 18:23:48 -0400 Subject: [PATCH 2/5] Update CONTRIBUTING.md for Codecov removal --- docs/contributing.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/contributing.md b/docs/contributing.md index ba0b43f..ec13d0a 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -109,12 +109,10 @@ As explained in the [VSCode docs](https://code.visualstudio.com/docs/containers/ - [parametrize](https://docs.pytest.org/en/latest/how-to/parametrize.html) - [temporary directories and files (`tmp_path` and `tmp_dir`)](https://docs.pytest.org/en/latest/how-to/tmpdir.html) - [pytest plugins](https://docs.pytest.org/en/latest/how-to/plugins.html) include: - - [pytest-cov](https://github.com/pytest-dev/pytest-cov) - [pytest-mock](https://github.com/pytest-dev/pytest-mock) - [pytest configuration](https://docs.pytest.org/en/latest/reference/customize.html) is in _[pyproject.toml](https://github.com/br3ndonland/inboard/blob/develop/pyproject.toml)_. - [FastAPI testing](https://fastapi.tiangolo.com/tutorial/testing/) and [Starlette testing](https://www.starlette.io/testclient/) rely on the [Starlette `TestClient`](https://www.starlette.io/testclient/). -- Test coverage results are reported when invoking `pytest` from the command-line. To see interactive HTML coverage reports, invoke pytest with `pytest --cov-report=html`. -- Test coverage reports are generated within GitHub Actions workflows by [pytest-cov](https://github.com/pytest-dev/pytest-cov) with [coverage.py](https://github.com/nedbat/coveragepy), and uploaded to [Codecov](https://docs.codecov.io/docs) using [codecov/codecov-action](https://github.com/marketplace/actions/codecov). Codecov is then integrated into pull requests with the [Codecov GitHub app](https://github.com/marketplace/codecov). +- Test coverage reports are generated by [coverage.py](https://github.com/nedbat/coveragepy). To generate test coverage reports, first run tests with `coverage run`, then generate a report with `coverage report`. To see interactive HTML coverage reports, run `coverage html` instead of `coverage report`. ## Docker From 829e862c3eae058375d50c1bed81c019ff143377 Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Tue, 12 Apr 2022 18:24:14 -0400 Subject: [PATCH 3/5] Remove pytest-cov and just use coverage.py --- poetry.lock | 21 +-------------------- pyproject.toml | 12 ++++++++++-- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/poetry.lock b/poetry.lock index 30bdab1..cdb05f4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -567,21 +567,6 @@ tomli = ">=1.0.0" [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] -[[package]] -name = "pytest-cov" -version = "3.0.0" -description = "Pytest plugin for measuring coverage." -category = "dev" -optional = false -python-versions = ">=3.6" - -[package.dependencies] -coverage = {version = ">=5.2.1", extras = ["toml"]} -pytest = ">=4.6" - -[package.extras] -testing = ["fields", "hunter", "process-tests", "six", "pytest-xdist", "virtualenv"] - [[package]] name = "pytest-mock" version = "3.7.0" @@ -837,7 +822,7 @@ starlette = ["starlette"] [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "59c433702d7523c59a7371d766140af2b0f73a8d191eda0f7004a16ed3faa91f" +content-hash = "72a1b3cfd1726e4ab1168597dad7553fa20219c25f259743d9e79d501dd0e7ed" [metadata.files] anyio = [ @@ -1216,10 +1201,6 @@ pytest = [ {file = "pytest-7.1.1-py3-none-any.whl", hash = "sha256:92f723789a8fdd7180b6b06483874feca4c48a5c76968e03bb3e7f806a1869ea"}, {file = "pytest-7.1.1.tar.gz", hash = "sha256:841132caef6b1ad17a9afde46dc4f6cfa59a05f9555aae5151f73bdf2820ca63"}, ] -pytest-cov = [ - {file = "pytest-cov-3.0.0.tar.gz", hash = "sha256:e7f0f5b1617d2210a2cabc266dfe2f4c75a8d32fb89eafb7ad9d06f6d076d470"}, - {file = "pytest_cov-3.0.0-py3-none-any.whl", hash = "sha256:578d5d15ac4a25e5f961c938b85a05b09fdaae9deef3bb6de9a6e766622ca7a6"}, -] pytest-mock = [ {file = "pytest-mock-3.7.0.tar.gz", hash = "sha256:5112bd92cc9f186ee96e1a92efc84969ea494939c3aead39c50f421c4cc69534"}, {file = "pytest_mock-3.7.0-py3-none-any.whl", hash = "sha256:6cff27cec936bf81dc5ee87f07132b807bcda51106b5ec4b90a04331cba76231"}, diff --git a/pyproject.toml b/pyproject.toml index e66e214..6b352e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,11 @@ +[tool.coverage.report] +fail_under = 100 +show_missing = true + +[tool.coverage.run] +command_line = "-m pytest" +source = ["inboard", "tests"] + [tool.isort] profile = "black" src_paths = ["inboard", "tests"] @@ -44,13 +52,13 @@ starlette = {version = "^0.17", optional = true} [tool.poetry.dev-dependencies] black = "^22" +coverage = {version = "^6", extras = ["toml"]} flake8 = "^4" isort = "^5.6" mkdocs-material = "^8" mypy = ">0.900,<1" pre-commit = "^2.8" pytest = "^7" -pytest-cov = "^3" pytest-mock = "^3.3" pytest-timeout = "^1.4" requests = "^2.24" @@ -64,7 +72,7 @@ starlette = ["starlette"] Docker = "https://github.com/br3ndonland/inboard/pkgs/container/inboard" [tool.pytest.ini_options] -addopts = "--cov=inboard -q" +addopts = "-q" minversion = "6.0" testpaths = ["tests"] From 5b9beff2d2e3482d65f2660262cdc99a469d5e35 Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Tue, 12 Apr 2022 18:24:31 -0400 Subject: [PATCH 4/5] Update GitHub Actions workflows for coverage.py --- .github/workflows/builds.yml | 12 ++++-------- .github/workflows/tests.yml | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml index 63ae223..83964c9 100644 --- a/.github/workflows/builds.yml +++ b/.github/workflows/builds.yml @@ -64,14 +64,10 @@ jobs: esac - name: Run pre-commit hooks run: poetry run pre-commit run --all-files - - name: Run unit tests - run: poetry run pytest --cov-report=xml - - name: Upload test coverage report to Codecov - if: matrix.python-version == '3.10' - uses: codecov/codecov-action@v3 - with: - fail_ci_if_error: true - flags: unit + - name: Run tests + run: poetry run coverage run + - name: Enforce test coverage + run: poetry run coverage report - name: Build Python package with latest Python version and publish to PyPI if: startsWith(github.ref, 'refs/tags/') && matrix.python-version == '3.10' run: | diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1e3cef3..3b57db7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,11 +59,7 @@ jobs: esac - name: Run pre-commit hooks run: poetry run pre-commit run --all-files - - name: Run unit tests - run: poetry run pytest --cov-report=xml - - name: Upload test coverage report to Codecov - if: matrix.python-version == '3.10' - uses: codecov/codecov-action@v3 - with: - fail_ci_if_error: true - flags: unit + - name: Run tests + run: poetry run coverage run + - name: Enforce test coverage + run: poetry run coverage report From 514368535352e0a74e0d613b3699931979f28149 Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Tue, 12 Apr 2022 18:25:21 -0400 Subject: [PATCH 5/5] Raise coverage of tests to 100% pytest-cov was previously just measuring coverage of the source code. This commit will increase coverage so the tests can be included also. --- tests/app/test_main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/app/test_main.py b/tests/app/test_main.py index d544ef0..8ebeea0 100644 --- a/tests/app/test_main.py +++ b/tests/app/test_main.py @@ -143,7 +143,7 @@ def test_get_asgi_incorrect_process_manager( monkeypatch.setenv("WITH_RELOAD", "false") with pytest.raises(NameError) as e: client_asgi.get("/") - assert str(e) == "Process manager needs to be either uvicorn or gunicorn." + assert "Process manager needs to be either uvicorn or gunicorn" in str(e.value) def test_get_root(self, client: TestClient) -> None: """Test a `GET` request to the root endpoint.""" @@ -181,7 +181,7 @@ def test_gets_with_basic_auth_no_credentials( "detail": "Server HTTP Basic auth credentials not set", "error": "Incorrect username or password", } - else: + else: # pragma: no cover raise AssertionError("TestClient should have a FastAPI or Starlette app.") assert error_response_json == expected_json @@ -214,7 +214,7 @@ def test_gets_with_basic_auth_incorrect_credentials( "detail": "HTTP Basic auth credentials not correct", "error": "Incorrect username or password", } - else: + else: # pragma: no cover raise AssertionError("TestClient should have a FastAPI or Starlette app.") assert error_response_json == expected_json @@ -237,7 +237,7 @@ def test_get_status_message( assert "FastAPI" in response_json["message"] elif isinstance(client.app, Starlette): assert "Starlette" in response_json["message"] - else: + else: # pragma: no cover raise AssertionError("TestClient should have a FastAPI or Starlette app.") def test_get_user(