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

Azure DevOps中Selenium Python测试报错:No module named 'POM'求助

解决Azure DevOps中Python Selenium测试找不到POM模块的问题

核心问题分析

本地运行正常但Azure DevOps报错ModuleNotFoundError: No module named 'POM',主要原因是:

  • 测试脚本中sys.path的路径添加语法错误(使用了无效的...层级)
  • Azure Pipelines执行环境中,Python路径未正确包含项目根目录

具体解决方案

1. 修复测试脚本的sys.path配置

打开TestScript/test_smoketest.py,替换错误的路径添加代码:

原代码:

sys.path.append(os.path.join(os.path.dirname(__file__), "...", "..."))

修改为:

# 将项目根目录添加到Python搜索路径
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(project_root)

这样可以确保脚本能找到根目录下的POM、Setup等文件夹。

2. 在Azure Pipelines中显式设置PYTHONPATH

修改azure-pipelines.yml中的Run test script步骤,先设置PYTHONPATH为项目根目录,再执行pytest:

- script: |    
    set PYTHONPATH=$(System.DefaultWorkingDirectory)
    pytest -s ./TestScript/test_smoketest.py --html=pytest_selenium_test_report.html
  displayName: 'Run test script'

$(System.DefaultWorkingDirectory)是Azure DevOps内置变量,指向代码仓库的根目录(即D:\a\1\s),设置后Python会自动从这里搜索模块。

3. 确认__init__.py文件存在

确保以下文件夹下都有__init__.py(空文件即可):

  • POM/
  • TestScript/
  • Setup/

Python需要通过该文件识别这些文件夹为可导入的包。

验证修改

提交代码到main分支后,重新触发Azure DevOps流水线,测试应该能正常加载POM模块并执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:25:16