Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ install_requires =

[options.extras_require]
dev =
coverage [toml]
pytest<5.0.0,>=3.3.0
pytest-cookies

Expand Down
20 changes: 18 additions & 2 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,24 @@ def test_pytest(baked_with_development_dependencies, project_env_bin_dir):
result = run([f'{bin_dir}pytest'], project_dir)
assert result.returncode == 0
assert '== 3 passed in' in result.stdout
assert (project_dir / 'coverage.xml').exists()
assert (project_dir / 'htmlcov' / 'index.html').exists()


def test_coverage(baked_with_development_dependencies, project_env_bin_dir):
project_dir = baked_with_development_dependencies
bin_dir = project_env_bin_dir
result = run([f'{bin_dir}coverage', 'run', '-m', 'pytest'], project_dir)
assert result.returncode == 0
assert '== 3 passed in' in result.stdout
assert (project_dir / '.coverage').exists()


def test_tox(baked_with_development_dependencies, project_env_bin_dir):
project_dir = baked_with_development_dependencies
bin_dir = project_env_bin_dir
result = run([f'{bin_dir}tox'], project_dir)
assert result.returncode == 0
assert '== 3 passed in' in result.stdout
assert (project_dir / '.tox' / 'dist' / 'my_python_package-0.1.0.zip').exists()


def test_subpackage(baked_with_development_dependencies, project_env_bin_dir):
Expand Down
31 changes: 30 additions & 1 deletion {{cookiecutter.directory_name}}/README.dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,41 @@ Afterwards check that the install directory is present in the `PATH` environment

## Running the tests

Running the tests requires an activated virtual environment with the development tools installed.
There are two ways to run tests.

The first way requires an activated virtual environment with the development tools installed:

```shell
pytest -v
```

The second is to use `tox`, which can be installed separately (e.g. with `pip install tox`), i.e. not necessarily inside the virtual environment you use for installing `{{ cookiecutter.package_name }}`, but then builds the necessary virtual environments itself by simply running:

```shell
tox
```

Testing with `tox` allows for keeping the testing environment separate from your development environment.
The development environment will typically accumulate (old) packages during development that interfere with testing; this problem is avoided by testing with `tox`.

### Test coverage

In addition to just running the tests to see if they pass, they can be used for coverage statistics, i.e. to determine how much of the package's code is actually executed during tests.
In an activated virtual environment with the development tools installed, inside the package directory, run:

```shell
coverage run
```

This runs tests and stores the result in a `.coverage` file.
To see the results on the command line, run

```shell
coverage report
```

`coverage` can also generate output in HTML and other formats; see `coverage help` for more information.

## Running linters locally

For linting we will use [prospector](https://pypi.org/project/prospector/) and to sort imports we will use
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.directory_name}}/project_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ help you decide which tool to use for packaging.
- The `tests` folder contains:
- Example tests that you should replace with your own meaningful tests (file: `test_my_module.py`)
- The testing framework used is [PyTest](https://pytest.org)
- [PyTest introduction](http://pythontesting.net/framework/pytest/pytest-introduction/)
- [PyTest introduction](https://pythontest.com/pytest-book/)
- PyTest is listed as a development dependency
- This is configured in `setup.cfg`
- The project uses [GitHub action workflows](https://docs.github.com/en/actions) to automatically run tests on GitHub infrastructure against multiple Python versions
Expand Down
18 changes: 18 additions & 0 deletions {{cookiecutter.directory_name}}/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
testpaths = ["tests"]

[tool.coverage.run]
branch = true
source = ["{{ cookiecutter.package_name }}"]
command_line = "-m pytest"

[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py37,py38,py39
skip_missing_interpreters = true
[testenv]
commands = pytest
extras = dev
"""
12 changes: 2 additions & 10 deletions {{cookiecutter.directory_name}}/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,26 @@ install_requires =
[options.extras_require]
dev =
bump2version
coverage [toml]
prospector[with_pyroma]
isort
pytest
pytest-cov
sphinx
sphinx_rtd_theme
sphinx-autoapi
tox
publishing =
twine
wheel

[options.packages.find]
include = {{ cookiecutter.package_name }}, {{ cookiecutter.package_name }}.*

[coverage:run]
branch = True
source = {{ cookiecutter.package_name }}

[isort]
lines_after_imports = 2
force_single_line = 1
no_lines_before = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
known_first_party = {{ cookiecutter.package_name }}
src_paths = {{ cookiecutter.package_name }},tests
line_length = 120

[tool:pytest]
testpaths = tests
# Note that visual debugger in some editors like pycharm gets confused by coverage calculation.
# As a workaround, configure the test configuration in pycharm et al with a --no-cov argument
addopts = --cov --cov-report xml --cov-report term --cov-report html