pytest.ini
pytest的主配置文件,可以改變pytest的默認(rèn)行為,其中有很多可配置的選項(xiàng)。
1、 可以用pytest --help命令查看
[AppledeMBP-3:~ apple$ pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
general:
-k EXPRESSION only run tests which match the given substring
expression. An expression is a python evaluatable
expression where all names are substring-matched against
test names and their parent classes. Example: -k
'test_method or test_other' matches all test functions
and classes whose name contains 'test_method' or
'test_other', while -k 'not test_method' matches those
that don't contain 'test_method' in their names. -k 'not
test_method and not test_other' will eliminate the
matches. Additionally keywords are matched to classes
and functions containing extra names in their
'extra_keyword_matches' set, as well as functions which
have names assigned directly to them.
-m MARKEXPR only run tests matching given mark expression. example:
-m 'mark1 and not mark2'.
--markers show markers (builtin, plugin and per-project ones).
-x, --exitfirst exit instantly on first error or failed test.
--maxfail=num exit after first num failures or errors.
--strict-markers, --strict
markers not registered in the `markers` section of the
configuration file raise errors.
-c file load configuration from `file` instead of trying to
locate one of the implicit configuration files.
--continue-on-collection-errors
Force test execution even if collection errors occur.
--rootdir=ROOTDIR Define root directory for tests. Can be relative path:
'root_dir', './root_dir', 'root_dir/another_dir/';
absolute path: '/home/user/root_dir'; path with
variables: '$HOME/root_dir'.
--fixtures, --funcargs
show available fixtures, sorted by plugin appearance
(fixtures with leading '_' are only shown with '-v')
--fixtures-per-test show fixtures per test
--import-mode={prepend,append}
prepend/append to sys.path when importing test modules,
default is to prepend.
--pdb start the interactive Python debugger on errors or
KeyboardInterrupt.
--pdbcls=modulename:classname
start a custom interactive Python debugger on errors.
For example:
--pdbcls=IPython.terminal.debugger:TerminalPdb
--trace Immediately break when running each test.
--capture=method per-test capturing method: one of fd|sys|no.
-s shortcut for --capture=no.
--runxfail report the results of xfail tests as if they were not
marked
--lf, --last-failed rerun only the tests that failed at the last run (or all
if none failed)
--ff, --failed-first run all tests but run the last failures first. This may
re-order tests and thus lead to repeated fixture
setup/teardown
--nf, --new-first run tests from new files first, then the rest of the
tests sorted by file mtime
--cache-show=[CACHESHOW]
show cache contents, don't perform collection or tests.
Optional argument: glob (default: '*').
--cache-clear remove all cache contents at start of test run.
--lfnf={all,none}, --last-failed-no-failures={all,none}
which tests to run with no previously (known) failures.
--sw, --stepwise exit on test failure and continue from last failing test
next time
--stepwise-skip ignore the first failing test but stop on the next
failing test
reporting:
--durations=N show N slowest setup/test durations (N=0 for all).
-v, --verbose increase verbosity.
-q, --quiet decrease verbosity.
--verbosity=VERBOSE set verbosity
-r chars show extra test summary info as specified by chars:
(f)ailed, (E)rror, (s)kipped, (x)failed, (X)passed,
(p)assed, (P)assed with output, (a)ll except passed
(p/P), or (A)ll. (w)arnings are enabled by default (see
--disable-warnings).
--disable-warnings, --disable-pytest-warnings
disable warnings summary
-l, --showlocals show locals in tracebacks (disabled by default).
--tb=style traceback print mode (auto/long/short/line/native/no).
--show-capture={no,stdout,stderr,log,all}
Controls how captured stdout/stderr/log is shown on
failed tests. Default is 'all'.
--full-trace don't cut any tracebacks (default is to cut).
--color=color color terminal output (yes/no/auto).
--pastebin=mode send failed|all info to bpaste.net pastebin service.
--junit-xml=path create junit-xml style report file at given path.
--junit-prefix=str prepend prefix to classnames in junit-xml output
--result-log=path DEPRECATED path for machine-readable result log.
collection:
--collect-only only collect tests, don't execute them.
--pyargs try to interpret all arguments as python packages.
--ignore=path ignore path during collection (multi-allowed).
--ignore-glob=path ignore path pattern during collection (multi-allowed).
--deselect=nodeid_prefix
deselect item during collection (multi-allowed).
--confcutdir=dir only load conftest.py's relative to specified dir.
--noconftest Don't load any conftest.py files.
--keep-duplicates Keep duplicate tests.
--collect-in-virtualenv
Don't ignore tests in a local virtualenv directory
--doctest-modules run doctests in all .py modules
--doctest-report={none,cdiff,ndiff,udiff,only_first_failure}
choose another output format for diffs on doctest
failure
--doctest-glob=pat doctests file matching pattern, default: test*.txt
--doctest-ignore-import-errors
ignore doctest ImportErrors
--doctest-continue-on-failure
for a given doctest, continue to run after the first
failure
test session debugging and configuration:
--basetemp=dir base temporary directory for this test run.(warning:
this directory is removed if it exists)
--version display pytest lib version and import information.
-h, --help show help message and configuration info
-p name early-load given plugin module name or entry point
(multi-allowed). To avoid loading of plugins, use the
`no:` prefix, e.g. `no:doctest`.
--trace-config trace considerations of conftest.py files.
--debug store internal tracing debug information in
'pytestdebug.log'.
-o OVERRIDE_INI, --override-ini=OVERRIDE_INI
override ini option with "option=value" style, e.g. `-o
xfail_strict=True -o cache_dir=cache`.
--assert=MODE Control assertion debugging tools. 'plain' performs no
assertion debugging. 'rewrite' (the default) rewrites
assert statements in test modules on import to provide
assert expression information.
--setup-only only setup fixtures, do not execute tests.
--setup-show show setup of fixtures while executing tests.
--setup-plan show what fixtures and tests would be executed but don't
execute anything.
pytest-warnings:
-W PYTHONWARNINGS, --pythonwarnings=PYTHONWARNINGS
set which warnings to report, see -W option of python
itself.
logging:
--no-print-logs disable printing caught logs on failed tests.
--log-level=LOG_LEVEL
logging level used by the logging module
--log-format=LOG_FORMAT
log format as used by the logging module.
--log-date-format=LOG_DATE_FORMAT
log date format as used by the logging module.
--log-cli-level=LOG_CLI_LEVEL
cli logging level.
--log-cli-format=LOG_CLI_FORMAT
log format as used by the logging module.
--log-cli-date-format=LOG_CLI_DATE_FORMAT
log date format as used by the logging module.
--log-file=LOG_FILE path to a file when logging will be written to.
--log-file-level=LOG_FILE_LEVEL
log file logging level.
--log-file-format=LOG_FILE_FORMAT
log format as used by the logging module.
--log-file-date-format=LOG_FILE_DATE_FORMAT
log date format as used by the logging module.
Hypothesis:
--hypothesis-profile=HYPOTHESIS_PROFILE
Load in a registered hypothesis.settings profile
--hypothesis-verbosity={quiet,normal,verbose,debug}
Override profile with verbosity setting specified
--hypothesis-show-statistics
Configure when statistics are printed
--hypothesis-seed=HYPOTHESIS_SEED
Set a seed to use for all Hypothesis tests
pytest-parallel:
--workers=WORKERS Set the max num of workers (aka processes) to start (int
or "auto" - one per core)
--tests-per-worker=TESTS_PER_WORKER
Set the max num of concurrent tests for each worker (int
or "auto" - split evenly)
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
markers (linelist): markers for test functions
empty_parameter_set_mark (string):
default marker for empty parametersets
norecursedirs (args): directory patterns to avoid for recursion
testpaths (args): directories to search for tests when no files or
directories are given in the command line.
usefixtures (args): list of default fixtures to be used with this project
python_files (args): glob-style file patterns for Python test module
discovery
python_classes (args):
prefixes or glob names for Python test class discovery
python_functions (args):
prefixes or glob names for Python test function and
method discovery
disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
disable string escape non-ascii characters, might cause
unwanted side effects(use at your own risk)
console_output_style (string):
console output: "classic", or with additional progress
information ("progress" (percentage) | "count").
xfail_strict (bool): default for the strict parameter of xfail markers when
not given explicitly (default: False)
enable_assertion_pass_hook (bool):
Enables the pytest_assertion_pass hook.Make sure to
delete any previously generated pyc cache files.
junit_suite_name (string):
Test suite name for JUnit report
junit_logging (string):
Write captured log messages to JUnit report: one of
no|system-out|system-err
junit_log_passing_tests (bool):
Capture log information for passing tests to JUnit
report:
junit_duration_report (string):
Duration time to report: one of total|call
junit_family (string):
Emit XML for schema: one of legacy|xunit1|xunit2
doctest_optionflags (args):
option flags for doctests
doctest_encoding (string):
encoding used for doctest files
cache_dir (string): cache directory path.
filterwarnings (linelist):
Each line specifies a pattern for
warnings.filterwarnings. Processed after -W and
--pythonwarnings.
log_print (bool): default value for --no-print-logs
log_level (string): default value for --log-level
log_format (string): default value for --log-format
log_date_format (string):
default value for --log-date-format
log_cli (bool): enable log display during test run (also known as "live
logging").
log_cli_level (string):
default value for --log-cli-level
log_cli_format (string):
default value for --log-cli-format
log_cli_date_format (string):
default value for --log-cli-date-format
log_file (string): default value for --log-file
log_file_level (string):
default value for --log-file-level
log_file_format (string):
default value for --log-file-format
log_file_date_format (string):
default value for --log-file-date-format
faulthandler_timeout (string):
Dump the traceback of all threads if a test takes more
than TIMEOUT seconds to finish. Not available on
Windows.
addopts (args): extra command line options
minversion (string): minimally required pytest version
workers (string): Set the max num of workers (aka processes) to start (int
or "auto" - one per core)
tests_per_worker (string):
Set the max num of concurrent tests for each worker (int
or "auto" - split evenly)
environment variables:
PYTEST_ADDOPTS extra command line options
PYTEST_PLUGINS comma-separated plugins to load during startup
PYTEST_DISABLE_PLUGIN_AUTOLOAD set to disable plugin auto-loading
PYTEST_DEBUG set to enable debug tracing of pytest's internals
to see available markers type: pytest --markers
to see available fixtures type: pytest --fixtures
(shown according to specified file_or_dir or current dir if not specified; fixtures with leading '_' are only shown with the '-v' option
AppledeMBP-3:~ apple$
2、 更改默認(rèn)命令行選項(xiàng)
[pytest]
addopts = -rsxX -l --tb=short --strict
-rsxX表示pytest報(bào)告所有測試用例被跳過、預(yù)計(jì)失敗但實(shí)際通過的原因。
-l表示pytest報(bào)告所有失敗測試的堆棧中的局部變量。
--tb=short表示簡化堆棧回溯信息,只保留文件和行數(shù)。--strict選項(xiàng)表示禁止使用未在配置文件中注冊的標(biāo)記。
3、注冊標(biāo)記來防范拼寫錯(cuò)誤
[pytest]
markers =
smoke: Run the smoke test functions
get: Run the test functions that test task.get()
標(biāo)記注冊后,可以通過命令pytest–markers來查看:
(venv2) AppledeMacBook-Pro-3:tests apple$ pytest --markers
@pytest.mark.smoke: Run the smoke test functions
@pytest.mark.get: Run the test functions that test task.get()
沒有注冊的標(biāo)記不住出現(xiàn)在—markers列表里。如果使用了--strice選項(xiàng),遇到拼寫錯(cuò)誤的標(biāo)記或未注冊的標(biāo)記就會報(bào)錯(cuò)。
4、指定pytest的最低版本號
Minversion選項(xiàng)可以指定運(yùn)行測試用例pytest的最低版本。例如,測試兩個(gè)浮點(diǎn)數(shù)的值是否非常接近,approx()函數(shù)功能直到pytest3.0才出現(xiàn)。為了避免混淆,在使用approx()函數(shù)的項(xiàng)目中增加一行配置
[pytest]
minversion = 3.0
5、指定pytest忽略某些目錄
pytest執(zhí)行測試搜索時(shí),會遞歸遍歷所有子目錄,包括某些你明知道沒必要遍歷的目錄。遇到這種情況,可以使用norecurse選項(xiàng)簡化pytest的搜索工作。norecurse的默認(rèn)設(shè)置是.builddistCVS_darcs{arch}和.egg。因?yàn)橛?*,所有以.開頭的目錄都不會被訪問。如果虛擬環(huán)境命名為venv,那么需要把它加入norecursedirs里。
[pytest]
norecursedirs = .* venv src *.egg dist build
更改一個(gè)已經(jīng)設(shè)置好的值時(shí),可以先找到原來的默認(rèn)值,并把它放在新添加內(nèi)容的后面,就像*.eggdistbuild。
6、指定測試目錄
norecursedirs告訴pytest哪些路徑不用訪問,而testpaths則指示pytest去哪里訪問。Testpaths是一系列相對于根目錄的路徑,用于限定測試用例的搜索范圍。只有在pytest未指定文件目錄參數(shù)或測試用例標(biāo)識符時(shí),該選項(xiàng)才會啟用。集成測試和tox很有用,定義搜索范圍可以節(jié)約測試搜索的時(shí)間。
7、 更改測試搜索的規(guī)則
pytest根據(jù)一定的規(guī)則搜索并允許測試。標(biāo)準(zhǔn)的測試搜索規(guī)則如下:
- 從一個(gè)或多個(gè)目錄開始查找。你可以在命令行指定文件名或目錄名。如果未指定,則使用當(dāng)前目錄。
- 在該目錄和所有子目錄下遞歸查找測試模塊。
- 測試模塊是指文件名為test_.py或_test.py的文件
- 在測試模塊中查找以test_開頭的函數(shù)名。
- 查找名字以Test開頭的類。其中,首先篩選掉包含init函數(shù)的類,再查找類
中以Test_開頭的類方法。
如果把測試類命名為<something>Test或<something>Suite可以通過python_classes, python_files, python_functions更改測試搜索規(guī)則:
[pytest]
python_classes = * Test test* *Suite
8.禁用XPASS
設(shè)置 xfail_strict=true 將會使那些被標(biāo)記為 @pytest.mark.xfail() 但實(shí)際通過的測試用例也被標(biāo)記為失敗。
[pytest]
xfail_strict = true
conftest.py
是本地的插件庫,其中的hook函數(shù)和fixture函數(shù)將作用于該文件所在的目錄以及所有子目錄。
init.py
每個(gè)測試子目錄都包含該文件時(shí),那么在多個(gè)測試目錄中可以出現(xiàn)同名測試文件。
舉例如下:
目錄a和目錄b都有一個(gè)test_foo.py文件,目錄結(jié)構(gòu)如圖:
單獨(dú)運(yùn)行每一個(gè)文件:
讓pytest從dups目錄運(yùn)行:
錯(cuò)誤信息并沒有說明哪里出錯(cuò)了。要解決這個(gè)問題,只需要在2個(gè)子目錄里各添加一個(gè)空的init.py文件,再試著從dups目錄執(zhí)行: