pythondjangocirclecicircleci-2.0

Unable to run django test in CircleCI


I am implementing CircleCI for one of the projects. The project is built on Django 3.2.

My test cases run properly when I run using python manage.py test blog, when I run the same in CircleCI it returns ,

======================================================================
ERROR: project.blog (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: project.blog
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/unittest/loader.py", line 470, in _find_test_path
    package = self._get_module_from_name(name)
  File "/usr/local/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
ModuleNotFoundError: No module named 'project.blog'

Here is my CircleCI config

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.8
    steps:
      - checkout
      - run:
          name: Installing dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip3 install -r requirements.txt
      - run:
          name: Running migrations
          command: |
            . venv/bin/activate
            python manage.py migrate --skip-checks
      - run:
          name: Running tests
          command: |
            . venv/bin/activate
            python manage.py test blog

I understand that CircelCI clones the project in project folder. Is that something that I am missing in config?


Solution

  • CircleCI by default checkouts our codebase to /home/circleci/project, problem was in list of installed_apps I had an app with name project.(which was conflicting)

    When CircleCI ran python manage.py test the unittest module was searching the app blog inside django app project.

    I fixed this problem by changing the default path to which CircleCI puts out codebase. Here is the updated CircleCI config

    version: 2
    jobs:
      build:
        working_directory: ~/platform #Here is the answer
        docker:
          - image: circleci/python:3.8
        steps:
          - checkout:
              path: ~/platform #Here is the answer
          - run:
              name: Install dependencies
              command: |
                ls -l
                python3 -m venv venv
                . venv/bin/activate
                pip3 install -r requirements.txt
          - run:
              name: Run migrations
              command: |
                . venv/bin/activate
                python manage.py migrate --skip-checks
          - run:
              name: Run tests
              command: |
                . venv/bin/activate
                python manage.py test website