Pytest系列14 -配置文件pytest.ini的詳細使用

一、前言

pytest配置文件可以改變pytest的運行方式,它是一個固定的文件pytest.ini文件,讀取配置信息,按指定的方式去運行

二、非test文件

pytest里面有些文件是非test文件

  • pytest.ini:pytest的主配置文件,可以改變pytest的默認行為
  • conftest.py:測試用例的一些fixture配置
  • init.py:識別該文件夾為python的package包
pytest.ini配置選項

cmd執行:
pytest --help

找到這部分內容

[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|log|system-out|system-err|out-err|all
  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/--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
  log_auto_indent (string):
                        default value for --log-auto-indent
  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
  rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.
  rsyncignore (pathlist):
                        list of (relative) glob-style paths to be ignored for rsyncing.
  looponfailroots (pathlist):
                        directories to check for changes

pytest.ini文件放在哪里?

就放在項目根目錄下 ,不要亂放,不要亂起其他名字

常用配置項

1.marks
作用:測試用例中添加了 @pytest.mark.webtest 裝飾器,如果不添加marks選項的話,就會報warnings
格式:list列表類型
寫法:

[pytest]
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

2.xfail_strict
作用:設置xfail_strict = True可以讓那些標記為@pytest.mark.xfail但實際通過顯示XPASS的測試用例被報告為失敗
格式:True 、False(默認),1、0

寫法:

[pytest]

# mark標記說明
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

具體代碼栗子

未設置 xfail_strict = True 時,測試結果顯示XPASS

@pytest.mark.xfail()
def test_case1():
    a = "a"
    b = "b"
    assert a != b
圖片.png

已設置 xfail_strict = True 時,測試結果顯示failed


圖片.png

3.addopts
作用:addopts參數可以更改默認命令行選項,這個當我們在cmd輸入一堆指令去執行用例的時候,就可以用該參數代替了,省去重復性的敲命令工作
比如:想測試完生成報告,失敗重跑兩次,一共運行兩次,通過分布式去測試,如果在cmd中寫的話,命令會很長

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

每次都這樣敲不太現實,addopts就可以完美解決這個問題

[pytest]

# mark
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

# 命令行參數
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

加了addopts之后,我們在cmd中只需要敲pytest就可以生效了!!

4.log_cli
作用:控制臺實時輸出日志
格式:log_cli=True 或False(默認),或者log_cli=1 或 0


圖片.png

圖片.png

圖片.png

5.norecursedirs
作用:pytest 收集測試用例時,會遞歸遍歷所有子目錄,包括某些你明知道沒必要遍歷的目錄,遇到這種情況,可以使用 norecursedirs 參數簡化 pytest 的搜索工作【還是挺有用的!!!】
默認設置: norecursedirs = .* build dist CVS _darcs {arch} *.egg
正確寫法:多個路徑用空格隔開

[pytest]

norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util
更改測試用例收集規則

pytest默認的測試用例收集規則

  • 文件名以 test_*.py 文件和 *_test.py
  • 以 test_ 開頭的函數
  • 以 Test 開頭的類,不能包含 init 方法
  • 以 test_ 開頭的類里面的方法

我們是可以修改或者添加這個用例收集規則的;當然啦,是建議在原有的規則上添加的,如下配置

[pytest]

python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*

pytest的ini配置文件

pytest.ini文件是pytest的主配置文件,可以改變pytest的默認行為。
1.pytest.ini的放置位置:一般放在項目工程的根目錄(即當前項目的頂級文件夾下)
2.pytest.ini的作用:指定pytest的運行方式(在cmd輸入pytest后,會讀取pytest.ini中的配置信息,按指定的方式去運行)
3.cmd下使用 pytest -h 命令查看pytest.ini的設置選項(以下截圖只是部分選項)
常用設置選項如下:
[pytest]
addopts = -s … #可添加多個命令行參數,用空格分隔
testpaths = …/pytestproject #測試用例文件夾,可自己配置,…/pytestproject為上一層的pytestproject文件夾。
python_files = test.py #配置測試搜索的模塊文件名稱
python_classes = Test
#配置測試搜索的測試類名
python_funtions = test #配置測試搜索的測試函數名

示例如下:

[pytest]
addopts = -s --html=./report.html
testpaths = …/pytestproject
python_files = test*.py
python_classes = Test*
python_funtions = test*

注意:
1.運行的時候自動讀取配置文件,運行pytestproject下的所有test開頭的模塊文件。


參考鏈接
https://www.cnblogs.com/poloyy/p/12702294.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,106評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,441評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,211評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,736評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,475評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,834評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,829評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,009評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,559評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,306評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,516評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,038評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,728評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,132評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,443評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,249評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,484評論 2 379

推薦閱讀更多精彩內容