9

I built a Django app and made a package out of it with setuptools. Now, I would like to do the following things:

  1. I would like to run all tests with python setup.py test. But when I issue this command, I get:

    /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'install_requires'
    warnings.warn(msg)
    usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
    or: setup.py --help [cmd1 cmd2 ...]
    or: setup.py --help-commands
    or: setup.py cmd --help
    
    error: invalid command 'test'
    
  2. I would like to use Tox to run my tests, but I have no idea what should I write in the command attribute to run my Django app tests.

linkyndy
  • 17,038
  • 20
  • 114
  • 194

1 Answers1

4

in your setup.py:

test_suite = "test_project.runtests.runtests",

And then in your project:

#This file mainly exists to allow python setup.py test to work.
import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_project.settings'
test_dir = os.path.dirname(__file__)
sys.path.insert(0, test_dir)

from django.test.utils import get_runner
from django.conf import settings

def runtests():
    test_runner = get_runner(settings)
    failures = test_runner([], verbosity=1, interactive=True)
    sys.exit(failures)

if __name__ == '__main__':
    runtests()

See this tutorial by Eric Holscher.

oz123
  • 27,559
  • 27
  • 125
  • 187
  • Eric Holscher's tutorial is out of date - see http://gremu.net/blog/2010/enable-setuppy-test-your-django-apps/ – jwhitlock Jul 28 '14 at 22:31
  • @jwhitlock your link is broken – Saeed Nov 17 '15 at 19:48
  • Yes, link is broken, but it's not my blog. I've gone back to @Oz123's method for my own projects. – jwhitlock Dec 10 '15 at 21:39
  • 1
    Here's a link to the archived version of the page @jwhitlock posted: http://web.archive.org/web/20121010073752/http://gremu.net/blog/2010/enable-setuppy-test-your-django-apps/ – Phil Gyford Apr 25 '16 at 10:27
  • See the official Django documentation for similar code: https://docs.djangoproject.com/en/1.11/topics/testing/advanced/ – Langston Jun 23 '17 at 19:43