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

Plotly-Dash测试在GitLab CI流水线运行报DashComposite未定义错误

问题根因

你遇到的NameError: name 'DashComposite' is not defined报错是Dash 2.0.0版本的内置缺陷,该版本的dash.testing.plugin模块没有预先导入DashComposite类,导致pytest fixture初始化时触发未定义错误,同时你的CI配置还有几处不兼容的地方需要调整。

修复步骤

1. 调整依赖版本

把requirements_for_testing.txt里的依赖版本替换为稳定兼容版,避免可选依赖安装失败:

dash==2.7.0
dash-testing==0.7.0
pytest>=7.0.0
selenium>=4.1.0
pandas
plotly
requests
webdriver-manager

不要使用dash >=2.0.0这种模糊版本声明,2.0.x小版本普遍存在测试模块导入异常的问题。

2. 修改gitlab-ci.yml配置

  • 去掉selenium服务配置里的多余空格,指定固定版本避免兼容性问题
  • 显式给pytest传入远程webdriver配置,匹配GitLab CI服务的访问规则
  • 升级pip避免可选依赖识别异常
    修改后的配置如下:
stages:
  - test
test:dashboard:
  stage: test
  image: python:3.8-slim
  services:
    - name: selenium/standalone-chrome:109.0
      alias: chrome
  before_script:
    - pip install --upgrade pip
    - pip install -r requirements_for_testing.txt
  script:
    - export PYTHONPATH=$PYTHONPATH:$PWD
    - pytest --webdriver Remote --remote-url http://chrome:4444/wd/hub --headless

3. 优化conftest.py配置

新增Linux容器环境下Chrome运行必须的启动参数,避免浏览器启动失败:

from selenium.webdriver.chrome.options import Options

def pytest_setup_options():
    options = Options()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--disable-gpu')
    return options

4. 修复测试用例废弃API

Selenium 4+已经移除了find_element_by_css_selector接口,替换为标准写法:

from dash.testing.application_runners import import_app
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions


def test_example(dash_duo):
    app = import_app(app_file='app')
    dash_duo.start_server(app)
    WebDriverWait(dash_duo.driver, 10).until(
        expected_conditions.visibility_of_element_located(
            (By.CSS_SELECTOR, "#example-graph")))
    assert dash_duo.driver.find_element(By.CSS_SELECTOR, "#example-graph").is_displayed()
本地验证方法

修改完成后可以先在本地执行以下命令验证配置有效性:
pytest --webdriver Chrome --headless
确认本地测试通过后再推送到GitLab运行流水线即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:27:05