question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportError while loading conftest - Python3

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
qci-amoscommented, Apr 23, 2021

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.

1reaction
CAM-Gerlachcommented, Jul 29, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

conftest.py ImportError: No module named Foo - Stack Overflow
The error says the conftest.py can not be loaded because of an ImportError . Try moving your import inside the foo fixture like...
Read more >
ImportError while loading conftest - Python3 - Bountysource
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 ...
Read more >
Building neo4j app with Python - Problem with pytest
ImportError while loading conftest './tests/conftest.py'. tests/conftest.py:7: in <module> from api import create_app E ModuleNotFoundError: ...
Read more >
Pytest -m connection: ImportError while loading conftest
ImportError while loading conftest '/Users/user/Dropbox/projects/code/mongodb/mflix-python/tests/conftest.py' - after I move project files.
Read more >
751244 – dev-python/cmd2-1.3.11 fails test - Gentoo's Bugzilla
ImportError while loading conftest '/var/tmp/portage/dev-python/cmd2-1.3.11/work/cmd2- ... tests/conftest.py:12: in <module> > import cmd2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found