测试数据库在 Django Channels Pytest 测试期间不保留数据

Test database not keeping data during Django Channels Pytest test

我正在为 documentation 之后的 Django Channels 应用程序编写测试。我可以成功测试与 WebSocket 消费者的连接和通信。但是,在其中一项测试中,我需要创建一个用户。

这是我的测试方法:

@pytest.mark.django_db
@pytest.mark.asyncio
async def test_1():
    my_user = get_user_model().objects.create_user(username='my_user', email='user@user.com', password='123456')
    my_user.save()

    print(User.objects.all())

    communicator = WebsocketCommunicator(MyConsumer, '/websocket/')
    communicator.instance.scope['user'] = my_user
    connected, subprotocol = await communicator.connect()
    assert connected
    await communicator.send_json_to({
        'command': 'test',
    })
    response = await communicator.receive_json_from(timeout=5)
    await communicator.disconnect()

错误发生在处理 'command' == 'test' 案例的方法内部。当它试图保存另一个将 User 作为外键的模型实例时,测试失败。

client = Client(user=scope['user'])
client.save()

如果我在消费者内部的方法中添加一个 print(User.objects.all())(在前两行之间),它 returns 一个空的 QuerySet,而测试方法中的第一个 print本身 returns 创建的用户。当然,由于没有用户,测试失败:

django.db.utils.IntegrityError: insert or update on table "chat_client" violates foreign key constraint "chat_client_user_id_d10928fc_fk_auth_user_id"
DETAIL:  Key (user_id)=(1) is not present in table "auth_user".

我尝试将用户创建转换为另一种异步方法并 await 尝试它,但没有任何改变。为什么 User table 在测试执行期间被清空?如何保留测试开始时创建的数据?

正如@hoefling 在评论中指出的那样,唯一缺少的是使测试具有事务性,如 中所示。

在我的例子中,我只需要将测试装饰器更改为:

@pytest.mark.django_db(transaction=True)