尝试使用多个测试类测试多个测试文件,但 PyTest 无法识别其他文件

问题描述 投票:0回答:1

我一直在互联网上搜索以找到解决此问题的方法。我一直在努力确保 PyTest 识别多个测试文件以解释多个测试文件及其内部的类,以便构建完整的测试套件。

这是配置文件:

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

这是它识别的测试类:

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

这是 Pytest 无法识别的文件:

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

我有什么遗漏的吗?我在这两个页面中到底缺少什么。我浏览了文档的相关部分,但没有得到满意的答案。如果可能的话请帮助我。谢谢。

python django pytest
1个回答
0
投票

Pytest 识别以前缀 test_ 开头的测试方法/函数

因此在函数名称前面添加 test_ 应该可以解决问题。

class TestPosts:
#Disallows user to create a post if they're not a contributor
    @pytest.mark.django_db
    def test_is_not_contributor(db):
        nonauthorizedUser = MemberFactory()
        can_post = nonauthorizedUser.has_perms("post.add_post")
        assert can_post is False
© www.soinside.com 2019 - 2024. All rights reserved.