Skip to content

ImportError while loading conftest - Python3 #7408

@waqashamid

Description

@waqashamid

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': '[email protected]',
            '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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions