You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins执行PyTest时遇AttributeError: 'Config'对象无'_variables'属性

Jenkins中新增PyTest任务触发AttributeError问题排查

问题背景

配置2个相同的Jenkins任务执行PyTest自动化脚本时运行正常,但新增第3个相同配置的任务后,执行脚本抛出AttributeError。

错误详情

"Executing tests"
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\_pytest\main.py", line 265, in wrap_session
INTERNALERROR>     config._do_configure()
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\_pytest\config\__init__.py", line 1056, in _do_configure
INTERNALERROR>     self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\pluggy\_hooks.py", line 277, in call_historic
INTERNALERROR>     res = self._hookexec(self.name, self.get_hookimpls(), kwargs, False)
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\pluggy\_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\pluggy\_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\pluggy\_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "C:\ProgramData\Jenkins\.jenkins\workspace\Integration-Automation\sf_venv\lib\site-packages\pytest_selenium\pytest_selenium.py", line 249, in pytest_configure
INTERNALERROR>     capabilities = config._variables.get("capabilities", {})
INTERNALERROR> AttributeError: 'Config' object has no attribute '_variables'
"Test complete."

执行的批处理命令

pip install virtualenv
python -m virtualenv test
call .\test\Scripts\activate.bat
pip install -r requirements.txt
echo "Executing tests"
python -m pytest ./src/scripts/activities -v -m mytags --environment="TestEnvironment" --stream="TestStream" --junitxml="test_results/results.xml"
call deactivate
echo "Test complete."

已尝试无效操作

  • 修改批处理命令
  • 改为不串行运行任务

解决建议

1. 锁定pytest-selenium与pytest兼容版本

错误源于pytest-selenium插件尝试访问pytest Config对象不存在的_variables属性,大概率是插件与pytest版本不兼容导致。

  • 检查requirements.txt,如果未指定版本,添加兼容的稳定版:
    pytest==7.4.0
    pytest-selenium==4.0.0
    
  • 版本号可根据实际兼容性调整,优先选择官方标注兼容的版本组合。

2. 强制清理Jenkins工作空间旧虚拟环境

新增任务可能因工作空间残留旧虚拟环境文件导致冲突,修改批处理命令先清理旧环境:

:: 先删除旧虚拟环境目录
rmdir /s /q test
pip install virtualenv
python -m virtualenv test
call .\test\Scripts\activate.bat
pip install -r requirements.txt
echo "Executing tests"
python -m pytest ./src/scripts/activities -v -m mytags --environment="TestEnvironment" --stream="TestStream" --junitxml="test_results/results.xml"
call deactivate
echo "Test complete."

3. 排查插件冲突

多个pytest插件可能相互干扰,导致Config对象属性异常:

  • 临时禁用非必要插件,仅保留pytest-selenium运行测试,验证是否为插件冲突导致。
  • 逐步恢复其他插件,定位冲突源后调整版本或移除冲突插件。

内容的提问来源于stack exchange,提问作者Nikhil Bidave

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 15:25:40