1.assert
- assert xx 判斷xx為真
- assert not xx 判斷xx不為真
- assert a in b 判斷b包含a
- assert a == b 判斷a等於b
- assert a != b 判斷a不等於b
2.pytest.ini
cmd執行:pytest --help #查看pytest.ini
pytest詳細使用:https://cloud.tencent.com/developer/article/1640840
"""
pytest測試:
測試文件以test_開頭(以_test結尾也可以)
測試類以Test開頭,並且不能帶有 init 方法
測試函數以test_開頭
斷言使用基本的assert即可
"""
3.allure(官網地址:https://docs.qameta.io/allure/#_installing_a_commandline)
環境依賴:java8+、jdk1.8
brew install allure pip install pytest pip install allure-pytest
終端中執行:
allure
Usage: allure [options] [command] [command options]
Options:
--help
Print commandline help.
-q, --quiet
Switch on the quiet mode.
Default: false
-v, --verbose
Switch on the verbose mode.
Default: false
--version
Print commandline version.
Default: false
Commands:
generate Generate the report
Usage: generate [options] The directories with allure results
Options:
-c, --clean
Clean Allure report directory before generating a new one.
Default: false
--config
Allure commandline config path. If specified overrides values from
--profile and --configDirectory.
--configDirectory
Allure commandline configurations directory. By default uses
ALLURE_HOME directory.
--profile
Allure commandline configuration profile.
-o, --report-dir, --output
The directory to generate Allure report into.
Default: allure-report
serve Serve the report
Usage: serve [options] The directories with allure results
Options:
--config
Allure commandline config path. If specified overrides values from
--profile and --configDirectory.
--configDirectory
Allure commandline configurations directory. By default uses
ALLURE_HOME directory.
-h, --host
This host will be used to start web server for the report.
-p, --port
This port will be used to start web server for the report.
Default: 0
--profile
Allure commandline configuration profile.
open Open generated report
Usage: open [options] The report directory
Options:
-h, --host
This host will be used to start web server for the report.
-p, --port
This port will be used to start web server for the report.
Default: 0
plugin Generate the report
Usage: plugin [options]
Options:
--config
Allure commandline config path. If specified overrides values from
--profile and --configDirectory.
--configDirectory
Allure commandline configurations directory. By default uses
ALLURE_HOME directory.
--profile
Allure commandline configuration profile.
4.logging
logging模塊的詳細使用:https://www.cnblogs.com/nancyzhu/p/8551506.html
https://blog.csdn.net/Runner1st/article/details/96481954
1)修改pytest.ini文件
pytest是從pytest.ini中讀取log_cli配置的,默認是關閉的
[pytest] log_cli = 1 log_cli_level = INFO log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s) log_cli_date_format=%Y-%m-%d %H:%M:%S
2)用pytest -o方式重寫,這個功能在pytest 3.4之后才實現,如下
pytest testcases.py -o log_cli=true -o log_cli_level=INFO
logging模塊使用:
logging.basicConfig(level=logging.INFO, filemode='a', filename='logger.log',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
def test_a():
log.info('info message')
log.debug('debug message')
log.warning('warning message')
log.error('error message')
log.critical('critical message')
assert 1, "pass"
allure+logging實例:
currentPath = os.path.dirname(os.path.abspath(__file__))
date = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
"""
filemode='a',##模式,有w和a,w就是寫模式,每次都會重新寫日志,覆蓋之前的日志
#a是追加模式,默認如果不寫的話,就是追加模式
"""
if __name__ == '__main__':
# 使用pytest生成報告:pytest --html=report.html #
使用allure生成報告
report_path = os.path.join(currentPath, 'results', 'reports', str(date))
allure_report_path = os.path.join(currentPath, 'results', 'allurereport', str(date))
test_folder = os.path.join(currentPath, 'testcases')
pytest.main([test_folder, '--alluredir=%s' % (report_path), '-o log_cli=true']) #-o log_cli_level=INFO
os.system('/usr/local/Cellar/allure/2.13.5/bin/allure generate %s -o %s/html --clean' % (report_path, allure_report_path)) # 替換為本地的 allure 安裝路徑
