pytest.ini文件是pytest的主配置文件,可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。pytest.ini的位置:一般放在项目工程的根目录(即当前项目的顶级文件夹下)
cmd下使用 pytest -h 命令查看pytest.ini的设置选项
-
[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
-
-
-
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
-
addopts配置
addopts参数可以更改默认命令行选项,可以将一些命令添加到pytest.ini里就不需要每次命令行执行时都带上了,默认就会以pytest.ini里配置去运行,多个命令行参数用空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数
-
[pytest]
-
addopts = -v -reruns
1 --html=../report/report.html
当pytest.ini未配置addopts = -v时,py文件里执行pytest.main(["test_001_rights.py"])或cmd下执行pytest,未带-v,执行结果不会有详细信息,需带上-v才会有详细信息,如果我们想执行时不带-v还能有详细信息,这时就需要在pytest.ini里配置addopts = -v
-
# py文件里执行pytest.main([
"test_001_rights.py"])
-
-
-
if __name__ ==
"__main__":
-
pytest.main([
"test_001_rights.py"])
-
-
"C:\Program Files\Python35\python.exe" C:
/Users/wangli/Desktop/InterfaceAutoPytest/test_case/srzp/test_001_rights.py
-
============================= test session starts =============================
-
platform win32 -- Python
3.5
.2, pytest
-5.1
.2, py
-1.8
.0, pluggy
-0.12
.0
-
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini
-
plugins: allure-pytest
-2.8
.5, html
-1.22
.0, metadata
-1.8
.0, rerunfailures
-8.0
-
collected
2 items
-
-
-
test_001_rights.py .. [
100%]
-
-
-
==============================
2 passed
in
3.21s ==============================
-
-
-
Process finished
with exit code
0
-
-
-
-
-
# cmd下执行pytest
-
C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>pytest
-
=============================================== test session starts ================================================
-
platform win32 -- Python
3.5
.2, pytest
-5.1
.2, py
-1.8
.0, pluggy
-0.12
.0
-
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini
-
plugins: allure-pytest
-2.8
.5, html
-1.22
.0, metadata
-1.8
.0, rerunfailures
-8.0
-
collected
2 items
-
-
-
test_001_rights.py .. [
100%]
-
-
-
================================================
2 passed
in
2.40s =================================================
-
-
-
C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>
-
-
-
-
当pytest.ini已配置addopts = -v时,py文件里执行pytest.main(["test_001_rights.py"])或cmd下执行pytest,未带-v,就会以配置的方式去执行,结果有详细的信息
-
# py文件里执行pytest.main([
"test_001_rights.py"])
-
-
-
if __name__ ==
"__main__":
-
pytest.main([
"test_001_rights.py"])
-
-
"C:\Program Files\Python35\python.exe" C:
/Users/wangli/Desktop/InterfaceAutoPytest/test_case/srzp/test_001_rights.py
-
============================= test session starts =============================
-
platform win32 -- Python
3.5
.2, pytest
-5.1
.2, py
-1.8
.0, pluggy
-0.12
.0 -- C:\Program Files\Python35\python.exe
-
cachedir: .pytest_cache
-
metadata: {
'JAVA_HOME':
'C:\\Program Files\\Java\\jdk1.8.0_101',
'Packages': {
'py':
'1.8.0',
'pluggy':
'0.12.0',
'pytest':
'5.1.2'},
'Platform':
'Windows-10-10.0.18362-SP0',
'Plugins': {
'rerunfailures':
'8.0',
'html':
'1.22.0',
'metadata':
'1.8.0',
'allure-pytest':
'2.8.5'},
'Python':
'3.5.2'}
-
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini
-
plugins: allure-pytest
-2.8
.5, html
-1.22
.0, metadata
-1.8
.0, rerunfailures
-8.0
-
collecting ... collected
2 items
-
-
-
test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\
1.0\business\
class\list?page=
1&page_size=
10-None0] PASSED [
50%]
-
test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\
1.0\business\
class\list?page=
1&page_size=
10-None1] PASSED [
100%]
-
-
-
==============================
2 passed
in
2.52s ==============================
-
-
-
Process finished
with exit code
0
-
-
-
# cmd下执行pytest
-
C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>pytest
-
=============================================== test session starts ================================================
-
platform win32 -- Python
3.5
.2, pytest
-5.1
.2, py
-1.8
.0, pluggy
-0.12
.0 -- c:\program files\python35\python.exe
-
cachedir: .pytest_cache
-
metadata: {
'Platform':
'Windows-10-10.0.18362-SP0',
'JAVA_HOME':
'C:\\Program Files\\Java\\jdk1.8.0_101',
'Packages':
-
{
'py':
'1.8.0',
'pytest':
'5.1.2',
'pluggy':
'0.12.0'},
'Plugins': {
'html':
'1.22.0',
'allure-pytest':
'2.8.5',
'rer
-
unfailures':
'8.0',
'metadata':
'1.8.0'},
'Python':
'3.5.2'}
-
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini
-
plugins: allure-pytest
-2.8
.5, html
-1.22
.0, metadata
-1.8
.0, rerunfailures
-8.0
-
collected
2 items
-
-
-
test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\
1.0\business\
class\list?page=
1&pa
-
ge_size=
10-None0] PASSED [
50%]
-
test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\
1.0\business\
class\list?page=
1&pa
-
ge_size=
10-None1] PASSED [
100%]
-
-
-
================================================
2 passed
in
3.48s =================================================
-
-
-
-
testpaths配置
pytest默认是搜索执行当前目录下的所有用例,当pytest.ini配置了testpaths = test_case/lxk或testpaths = test_case/lxk/test_001_case.py就会只执行当前配置的文件夹下或文件里的用例,这样我们就可以灵活的控制运行需要测试的用例了,可配置多个,空格隔开
项目目录:
当pytest.ini未配置testpaths时,会按pytest默认搜索执行方式,run_all_case.py里执行pytest.main() lxk和srzp文件夹下的用例都执行了
-
-
-
"C:\Program Files\Python35\python.exe"
C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
-
============================= test session starts =============================
-
platform win32 -- Python
3.5.
2, pytest-
5.1.
2, py-
1.8.
0, pluggy-
0.
12.0 --
C:\Program Files\Python35\python.exe
-
cachedir: .pytest_cache
-
metadata: {
'Python':
'3.5.2',
'Packages': {
'pytest':
'5.1.2',
'pluggy':
'0.12.0',
'py':
'1.8.0'},
'JAVA_HOME':
'C:\\Program Files\\Java\\jdk1.8.0_101',
'Platform':
'Windows-10-10.0.18362-SP0',
'Plugins': {
'allure-pytest':
'2.8.5',
'metadata':
'1.8.0',
'rerunfailures':
'8.0',
'html':
'1.22.0'}}
-
rootdir:
C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini
-
plugins: allure-pytest-
2.8.
5, html-
1.22.
0, metadata-
1.8.
0, rerunfailures-
8.0
-
collecting ... collected
8 items
-
-
-
test_case/lxk/test_001_case.py::Test::test_001 PASSED [
12%]
-
test_case/lxk/test_001_case.py::Test::test_002 PASSED [
25%]
-
test_case/lxk/test_002_case.py::Test::test_001 PASSED [
37%]
-
test_case/lxk/test_002_case.py::Test::test_002 PASSED [
50%]
-
test_case/srzp/test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u621
0\u529f-get-
/1.0/business
/class/list?page=
1&page_size=
10-None
0] PASSED [
62%]
-
test_case/srzp/test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u621
0\u529f-get-
/1.0/business
/class/list?page=
1&page_size=
10-None1] PASSED [
75%]
-
test_case/srzp/test_002_case.py::Test::test_001_login PASSED [
87%]
-
test_case/srzp/test_002_case.py::Test::test_002_order PASSED [
100%]
-
-
-
============================== warnings summary ===============================
-
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.
py:
324
-
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.
py:
324:
PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo? You can register custom marks to avoid this warning -
for details, see
https:/
/docs.pytest.org/en
/latest/mark.html
-
PytestUnknownMarkWarning,
-
-
-
--
Docs:
https:/
/docs.pytest.org/en
/latest/warnings.html
-
========================
8 passed,
1 warnings
in
1.80s ========================
-
-
-
Process finished with exit code
0
当pytest.ini已配置testpaths = test_case/lxk,run_all_case.py里执行pytest.main() 只会运行lxk文件夹下的用例
-
"C:\Program Files\Python35\python.exe"
C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
-
============================= test session starts =============================
-
platform win32 -- Python
3.5.
2, pytest-
5.1.
2, py-
1.8.
0, pluggy-
0.
12.0 --
C:\Program Files\Python35\python.exe
-
cachedir: .pytest_cache
-
metadata: {
'Plugins': {
'html':
'1.22.0',
'allure-pytest':
'2.8.5',
'metadata':
'1.8.0',
'rerunfailures':
'8.0'},
'Packages': {
'pytest':
'5.1.2',
'py':
'1.8.0',
'pluggy':
'0.12.0'},
'JAVA_HOME':
'C:\\Program Files\\Java\\jdk1.8.0_101',
'Python':
'3.5.2',
'Platform':
'Windows-10-10.0.18362-SP0'}
-
rootdir:
C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini,
testpaths: test_case/lxk
-
plugins: allure-pytest-
2.8.
5, html-
1.22.
0, metadata-
1.8.
0, rerunfailures-
8.0
-
collecting ... collected
4 items
-
-
-
test_case/lxk/test_001_case.py::Test::test_001 PASSED [
25%]
-
test_case/lxk/test_001_case.py::Test::test_002 PASSED [
50%]
-
test_case/lxk/test_002_case.py::Test::test_001 PASSED [
75%]
-
test_case/lxk/test_002_case.py::Test::test_002 PASSED [
100%]
-
-
-
============================== warnings summary ===============================
-
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.
py:
324
-
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.
py:
324:
PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo? You can register custom marks to avoid this warning -
for details, see
https:/
/docs.pytest.org/en
/latest/mark.html
-
PytestUnknownMarkWarning,
-
-
-
--
Docs:
https:/
/docs.pytest.org/en
/latest/warnings.html
-
========================
4 passed,
1 warnings
in
0.
07s ========================
-
-
-
Process finished with exit code
0
norecursedirs配置
pytest.ini配置norecursedirs= lxk test.py 不搜索执行对应文件夹下或文件下的用例,和testpaths配置完全相反的效果,可配置多个,空格隔开
python_files (args)配置
pytest默认是匹配test_*.py、 *_test.py文件,如果配置python_files = smoke.py 匹配 python 用例文件, 如smoke_*.py、 *_smoke.py 可配置多个,空格隔开
目录:
run_all_case运行pytest.main()可以看到只运行了smoke.py文件里的用例
-
"C:\Program Files\Python35\python.exe"
C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
-
============================= test session starts =============================
-
platform win32 -- Python
3.5.
2, pytest-
5.1.
2, py-
1.8.
0, pluggy-
0.
12.0 --
C:\Program Files\Python35\python.exe
-
cachedir: .pytest_cache
-
metadata: {
'Platform':
'Windows-10-10.0.18362-SP0',
'JAVA_HOME':
'C:\\Program Files\\Java\\jdk1.8.0_101',
'Packages': {
'pytest':
'5.1.2',
'py':
'1.8.0',
'pluggy':
'0.12.0'},
'Python':
'3.5.2',
'Plugins': {
'metadata':
'1.8.0',
'rerunfailures':
'8.0',
'allure-pytest':
'2.8.5',
'html':
'1.22.0'}}
-
rootdir:
C:\Users\wangli\Desktop\InterfaceAutoPytest,
inifile: pytest.ini
-
plugins: allure-pytest-
2.8.
5, html-
1.22.
0, metadata-
1.8.
0, rerunfailures-
8.0
-
collecting ... collected
2 items
-
-
-
test_case/lxk/smoke.py::Test::test_001 PASSED [
50%]
-
test_case/lxk/smoke.py::Test::test_002 PASSED [
100%]
-
-
-
============================== warnings summary ===============================
-
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.
py:
324
-
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.
py:
324:
PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo? You can register custom marks to avoid this warning -
for details, see
https:/
/docs.pytest.org/en
/latest/mark.html
-
PytestUnknownMarkWarning,
-
-
-
--
Docs:
https:/
/docs.pytest.org/en
/latest/warnings.html
-
========================
2 passed,
1 warnings
in
0.
17s ========================
-
-
-
Process finished with exit code
0
python_classes (args)配置
配置python_classes =Test* 匹配class 类名称 如Test*,可配置多个,空格隔开,和python_files (args)方法配置类似
python_functions (args)配置
配置python_functions = test_* 匹配函数和class里面方法 如test_*可配置多个,空格隔开,和python_files (args)方法配置类似
转载:https://blog.csdn.net/qq_36502272/article/details/108557806