Django not findind tests within my monorepo

80 Views Asked by At

Alright, this seems like a noob question, but here I go:

On my current company, we use a monorepo that stores several projects. Django is among one of them. Here's the basic structure:

├── README.md
├── code
│   ├── __init__.py
│   ├── gunicorn.conf.py
│   ├── manage.py
│   ├── non-django-project
│   │   ├── ...
│   ├── django_project
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── apps
│   │   │   ├── django_app_1
│   │   │   │   ├── ...
│   │   │   │   ├── tests
│   │   │   │   │   ├── test_something.py
│   │   │   ├── django_app_2
│   │   │   │   ├── ...
│   │   │   │   ├── tests
│   │   │   │   │   ├── test_something.py
│   │   │   ├── django_app_3
│   │   │   │   ├── ...
│   │   │   │   ├── tests
│   │   │   │   │   ├── test_something.py
│   │   │   │   ├── ...
│   │   ├── libs
│   │   │   ├── lib1.py
│   │   │   ├── lib2.py
│   │   │   ├── lib1.py
│   │   │   ├── tests.py

Now, my github workflow is simple, and baiscally setups python and run the django tests:

name: django
defaults:
  run:
    working-directory: ./code
on:
  workflow_dispatch:
  pull_request:
    paths: ["code/django_project/**", ".github/workflows/django.yml"]
    branches:
      - develop
  push:
    branches:
      - develop    

jobs:
  tests:
    runs-on: ubuntu-20.04
    permissions:
      # Gives the action the necessary permissions for publishing new
      # comments in pull requests.
      pull-requests: write
      # Gives the action the necessary permissions for pushing data to the
      # python-coverage-comment-action branch, and for editing existing
      # comments (to avoid publishing multiple comments in the same PR)
      contents: write
    services:
      redis:
        image: redis
      postgres:
        image: postgres:15.2-alpine
        env:
          POSTGRES_PASSWORD: ****
          POSTGRES_USER: *****
          POSTGRES_DB: ******
        ports:
          - 5432:5432
    env:
      PYTHONUNBUFFERED: 1
    steps:
      - name: checkout
        uses: actions/checkout@v4
      - name: Setup python 3.12
        uses: actions/setup-python@v4
        with:
          python-version: "3.12"
          architecture: x64
          cache: "pip"
          cache-dependency-path: code/django_project/requirements.txt
      - run: sudo apt-get update -qq
      - name: Install & cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@v1
        with:
          packages: build-essential xvfb gdebi
          version: 1.0
      - name: Install locked pip version
        run: pip install --upgrade pip==23.2.1
      - name: Install python dependencies
        run: pip install -r **django_project/requirements**.txt
      - name: Run Django tests
        run: coverage run --branch --rcfile=.coveragerc ./manage.py test -v 3 --noinput
      - name: Coverage comment
        uses: py-cov-action/python-coverage-comment-action@v3
        with:
          COVERAGE_PATH: code/
          GITHUB_TOKEN: ${{ github.token }}

I should also mention that my settings.py contains a bit of code to deal with the unsual all locations:

"""settings.py"""
# Build paths inside the project like this: join_to_base_dir(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))


def join_to_base_dir(path):
    return os.path.join(BASE_DIR, path)

import sys
sys.path.append(join_to_base_dir('.'))
sys.path.append(join_to_base_dir('apps'))

The issue I'm facing is, when this workflow runs, it doesn't find any tests:

Found 1 test(s).
Skipping setup of unused database(s): default.
System check identified no issues (0 silenced).
CODEOWNERS file is not available
code.django_project (unittest.loader._FailedTest.code.django_project) ... ERROR

======================================================================
ERROR: code.django_project (unittest.loader._FailedTest.code.django_project)
----------------------------------------------------------------------
ImportError: Failed to import test module: code.django_project
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/unittest/loader.py", line 415, in _find_test_path
    package = self._get_module_from_name(name)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/unittest/loader.py", line 325, in _get_module_from_name
    __import__(name)
ModuleNotFoundError: No module named 'code.django_project'; 'code' is not a package

This was working perfectly before I updated from Django 2.2 @ Python 3.9 to Django 4.2 @ Python 3.12

What am I doing wrong?

1

There are 1 best solutions below

1
On

Try to install your package in editable mode before running the tests:

pip install -e ./