Skip to content

Commit

Permalink
Merge pull request #104 from uclahs-cds/aholmes-test-coverage
Browse files Browse the repository at this point in the history
Add Tox to manage dynamic options with `pytest`.
  • Loading branch information
aholmes authored Sep 5, 2024
2 parents f75be64 + 529cf20 commit 8d8768f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*.rd[as]

# Python
.tox
.coverage
coverage
cov.xml
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ PYPI_REPO ?= testpypi
REPORTS_DIR ?= reports
BANDIT_REPORT := bandit.sarif
PYTEST_REPORT := pytest
PYTEST_TARGET ?= .
TOX_DIR := .tox


# Can be overridden. This is used to change the prereqs
Expand Down Expand Up @@ -195,7 +197,7 @@ test-bandit : $(VENV) $(BUILD_TARGET)

test-pytest : $(VENV) $(BUILD_TARGET)
-$(ACTIVATE_VENV) && \
pytest $(PYTEST_FLAGS) && PYTEST_EXIT_CODE=0 || PYTEST_EXIT_CODE=$$?; \
PYTEST_TARGET=$(PYTEST_TARGET) tox && PYTEST_EXIT_CODE=0 || PYTEST_EXIT_CODE=$$?; \
coverage html --data-file=$(REPORTS_DIR)/$(PYTEST_REPORT)/.coverage; \
junit2html $(REPORTS_DIR)/$(PYTEST_REPORT)/pytest.xml $(REPORTS_DIR)/$(PYTEST_REPORT)/pytest.html; \
exit $$PYTEST_EXIT_CODE
Expand Down Expand Up @@ -230,6 +232,7 @@ clean-build :

clean-test :
$(CMD_PREFIX)rm -rf \
$(TOX_DIR) \
$(REPORTS_DIR)/$(PYTEST_REPORT) \
$(REPORTS_DIR)/$(BANDIT_REPORT)

Expand Down
27 changes: 24 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ packages = [
#
[project.optional-dependencies]
dev-dependencies = [
"tox ~= 4.18",
"pytest ~= 8.0",
"pytest-mock",
"mock",
"pytest-cov >= 4.1,< 5.0",
"coverage ~= 7.4",
"junit2html >= 30.1,< 32.0",
# Pyright >= 1.1.367 breaks the build.
# Waiting for new pyright release to fix it. https://github.com/uclahs-cds/BL_Python/issues/79
"pyright == 1.1.379",
"isort ~= 5.13",
"ruff ~= 0.3",
Expand Down Expand Up @@ -183,7 +182,6 @@ addopts = [
# https://github.com/microsoft/vscode-python/issues/21255
# and
# https://github.com/microsoft/vscode-python/issues/21845
"--cov=.",
"--junitxml=reports/pytest/pytest.xml",
"-o=junit_family=xunit2",
"--cov-report=xml:reports/pytest/cov.xml",
Expand All @@ -194,6 +192,29 @@ python_files = "test_*.py"

norecursedirs = "__pycache__ build .pytest_cache *.egg-info .venv .github-venv"

[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py310
[testenv]
# these options make Tox use the existing .venv dir created by venv
envdir = {env:VENV:.venv}
recreate = false
skip_install = true
setenv =
# configure --cov here because we can't use
# envvars in pyproject.toml, and we want this
# to be configurable in order to test coverage
# for a subset of tests in the monorepo rather
# than for all tests in the repo.
PYTEST_ADDOPTS = --cov={env:PYTEST_TARGET:.}
commands =
# use `.` add the default --cov option if it's not specified
pytest {env:PYTEST_FLAGS} {env:PYTEST_ADDOPTS:--cov=.} {env:PYTEST_TARGET}
"""

[tool.coverage.report]
include_namespace_packages = true

Expand Down

0 comments on commit 8d8768f

Please sign in to comment.