Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django fixture不能直接调用_Django_Unit Testing_Pytest_Fixtures_Django Testing - Fatal编程技术网

Django fixture不能直接调用

Django fixture不能直接调用,django,unit-testing,pytest,fixtures,django-testing,Django,Unit Testing,Pytest,Fixtures,Django Testing,我正在使用Django 3.0.5、pytest 5.4.1和pytest Django 3.9.0。我想创建一个fixture,该fixture返回一个在测试中使用的用户对象。 这是我的conftest.py import pytest from django.contrib.auth import get_user_model @pytest.fixture def create_user(db): return get_user_model().objects.create_u

我正在使用Django 3.0.5pytest 5.4.1pytest Django 3.9.0。我想创建一个fixture,该fixture返回一个在测试中使用的用户对象。 这是我的conftest.py

import pytest
from django.contrib.auth import get_user_model


@pytest.fixture
def create_user(db):
    return get_user_model().objects.create_user('user@gmail.com', 'password')
import pytest
from rest_framework.test import APITestCase, APIClient


    class StudentViewTests(APITestCase):

        user = None

        @pytest.fixture(scope="session")
        def setUp(self, create_user):
            self.user = create_user

        def test_create_student(self):
            assert self.user.email == 'user@gmail.com'  
            # other stuff ...
这是我的api学生测试。py

import pytest
from django.contrib.auth import get_user_model


@pytest.fixture
def create_user(db):
    return get_user_model().objects.create_user('user@gmail.com', 'password')
import pytest
from rest_framework.test import APITestCase, APIClient


    class StudentViewTests(APITestCase):

        user = None

        @pytest.fixture(scope="session")
        def setUp(self, create_user):
            self.user = create_user

        def test_create_student(self):
            assert self.user.email == 'user@gmail.com'  
            # other stuff ...
我一直得到以下错误

Fixture "setUp" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.

我读了又读,但找不到解决办法。此外,在这个问题中,夹具没有返回任何内容,而在我的情况下,它应该返回一个对象(不知道它是否能产生任何影响)

只需跳过
设置即可:

@pytest.fixture(scope='session')
def create_user(db):
    return get_user_model().objects.create_user('user@gmail.com', 'password')


class StudentViewTests(APITestCase):

    def test_create_student(self, create_user):
        assert user.email == 'user@gmail.com'  
        # other stuff ...

您应该使用pytest fixture或UnitTest设置函数。两者都用是没有意义的。(注:使用example.com作为测试域等)@thebjorn抱歉,我想我还是错过了一些东西。所以我有两个选择:1-->使用装置创建用户;2-->在设置方法中创建该用户。对的但是在选项2的情况下,我应该为每个需要用户对象的测试重写相同的代码,不是吗?如果不从头重写,就无法重用逻辑在安装程序中创建用户?它不起作用,失败,出现以下错误
errepiuapp/tests/mock_tests.py::StudentViewTests::test_create_student-TypeError:test_create_student()缺少1个必需的位置参数:“create_user”
您是如何运行测试的?我已将您的代码段复制到一个文件中,然后运行了
pytest-vs