pytest: ImportError while loading conftest - Python3

I am trying to use pytest-django for writing my unit tests.

I have a project structure like:

cwweb/
     accounts/
             tests/
                  test_views.py
             views.py
             models.py
             __init__.py
     cwweb/
          __init__.py
     	  settings/
     	  		  __init__.py
     	          local.py
     	          prod.py
     src/
     conftest.py
     pytest.ini
     manage.py

Now my pytest.ini looks like:

[pytest]
addopts = --reuse-db --nomigrations
DJANGO_SETTINGS_MODULE = cwweb.settings.local

Also, my conftest.py looks like:

import string
from random import choice, randint

import pytest


@pytest.fixture()
def test_password():
    """
    generate_test_password
        A fixture to generate strong test password
    """
    characters = string.ascii_letters + string.punctuation + string.digits
    return "".join(choice(characters) for _ in range(randint(8, 16)))


@pytest.fixture()
def create_test_user(db, django_user_model, test_password):
    """
    create_test_user
    """

    def make_user(**kwargs):
        kwargs['password'] = test_password
        return django_user_model.objects.create_user(**kwargs)

    return make_user


@pytest.fixture()
def user_client(db, client, create_test_user, test_password):
    """
    user_client
        This fixture is used to get an authenticated user client and re-used wherever needed
    """

    def create_authenticated_user():
        """
        create_authenticated_user
        """
        user_client_data = {
            'firstName': 'CWK',
            'middleName': 'Test',
            'lastName': 'User',
            'email': 'testuser@testcwk.com',
            'primaryContact': '0000000000',
            'is_verified': True,
        }
        user = create_test_user(**user_client_data)
        client.login(username=user.username, password=user.password)
        return client, user

    return create_authenticated_user

Also, my test_views.py looks like:

import pytest
from django.urls import reverse


class TestAccountsViews(object):

    @pytest.mark.django_db
    def test_user_registration_status(self, user_client):
        """
        test_user_registration_status
        """
        url = reverse('check-user-registration')
        response = user_client.get(url)
        assert response.status == 200

When I issue a pytest command on my terminal from the directory where my conftest.py is situated I get:

pytest
Current Environment : local
ImportError while loading conftest '/home/waqas/Desktop/projects/cwweb/conftest.py'.
ModuleNotFoundError: No module named 'cwweb.conftest'

I have been banging my head against the wall since yesterday to get a solution to this problem.

I have googled my way into everything and did not find a solution which is when I decided to reach out to the community here.

What am I doing wrong guys? What am I missing here?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I just ran into a very similar problem:

ImportError while loading conftest '[...]/test/conftest.py'.
ModuleNotFoundError: No module named 'test.conftest'

I could fix it by adding --import-mode=importlib or by deleting the __init__.py that was a sibling to my conftest.py.

well, for me, i had to delete all the pyc files and try again, and it worked. check this SO comment to delete all pyc files in a project.

Can you try this with pytest master and say if the behavior is the same?

pip install git+https://github.com/pytest-dev/pytest

Also with this version, can you try with different values of the --import-mode option, in particular pytest --import-mode=importlib?

This is an incredibly common, and highly annoying issue. What could be stopping pytest from seeing the environment it’s literally running in? Every single time I write a test after adding a library to my project, I go through this, and I end up just messing with it until it eventually decides to do what it’s supposed to after a couple of hours

I’m no Pytest expert, but the previous reply (while presumably well-intentioned) seems to likely be poor, or potentially even dangerous advice (pythonpath manipulation, archaic pytest invocation, OS, environment and project-dependent commands, etc) while not really addressing the problem. Given its the latest reply, and as a fairly highly ranked page on Google many users are likely to stumble upon it, I would suggest taking some action @Zac-HD @bluetech . On the Spyder org, we’ve found Github’s moderation tools (comment hiding, etc) useful for this purpose. Feel free to hide my comment too once dealt with.

i think you should first activate your virtual environment then try to run in – source VENV/bin/activate – export PYTHONPATH=$PATH:. – pip install -r requirements.txt – py.test