Code Monkey home page Code Monkey logo

django-setuptest's Introduction

Django Setuptest

Simple module enabling Django app testing via $ python setup.py test.

Normally when you execute $ python setup.py test for Django related modules you're almost certain to run into DJANGO_SETTINGS_MODULE environment variable issues, e.g.:

ImportError: Settings cannot be imported, because environment variable
DJANGO_SETTINGS_MODULE is undefined.

This module overcomes this by configuring the DJANGO_SETTINGS_MODULE environment variable before executing your test suite. As a bonus it also generates Coverage and PEP 8 reports as part of the test.

  1. Provide a test_suite argument to the setup call specifying the setuptest.setuptest.SetupTestSuite test suite, e.g.:

    setup(
        # ...
        test_suite='setuptest.setuptest.SetupTestSuite',
    )
    

    Alternatively provide a cmdclass test argument to the setup call specifying the setuptest.test command, e.g.:

    from setuptest import test
    
    #...
    
    setup(
        # ...
        cmdclass={'test': test},
    )
    

    This overrides Python's builtin test command to enable the Django testrunner as well as allowing you to pass --failfast as a commandline argument, i.e.:

    $ python setup.py test --failfast
    

    For the cmdclass method to work django-setuptools should be installed and available in your Python path prior to running the test command, in which case django-setuptest is not required to be specified as part of the tests_required argument as detailed next.

  2. Provide a tests_require argument to the setup call including django-setuptest (required only if not already installed) and other package dependencies needed to execute the tests, e.g.:

    setup(
        # ...
        tests_require=(
            'django-setuptest',
        ),
    )
    
  3. Specify your test specific Django settings in a test_settings module in the same path as your app's setup.py. These settings will be used when executing the tests, e.g. in test_settings.py:

    DATABASE_ENGINE = 'sqlite3'
    
    INSTALLED_APPS = (
        'myapp',
    )
    
  4. In order for the test suite to find your tests you must provide either a packages or py_modules argument to the setup call, e.g.:

    from setuptools import setup, find_packages
    
    setup(
        # ...
        packages=find_packages(),
    )
    
    # Or alternatively...
    
    setup(
        # ...
        py_modules=['myapp'],
    )
    

Once correctly configured you can execute tests from the command line:

$ python setup.py test

or, if you want the test suite to stop after the first test failure is detected:

$ python setup.py test --failfast

This should output your test results as well as Coverage and PEP 8 reports.

Note

An XML Coverage report is generated in a file called coverage.xml and a PEP8 report is generated in a file called pep8.txt

To mute the output of the Coverage and PEP 8 reports provide the --quiet option:

$ python setup.py test --quiet

To automatically restart the test runner when code changes are detected (similar to how runserver restarts) provide the --autoreload option:

$ python setup.py test --autoreload

To only run tests for a particular test case specify the test case as the --label option:

$ python setup.py test --label app.TestCase

Or for a particular test method specify the test case's test method as the --label option:

$ python setup.py test --label app.TestCase.test_method

Example output of dummy test including Coverage and PEP 8 reports:

$ python setup.py test
running test
running egg_info
writing django_dummy.egg-info/PKG-INFO
writing top-level names to django_dummy.egg-info/top_level.txt
writing dependency_links to django_dummy.egg-info/dependency_links.txt
reading manifest file 'django_dummy.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'django_dummy.egg-info/SOURCES.txt'
running build_ext
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_something (dummy.tests.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/tmp/django-dummy/dummy/tests/__init__.py", line 6, in test_something
    raise NotImplementedError('Test not implemented. Bad developer!')
NotImplementedError: Test not implemented. Bad developer!

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
Destroying test database for alias 'default'...

Coverage Report:
Name              Stmts   Miss  Cover   Missing
-----------------------------------------------
dummy/models      20      2    90%   22, 55

PEP8 Report:
dummy/tests/__init__.py:6:1: W391 blank line at end of file

$

django-setuptest's People

Contributors

bufke avatar dominicrodger avatar gregmuellegger avatar hedleyroos avatar jezdez avatar jpaidoussi avatar kezabelle avatar lamby avatar mikebryant avatar qoda avatar rizziepit avatar smn avatar stefanfoulis avatar yakky avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.