Skip to main content

A Pytest plugin for your Cookiecutter templates

Project description

Join the chat at https://gitter.im/hackebrot/pytest-cookies PyPI Package PyPI Python Versions PyPI Package License Documentation Status See Build Status on Travis CI See Build Status on AppVeyor

pytest is a mature full-featured Python testing tool that provides easy, no boilerplate testing. Its hook-based customization system supports integration of external plugins such as pytest-cookies.

This plugin comes with a cookies fixture which is a wrapper for the cookiecutter API for generating projects. It helps you verify that your template is working as expected and takes care of cleaning up after running the tests.

Cookiecutter

Installation

pytest-cookies is available for download from PyPI via pip:

$ pip install pytest-cookies

It will automatically install pytest along with cookiecutter.

Features

cookies.bake() returns a result instance with a bunch of fields that hold useful information:

  • exit_code: is the exit code of cookiecutter, 0 means successful termination

  • exception: is the exception that happened (if one did, None otherwise)

  • project: a py.path.local object pointing to the rendered project

The returned LocalPath instance provides you with a powerful interface to filesystem related information, that comes in handy for validating the generated project layout and file contents:

def test_readme(cookies):
    result = cookies.bake()

    readme_file = result.project.join('README.rst')
    readme_lines = readme_file.readlines(cr=False)
    assert readme_lines == ['helloworld', '==========']

Usage

Generate a new project

The cookies.bake() method generates a new project from your template based on the default values specified in cookiecutter.json:

def test_bake_project(cookies):
    result = cookies.bake(extra_context={'repo_name': 'helloworld'})

    assert result.exit_code == 0
    assert result.exception is None
    assert result.project.basename == 'helloworld'
    assert result.project.isdir()

It accepts the extra_context keyword argument that will be passed to cookiecutter. The given dictionary will override the default values of the template context, allowing you to test arbitrary user input data.

Please see the Injecting Extra Context section of the official cookiecutter documentation.

Specify template directory

By default, cookies.bake looks for a cookiecutter template in the current directory. This can be overridden on the command line by passing a --template parameter to pytest:

$ pytest --template TEMPLATE

To customize the cookiecutter template directory in a test, cookies.bake() can also accept an optional template parameter:

@pytest.fixture
def custom_template(tmpdir):
    template = tmpdir.ensure('cookiecutter-template', dir=True)
    template.join('cookiecutter.json').write(
        '{"repo_name": "example-project"}'
    )

    repo_dir = template.ensure('{{cookiecutter.repo_name}}', dir=True)
    repo_dir.join('README.rst').write('{{cookiecutter.repo_name}}')

    return template


def test_bake_custom_project(cookies, custom_template):
    """Test for 'cookiecutter-template'."""
    result = cookies.bake(template=str(custom_template))

    assert result.exit_code == 0
    assert result.exception is None
    assert result.project.basename == 'example-project'
    assert result.project.isdir()

Issues

If you encounter any problems, please file an issue along with a detailed description.

Contributing

Contributions are very welcome! Tests can be run with tox, please make sure all of the tests are green before you submit a pull request.

Code of Conduct

Everyone interacting in the pytest-cookies project’s codebases, issue trackers, chat rooms, and mailing lists is expected to follow the PyPA Code of Conduct.

License

Distributed under the terms of the MIT license, Pytest-Cookies is free and open source software.

OSI certified

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-cookies-0.3.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pytest_cookies-0.3.0-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-cookies-0.3.0.tar.gz.

File metadata

File hashes

Hashes for pytest-cookies-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7054b5abd87704c3769b46deb65c332fadba80905d4103b0b1e0b4fe8a9a852a
MD5 5fba6c6248f15d02088a4bf18835d8c9
BLAKE2b-256 e6055f548c4cf973e22c7196e0b49d491af687758404d7f9886717864ccafc32

See more details on using hashes here.

File details

Details for the file pytest_cookies-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_cookies-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a6f37393b697eb3357625a5c226888d9dd020cca3d112b38c630467ce9ff7844
MD5 95959e46d306a611937ecb9579440069
BLAKE2b-256 68a20172b87baf2bd39b3794c9fd6b3ff1a74f482f826d0dcf69120f4d6a7f59

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page