Running python unit test in Google Cloud Build

允我心安 提交于 2020-12-10 09:00:26

问题


I would like Google Cloud Build to run the unit test I have in my python project after pushing the changes. I am able to configure the step that runs the test, but I am not sure how to input the directory the holds the test and if I just put . then it run 0 test

my project structure is: - project_name - package_name - test - sample_test.py

This is my cloudbuild.yaml configuration:

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

The above fails with this message:

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

And if I replace the folder with just . then it runs but does not discover any tests. For copying the code to gs buckets, we use gsutil and then ./package_name copies the package to the bucket Locally this of course works

How can I understand what is the correct folder structure for my tests to run? Thanks!


回答1:


  1. Why do you use appengine container? Why not just use an official Python container - name: python:3.7? Steps run as Docker containers, and I see no reason to run Docker in docker when you want to run docker container. Try this:
steps:
  - name: python:3.7
    args: ["python","-m","unittest","discover","--verbose","-s","./package_name/test/","-p","*_test.py"]
    id: unittest
  1. Also, do you have __init__.py file in the directory ./package_name/test? The message directory is not importable usually means that this directory is not a Python package because of missing __init__.py file.


来源:https://stackoverflow.com/questions/55022058/running-python-unit-test-in-google-cloud-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!