python - 如何使用 CircleCI 在后台运行服务器?

标签 python django continuous-integration circleci

我在我的 Django 项目中使用 CircleCI。我想在后台运行服务器(特别是 python manage.py runserver)以进行一些特定的 selenium 测试。

我的config.yml有点像

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.6.1-browsers
      - image: selenium/standalone-chrome

    working_directory: ~/myproject

    steps:
      - checkout
      - run:
          name: install dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements.txt

      - run:
          name: run unit tests
          command: |
            . venv/bin/activate
            python manage.py test

      - run:
          name: run selenium tests
          command: |
            . venv/bin/activate
            python manage.py migrate
            python manage.py runserver 8000 
            python manage.py run_selenium_tests         

我可以通过在 django LiveServerTestCase 中运行 selenium 测试来让它工作。但是我想独立运行 selenium 测试,因为我需要 runserver 在后台运行。现在 circleci 在 python manage.py runserver 停止执行并最终超时。有什么想法吗?

最佳答案

您需要将服务器启动为 background command .或者,您也可以使用 cURL 等待服务器准备就绪。

根据您发布的配置,您可以执行以下操作:

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.6.1-browsers
      - image: selenium/standalone-chrome

    working_directory: ~/myproject

    steps:
      - checkout
      - run:
          name: install dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements.txt

      - run:
          name: run unit tests
          command: |
            . venv/bin/activate
            python manage.py test

      - run:
          name: run selenium tests prep
          command: |
            . venv/bin/activate
            python manage.py migrate
      - run:
          name: run server
          command: python manage.py runserver 8000
          background: true
      - run:
          name: run selenium tests
          command: |
            curl --retry-delay 5 --retry 10  --retry-connrefused http://localhost:8000
            python manage.py run_selenium_tests

curl 语句在继续之前等待端口响应。这使您的服务器有时间完全启动。

- 里卡多·费利西亚诺
CircleCI 开发人员布道师

关于python - 如何使用 CircleCI 在后台运行服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45594861/

相关文章:

python - MySql 批量导入,无需将文件写入磁盘

python - 附加时更改数据帧索引

python - 用于比较两个向量以进行分类的损失函数

python - numpy 数组中的索引混淆

python - django 迁移在运行迁移之前运行 django table2 模块的过滤器是否正确?

c# - TeamCity - 将工件依赖项的文件路径映射到系统属性

firebase - 在 GitLab CI 上运行 Firebase 模拟器

django - 用特定于请求的属性装饰 Django 模型的干净方法是什么?

python - Django FilterSet AND(?)条件

jenkins - 无法使用 Jenkins 从 Powershell 脚本导入模块