[英]NameError: name 'pytest' is not defined - GitHub Actions
我有一個簡單的Docker
圖像,其中包含一些 python 測試用例。
這是我的Dockerfile
ARG PYTHON_VERSION=3.8.3-alpine
# Use the python image as the base image
FROM python:${PYTHON_VERSION}
# upgrade pip
RUN pip install --upgrade pip
# set CUSTOMER_NAME as environment variables
ENV CUSTOMER_NAME=${CUSTOMER_NAME:-A}
# Set the working directory
WORKDIR /app
# Create a non-root user and add the permissions
RUN adduser -D "${USERNAME:-jananath}"
# set the username - HERE WE ARE USING A DEFAULT VALUE FOR THE USERNAME, WHICH IS "jananath"
USER "${USERNAME:-jananath}"
# copy the requirements file and install the dependencies
COPY --chown="${USERNAME:-jananath}":"${USERNAME:-jananath}" requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade --user -r requirements.txt
ENV PATH="/home/${USERNAME:-jananath}/.local/bin:${PATH}"
# copy the app code
COPY --chown=${USERNAME:-jananath}:${USERNAME:-jananath} . .
# expose the default Flask port
EXPOSE 80
# set the entrypoint to run the app
CMD ["uvicorn", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"]
然后我構建圖像:
docker build -t hello:v1 .
然后我運行應用程序
docker run --rm -itd -p 80:80 -e CUSTOMER_NAME=A hello:v1
然后我執行pytest
docker exec -it 570e3153ab90 pytest
它成功地給出了pytest
output 如下:
================================================= test session starts =================================================
platform linux -- Python 3.8.3, pytest-7.2.0, pluggy-1.0.0
rootdir: /app, configfile: pytest.ini
plugins: anyio-3.6.2
collected 2 items
test_main.py .. [100%]
================================================== 2 passed in 0.24s ==================================================
一切正常,除了我在`GitHub Actions 中運行相同的圖像。
. . .
container-test-job:
runs-on: ubuntu-latest
container:
image: ghcr.io/<USERNAME>/<REPO>/hello:v2
credentials:
username: ${{ github.actor }}
password: ${{ secrets.TOKEN_REPOSITORY }}
env:
CUSTOMER_NAME: A
steps:
- name: Test
shell: python
run: |
pytest
. . .
但我收到以下錯誤:
追溯(最近一次通話最后一次):文件“/__w/_temp/598eea6e-7acf-4d7c-964f-a69440ea38c2.py”,第 1 行,在 pytest NameError:名稱“pytest”未定義錯誤:進程已完成,退出代碼為 1 .
有人可以幫我理解這里的問題以及如何解決嗎?
謝謝!
嘗試此代碼,然后您可以構建圖像並將其推送到您的注冊表。
name: build and test
on:
push:
branches:
- "main"
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: 3.10.6
architecture: "x64"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with pytest
run: |
pytest
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.