Testing cookiecutter templates with pytest

I added some unit tests to my datasette-plugin cookiecutter template today, since the latest features involved adding a hooks/post_gen_project.py script.

Here's the full test script I wrote. It lives in tests/test_cookiecutter_template.py in the root of the repository.

To run the tests I have to use pytest tests because running just pytest gets confused when it tries to run the templated tests that form part of the cookiecutter template.

The pattern I'm using looks like this:

from cookiecutter.main import cookiecutter
import pathlib

TEMPLATE_DIRECTORY = str(pathlib.Path(__file__).parent.parent)

def test_static_and_templates(tmpdir):
            "plugin_name": "foo",
            "description": "blah",
            "include_templates_directory": "y",
            "include_static_directory": "y",
    assert paths(tmpdir) == {
    setup_py = (tmpdir / "datasette-foo" / "setup.py").read_text("utf-8")
    assert (
        'package_data={\n        "datasette_foo": ["static/*", "templates/*"]\n    }'
    ) in setup_py

def paths(directory):
    paths = list(pathlib.Path(directory).glob("**/*"))
    paths = [r.relative_to(directory) for r in paths]
    return {str(f) for f in paths if str(f) != "."}

Created 2021-01-27T15:50:02-08:00, updated 2021-01-27T15:58:29-08:00 · History · Edit