使用setup.py test和tox运行Django测试

时间:2013-10-28 11:11:05

标签: python django unit-testing setuptools tox

我构建了一个Django应用程序并使用setuptools创建了一个包。现在,我想做以下事情:

  1. 我想用python setup.py test运行所有测试。但是当我发出这个命令时,我得到了:

    /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. 我想使用Tox来运行我的测试,但我不知道应该在command属性中写什么来运行我的Django app测试。

1 个答案:

答案 0 :(得分:3)

setup.py

test_suite = "test_project.runtests.runtests",

然后在你的项目中:

#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()

请参阅this tutorial by Eric Holscher.