简体   繁体   English

Django测试manage.py

[英]Django tests manage.py

To run my Django's tests, I was using the manage.py file. 为了运行Django的测试,我使用了manage.py文件。 But I decided to create my own file runtests.py. 但是我决定创建自己的文件runtests.py。

As specified in the Django's doc , the manage.py file will execute all methods whose name is starting like "test" and whose class inherits TestCase ! 根据Django的文档中的指定,manage.py文件将执行所有名称以“ test”开头的方法,并且其类继承TestCase!

My aim is to use this searching method to display all possible tests (not running them). 我的目的是使用这种搜索方法来显示所有可能的测试(而不是运行它们)。 So do you know where I could get this "searching method" ? 那么您知道我在哪里可以得到这种“搜索方法”吗?

Thank you ! 谢谢 !

What you're looking for is called "test discovery", and you don't need to do it manually. 您正在寻找的被称为“测试发现”,您不需要手动进行。

If you want to write a standalone script that runs your tests, you can simply invoke Django's test runner manually from Python code after configuring. 如果要编写一个运行测试的独立脚本,则只需在配置后从Python代码手动调用Django的测试运行程序。 The process for this is: 此过程是:

# First you need settings.
# You can do this by setting DJANGO_SETTINGS_MODULE:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'path.to.your.settings')

# Or you can do it manually:
SETTINGS_DICT = {
    # Put all the settings you'll need in here...
}
from django.conf import settings
settings.configure(**SETTINGS_DICT)

import django
django.setup()

import sys

# Now you can run tests:
from django.test.utils import get_runner
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=2, interactive=True)
failures = test_runner.run_tests(
    ['test_module_to_look_in']
)
sys.exit(bool(failures))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM