简体   繁体   English

无法使用 GitHub 操作从 pytest 中的存储库导入脚本

[英]Cannot import script from repository in pytest using GitHub actions

I am learning to implement automatic testing using GitHub actions.我正在学习使用 GitHub 操作实施自动测试。 I am trying to solve the following import error.我正在尝试解决以下导入错误。

The error is:错误是:

utils\test_capitalize.py:2: in <module>
    from src.capital import capital_case
E   ModuleNotFoundError: No module named 'src'

The structure of my repository is:我的存储库的结构是:

example/
|-- setup.cfg
|-- setup.py
|-- pyproject.toml
|-- .github/
|   |-- workflows/
|   |   |-- ci.yml
|-- src/
|   |-- capital.py
|-- utils/
|   |-- test_capitalize.py

The content of ci.yml is: ci.yml的内容是:

name: ci

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build-and-test:

    runs-on: windows-latest
    strategy:
      matrix:
        python-version: [3.8]
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Build
        run: |
          python -m pip install .
          python -m pip install src
      - name: Test with pytest
        run: |
          python -m pip install pytest
          pytest

I thought that python -m pip install.我认为python -m pip install. already installed all the modules in the repository, do I have a misconception about it?.已经在存储库中安装了所有模块,我对此有误解吗?。

Edit: The pytest file is a minimum example (I have tried removing the src before pushing):编辑:pytest 文件是一个最小示例(我尝试在推送之前删除 src):

import pytest
from src.capital import capital_case


def test_capital_case():
    assert capital_case('semaphore') == 'Semaphore'

Include these lines in the header of your script before importing the Module.在导入模块之前,将这些行包含在脚本的 header 中。

import sys

sys.path.append("../")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM