简体   繁体   中英

Cannot import script from repository in pytest using GitHub actions

I am learning to implement automatic testing using GitHub actions. 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:

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. 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):

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.

import sys

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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