[Python] Trying to test multiple test files with multiple test classes but PyTest doesn't...

Discussão em 'Python' iniciado por Stack, Outubro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I've been scouring all over the internet in order to find solution for this problem. I've been trying to make sure that PyTest recognizes multiple test files to account for the multiple test files and the classes inside them in order to build a full test suite.

    Here's the config file:

    [pytest]
    asyncio_mode=auto
    asyncio_default_fixture_loop_scope="class"
    DJANGO_SETTINGS_MODULE = personal_blog.settings
    testpaths = tests


    Here's the test class that it recognizes:

    test_members.py

    import pytest
    import factory
    from django.contrib.auth import get_user_model
    from factories import MemberFactory
    from faker import Faker
    from django.urls import reverse
    from django.contrib.auth.models import Permission

    fake = Faker()

    # Create your tests here.
    User = get_user_model()

    # Basic Tests
    class TestMembers:
    #Confirms that a user has been created
    @pytest.mark.django_db
    def test_if_user_exists(db):
    user = MemberFactory()
    assert user.username is not None
    assert user.email is not None

    # Checks if the password fails
    @pytest.mark.django_db
    def test_set_check_password_fail(db):
    # basic_user.set_password("password")
    user = MemberFactory()
    assert user.password != 'Wrong'

    # Checks if the password fails
    @pytest.mark.django_db
    def test_set_check_password_success(db):
    user = MemberFactory()
    assert user.password == 'password'

    # Checks if the user is not a contributor by default.
    @pytest.mark.django_db
    def test_is_not_contributor_by_default(db):
    user = MemberFactory()
    assert user.is_contributor is False

    # Checks if the User is a contributor
    @pytest.mark.django_db
    def test_is_contributor(db):
    user = MemberFactory(is_contributor = True, is_superuser = False)
    assert user.is_contributor is True

    # Checks if the user is not a superuser
    @pytest.mark.django_db
    def test_is_not_superuser(db):
    user = MemberFactory()
    assert user.is_superuser is False

    # Checks if the user is a superuser
    @pytest.mark.django_db
    def test_is_superuser(db):
    user = MemberFactory(is_superuser = True, is_contributor = True)
    assert user.is_superuser is True

    class TestPosts:
    #Disallows user to create a post if they're not a contributor
    @pytest.mark.django_db
    def is_not_contributor(db):
    nonauthorizedUser = MemberFactory()
    can_post = nonauthorizedUser.has_perms("post.add_post")
    assert can_post is False


    Here's the file that Pytest doesn't recognize:

    import pytest
    import factory
    from django.contrib.auth import get_user_model
    from posts.models import Post
    from factories import MemberFactory, PostFactory
    from faker import Faker
    from django.contrib.auth.models import Permission

    # Create your tests here.
    User = get_user_model()
    fake = Faker()






    # Post Tests
    class TestPosts:
    #Disallows user to create a post if they're not a contributor
    @pytest.mark.django_db
    def is_not_contributor(db):
    nonauthorizedUser = MemberFactory()
    can_post = nonauthorizedUser.has_perms("post.add_post")
    assert can_post is False



    Is there something I'm missing? What exactly am I missing in both the page. I've look through the relevant parts of the documentation without a satisfying answer. Please help me if possible. Thanks.

    Continue reading...

Compartilhe esta Página