首页 > 解决方案 > 在 Google Cloud Build 中运行 python 单元测试

问题描述

我希望 Google Cloud Build 在推送更改后运行我在 python 项目中的单元测试。我能够配置运行测试的步骤,但我不确定如何输入保存测试的目录,如果我只是把.它运行 0 test

我的项目结构是: - project_name - package_name - test - sample_test.py

这是我的cloudbuild.yaml配置:

steps:
    - name: 'gcr.io/cloud-builders/docker'
      args: ["run","gcr.io/google-appengine/python","python3","-m","unittest","discover","--verbose","-s","./package_name/test/","-p","*_test.py"]
      id:   unittest

以上失败并显示此消息:

raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: './package_name/test/'
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: exit status 1
Show debug panel

如果我用 just .then 替换文件夹,它会运行但没有发现任何测试。为了将代码复制到 gs 存储桶,我们使用 gsutil 然后 ./package_name 将包复制到存储桶本地这当然可以

我如何理解运行测试的正确文件夹结构是什么?谢谢!

标签: python-3.xgoogle-cloud-build

解决方案


  1. 为什么要使用appengine容器?为什么不直接使用官方的 Python 容器name: python:3.7?步骤作为 Docker 容器运行,当你想运行 docker 容器时,我认为没有理由在 docker 中运行 Docker。尝试这个:
steps:
  - name: python:3.7
    args: ["python","-m","unittest","discover","--verbose","-s","./package_name/test/","-p","*_test.py"]
    id: unittest
  1. 另外,您__init__.py的目录中有文件./package_name/test吗?该消息directory is not importable通常表示此目录不是 Python 包,因为缺少__init__.py文件。

推荐阅读