前言
pytest配置文件能夠改變pytest框架代碼的運行規則。比如修改pytest收集用例的規則,添加命令行參數等等!下面我們來一一講解常用的一些配置項
Help
通過命令pytest --help查看配置文件中可以添加的一些參數及選項,這些選項都是可以添加到pytest的配置文件的
[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 dire console_output_style (string) console output: classic or with additional progr usefixtures (args) list of default fixtures to be used with this project python_files (args) glob-style file patterns for Python test module disco python_classes (args) prefixes or glob names for Python test class discover python_functions (args) prefixes or glob names for Python test function and m disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool) di xfail_strict (bool) default for the strict parameter of xfail markers whe junit_suite_name (string) Test suite name for JUnit report junit_logging (string) Write captured log messages to JUnit report: one of n 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.filterwar 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 "li 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 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
File
通常情況下我們會把配置文件放到我們的項目根目錄下 命名為 pytest.ini, 項目在運行時會首先按照配置文件中設置的參數選項來運行,其次再遵守pytest的默認規則
使用
添加默認參數選項
我們都知道pytest可以在cmd中使用命令行運行腳本,通常是這樣的 pytest -vqs 腳本, 我們通過這個命令來運行一下腳本,大概輸出運行信息就是下面這種形式
D:\PytestAutoTestFrameWork\TestCases>pytest -vqs test_loginCase.py ============================= test session starts ============================= platform win32 -- Python 3.6.4, pytest-4.4.1, py-1.6.0, pluggy-0.9.0 rootdir: D:\PytestAutoTestFrameWork, inifile: pytest.ini plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0, allure-pytest-2.6.2 collected 4 items test_loginCase.py ------------open browser------------ -------staring login------- info: string upload url "https://mail.126.com" info:switching to iframe "//div[@id="loginDiv"]/iframe" info:clearing value [Info:Starting find the element "//input[@name="email"]" by "xpath"!] info:input "linuxxiaochao" [Info:Starting find the element "//input[@name="email"]" by "xpath"!] info:clearing value [Info:Starting find the element "//input[@name="password"]" by "xpath"!] info:input "xiaochao11520" [Info:Starting find the element "//input[@name="password"]" by "xpath"!] info:click "//a[@id="dologin"]" info:switch back to default iframe ---------end login---------
現在們在我們的項目根目錄下新建pytest.ini文件並輸入下面的選項
[pytest]
addopts=-vqs
我們再次運行腳本僅通過命令pytest即可,這時候一會發現輸出信息和上面一摸一樣,這就是配置文件的作用
添加用例路徑
[pytest]
testpaths=./TestCases
通過這樣一項設置,我們可以把我們用例所在的目錄添加到配置文件,這樣我們在運行用例的時候,pytest會直接在配置文件所在的目錄搜索用例
指定pytest的版本
[pytest]
minversion = 3.0
指定pytest忽略哪些搜索目錄
[pytest] norecursedirs = .* venv src *.egg dist build
標紅部分為系統默認不會搜索的路徑,前面是用戶自定義的路徑,注:當自定義時最好把系統默認的添加到后面
修改pytest收集用例的規則-測試類
pytest默認會搜索以test_開頭/以_test結尾的文件,以Test開頭的類,且以test_開頭的測試函數為測試用例
我們希望pytest能夠搜索以Test*開頭,*Test結尾或者*Suite開頭或結尾的類名字,我們需要添加這樣的選項
[pytest]
python_classes = *Test Test* *Suite
修改pytest收集用例規則-修改測試模塊
我們希望pytest可以搜索以check_*開頭的文件
[pytest] python_files=test_* *_test check_*
修改pytest收集用例規則-修改測試方法
我們希望可以搜索以check_* 開頭的測試函數為測試用例
[pytest]
python_functions = test_* *_test check_*
添加生成報告選項
[pytest]
addopts = -v --rerun 1 --html=report.html --self-contained-html
添加mark標記
通常我們在編寫測試用例的時候,會在用例上回添加一些標記來記錄哪些用例需要運行,哪些需要跳過,哪些需要標記為failed等等,那么難免我們記不住這些標記,我們可以通過配置文件記錄這些標記
[pytest] markers= loginTest: Run login test cases contactTest: Run add contact test cases sendMailTest: Run send mail test cases
我們添加完這些標記之后,可以通過 pytest --markers 查看我們自定義的mark
D:\PytestAutoTestFrameWork\TestCases>pytest --markers @pytest.mark.loginTest: Run login test cases @pytest.mark.contactTest: Run add contact test cases @pytest.mark.sendMailTest: Run send mail test cases @pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between re-runs. @pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.
最后
pytest看的差不多了,其實小項目也寫完了,一直沒時間整理,也沒時間寫到博客上來,快51了,爭取這幾天發出來!