Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlummox/3487f953a764522d68d930892614c358 to your computer and use it in GitHub Desktop.
Save phlummox/3487f953a764522d68d930892614c358 to your computer and use it in GitHub Desktop.
Example of the directory structure, setup.py, tox.ini and pytest tests for a new python project

Directory structure

projectname/                 [1]
├── projectname              [2]
│   ├── __init__.py
├── README.md
├── setup.py
├── tests
│   └── test_projectname.py
└── tox.ini

  • These names all match
    • git repository
    • repository directory [1]
    • python project [2]
  • The repository directory contains
    • no __init__.py
    • setup.py
    • tox.ini
    • README.md

setup.py

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()


setuptools.setup(
    name="projectname",
    version="0.0.1",
    author="John Doe",
    author_email='jdoe@example.com',
    description="My short description",
    extras_require={
        "test": [
            "pytest",
            "pytest-cov",
            "pytest-clarity",
            'mock;python_version<"3.3"']
    },
    long_description=long_description,
    long_description_content_type="text/markdown",
    install_requires=["PyYAML", "other_packages"],
    url="https://github.com/gene1wood/projectname",
    packages=setuptools.find_packages(),
    classifiers=[
        "Development Status :: 3 - Alpha",
    ],
)
  • The extra_require field sets a custom target called test. test is not a reserved word
    • This is used in tox.ini to install pytest
  • pytest-clarity improves readability of pytest diff output

tox.ini

[tox]
envlist = py27, py37, flake8

[travis]
python =
    3.7: py37
    2.7: py27

[testenv:flake8]
basepython = python
deps = flake8
commands = flake8 projectname tests setup.py

[testenv]
setenv =
    PYTHONPATH = {toxinidir}
deps = .[test]
commands =
    python -m pytest -vv --cov=projectname {posargs}

test_projectname.py

from projectname import main

def test_projectname():
    assert 1 == 1
  • It's possible to import projectname because we're calling pytest with python -m pytest which adds the repository directory to sys.path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment