Django 中的测试 | Django 文档 | Django (djangoproject.com)
unittest
模块编写并运行测试 | Django 文档 | Django (djangoproject.com)
测试工具 | Django 文档 | Django (djangoproject.com)
不会使用“实际”(生产)数据库
。 将为测试创建单独的空白数据库
。
临时数据(需要编写相关代码手动的创建符合测试要求(能够达到测试目的的数据,可能是精心设计的数据,未必是随意产生的数据)!)
和临时数据库(自动创建)
来检测api功能python manage.py shell
中的一次性临时测试不同(python shell
的环境时可以访问到正式的数据库
python shell
中不会自动创建临时数据库,它肯定用的时实际环境中的数据库而不是临时数据库(换句话说,和浏览器这类客户端访问的效果时一样的))python manage.py test <test_target>
的运行中情况大为不同,这种情况下会创建空白数据库(这也是为什么前面说,会出现api路由访问成功(譬如200 OK),但是数据内容却是空白数据的情况)> 无论测试是通过还是失败,当所有测试执行完毕后,测试数据库都会被销毁。
test --keepdb
选项来防止测试数据库被破坏。
真实的数据库
,则生产数据可能会污染你的测试。python manage.py test --help
usage: manage.py test [-h] [--noinput] [--failfast] [--testrunner TESTRUNNER]
[-t TOP_LEVEL] [-p PATTERN] [--keepdb]
[--shuffle [SEED]] [-r] [--debug-mode] [-d]
[--parallel [N]] [--tag TAGS]
[--exclude-tag EXCLUDE_TAGS] [--pdb] [-b]
[--no-faulthandler] [--timing] [-k TEST_NAME_PATTERNS]
[--version] [-v {0,1,2,3}] [--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback] [--no-color]
[--force-color]
[test_label ...]
Discover and run tests in the specified modules or the current directory.
positional arguments:
test_label Module paths to test; can be modulename,
modulename.TestCase or modulename.TestCase.test_method
options:
-h, --help show this help message and exit
--noinput, --no-input
Tells Django to NOT prompt the user for input of any
kind.
--failfast Tells Django to stop running the test suite after
first failed test.
--testrunner TESTRUNNER
Tells Django to use specified test runner class
instead of the one specified by the TEST_RUNNER
setting.
-t TOP_LEVEL, --top-level-directory TOP_LEVEL
Top level of project for unittest discovery.
-p PATTERN, --pattern PATTERN
The test matching pattern. Defaults to test*.py.
--keepdb Preserves the test DB between runs.
--shuffle [SEED] Shuffles test case order.
-r, --reverse Reverses test case order.
--debug-mode Sets settings.DEBUG to True.
-d, --debug-sql Prints logged SQL queries on failure.
--parallel [N] Run tests using up to N parallel processes. Use the
value "auto" to run one test process for each
processor core.
--tag TAGS Run only tests with the specified tag. Can be used
multiple times.
--exclude-tag EXCLUDE_TAGS
Do not run tests with the specified tag. Can be used
multiple times.
--pdb Runs a debugger (pdb, or ipdb if installed) on error
or failure.
-b, --buffer Discard output from passing tests.
--no-faulthandler Disables the Python faulthandler module during tests.
--timing Output timings, including database set up and total
run time.
-k TEST_NAME_PATTERNS
Only run test methods and classes that match the
pattern or substring. Can be used multiple times. Same
as unittest -k option.
--version Show program's version number and exit.
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on CommandError exceptions.
--no-color Don't colorize the command output.
--force-color Force colorization of the command output.
pmg test word.test_dict.DictTestCase --keepdb --verbosity 2
pmg=python manage.py
word.test_dict.DictTestCase
表示被测试的目标(这里是一个整个类)--verbosity 2
指定需要显示的测试过程信息PS D:\repos\ELA\backEnd\ela> pmg test word.test_dict.DictTestCase --keepdb --verbosity 2
Found 5 test(s).
Using existing test database for alias 'default' ('test_ela4')...
Operations to perform:
Synchronize unmigrated apps: coreapi, django_filters, drf_yasg, messages, rest_framework, staticfiles
Apply all migrations: admin, auth, contenttypes, polls, scoreImprover, sessions, user, word
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
Applying user.0019_alter_user_examdate_alter_user_name_and_more... OK
System check identified no issues (0 silenced).
test_demo (word.test_dict.DictTestCase) ... ['apple', 'ˈæp(ə)l', 'apples', 'NULL', 'NULL', 'NULL', 'NULL', "['n. 苹果']"]
@test_url /word/test/
{'spelling': 'apple', 'phnetic': 'ˈæp(ə)l', 'explains': "['n. 苹果']"}
@res.type: <class 'dict'="">
@res <response status_code="200," "application="" json"="">
@res.data {'spelling': 'apple', 'phnetic': 'ˈæp(ə)l', 'explains': "['n. 苹果']"}
ok
test_false_is_true (word.test_dict.DictTestCase) ... Method: test_false_is_true.
ok
test_no_explain (word.test_dict.DictTestCase) ... ok
test_one_plus_one_equals_two (word.test_dict.DictTestCase) ... Method: test_one_plus_one_equals_two.
ok
test_reverse (word.test_dict.DictTestCase) ... @type_res: <class 'rest_framework.response.response'="">
@res.content {"detail":"Not found."}
@type_res_list: <class 'django.http.response.httpresponsepermanentredirect'="">
@res.content_list
ok
----------------------------------------------------------------------
Ran 5 tests in 1.358s
OK
Preserving test database for alias 'default' ('test_ela4')...
PS D:\repos\ELA\backEnd\ela> pmg test word.test_dict.DictTestCase --keepdb --verbosity 3
Found 5 test(s).
Using existing test database for alias 'default' ('test_ela4')...
Operations to perform:
Synchronize unmigrated apps: coreapi, django_filters, drf_yasg, messages, rest_framework, staticfiles
Apply all migrations: admin, auth, contenttypes, polls, scoreImprover, sessions, user, word
Running pre-migrate handlers for application main
Running pre-migrate handlers for application scoreImprover
Running pre-migrate handlers for application word
Running pre-migrate handlers for application user
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application polls
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
No migrations to apply.
Running post-migrate handlers for application main
Running post-migrate handlers for application scoreImprover
Running post-migrate handlers for application word
Running post-migrate handlers for application user
Running post-migrate handlers for application admin
Running post-migrate handlers for application polls
Running post-migrate handlers for application auth
Running post-migrate handlers for application contenttypes
Running post-migrate handlers for application sessions
System check identified no issues (0 silenced).
test_demo (word.test_dict.DictTestCase) ... ['apple', 'ˈæp(ə)l', 'apples', 'NULL', 'NULL', 'NULL', 'NULL', "['n. 苹果']"]
@test_url /word/test/
{'spelling': 'apple', 'phnetic': 'ˈæp(ə)l', 'explains': "['n. 苹果']"}
@res.type: <class 'dict'="">
@res <response status_code="200," "application="" json"="">
@res.data {'spelling': 'apple', 'phnetic': 'ˈæp(ə)l', 'explains': "['n. 苹果']"}
ok
test_false_is_true (word.test_dict.DictTestCase) ... Method: test_false_is_true.
ok
test_no_explain (word.test_dict.DictTestCase) ... ok
test_one_plus_one_equals_two (word.test_dict.DictTestCase) ... Method: test_one_plus_one_equals_two.
ok
test_reverse (word.test_dict.DictTestCase) ... @type_res: <class 'rest_framework.response.response'="">
@res.content {"detail":"Not found."}
@type_res_list: <class 'django.http.response.httpresponsepermanentredirect'="">
@res.content_list
ok
----------------------------------------------------------------------
Ran 5 tests in 1.278s
OK
Preserving test database for alias 'default' ('test_ela4')...
PS D:\repos\ELA\backEnd\ela>
setUpTestData()
用于类级别设置,在测试运行开始的时侯,会调用一次。您可以使用它来创建在任何测试方法
中,都不会修改或更改的对象
。setUp()
在每个测试函数之前
被调用,以设置可能被测试修改的
任何对象(每个测试函数,都将获得这些对象的 “新” 版本)