0
点赞
收藏
分享

微信扫一扫

UI自动化测试框架搭建 —— 编写执行脚本入口


执行测试的时候使用的语句是

stage('执行测试'){
steps{
dir("${env.WORKSPACE}/src/cases/") {
sh ""
sh '''
python3 allure_debug.py
exit 0
'''
}
}
}

所以还需要一个​​src/cases/allure_debug.py​​文件来执行整个测试套

编写allure_debug.py

第三方模块路径添加到环境变量

在项目中导包使用的语句为​​from src.xxx import xxx​

这种导入方式在​​Pycharm​​中可以正常执行,但是在命令行执行的时候会报错,因为对于命令行的环境来说,它不知道你的src所在的路径,所以需要把它加到环境变量中

import os
import sys

BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(BASE_PATH)
sys.path.append(os.path.join(BASE_PATH, 'src'))

通过​​os​​​和​​sys​​模块找到文件路径并添加到环境变量中

获取需要执行的用例

有两种方式获取要执行的用例

  1. 根据jenkinsfile中的case的内容
  2. 当前文件夹下全部符合pytest执行条件的测试文件
from src.utils.constant import CASES

if CASES:
test_cases = CASES.split('\n')

通过切割​​\n​​就可以拿到jenkinsfile中填写的测试用例了

使用pytest.main执行


https://docs.pytest.org/en/7.0.x/reference/reference.html?highlight=main#pytest.main


pytest有很多的命令可以使用,可以使用​​pytest -h​​查看

这次用到

​-s​​ :shortcut for --capture=no.

​-v​​:increase verbosity.

​--durations=0​​:show N slowest setup/test durations (N=0 for all)

​--alluredir​​:Generate Allure report in the specified directory (may not exist)

拼接一个pytest执行命令来执行

if CASES:
test_cases = CASES.split('\n')
main_list = [
'-s',
'-v',
*test_cases,
'--durations=0', '--clean-alluredir',
'--alluredir', f'{REPORT_PATH}/allure_results'
]
else:
main_list = [
'-s',
'-v',
'--durations=0', '--clean-alluredir',
'--alluredir', f'{REPORT_PATH}/allure_results'
]

判断是否需要并发

if CONCURRENT != '否':  # 是否并发执行
main_list.append('-n')
main_list.append(CONCURRENT)
main_list.append('--dist')
main_list.append('loadfile')

运行

pytest.main(main_list)

本地生成Allure报告查看

判断当前不是在jenkins执行中

mac系统需要给​​{ALLURE_TOOL_PATH}/allure​​文件添加运行权限

cd xxx/allure
chmod 777 allure
if not os.getenv("BUILD_URL"):
os.system(f"{ALLURE_TOOL_PATH}/allure serve {REPORT_PATH}/allure


举报

相关推荐

0 条评论