Django新手。我正在尝试为我开发的一个简单的API实现单元测试。下面您可以找到我的测试实现,它运行良好:
from django.test import TestCase
from my_app.models import MyModel
class TestMyViewSet(TestCase):
"""
Unit tests for endpoints in MyViewSet.
"""
fixtures = ['my_app/fixtures/data.yaml']
def setUp(self):
# Making setup for the test case here.
def test_post_my_resource(self):
# Checking that fixture is loaded correctly.
self.assertEqual(MyModel.objects.all().count(),1)
request_body = {
'my_resource': "input_for_my_resource"
}
response = self.client.post('/my_resources/', data=request_body)
self.assertEqual(response.status_code, 201)
# self.assertEqual(MyModel.objects.all().count(),2)
但是,当我从注释中删除最后一行self.assertEqual(MyModel.objects.all().count(),2)
以测试{
TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
我错过了什么?在
提前谢谢!在
附言:我遇到了以下问题:TransactionManagementError “You can't execute queries until the end of the 'atomic' block” while using signals, but only during Unit Testing但我不确定我的情况是否相同。在
显然,从}解决了这个问题。关于}之间的区别,以下是一些重要的要点:
django.test.TestCase
转移到{django.test.TestCase
和{在这里,您可以从文档TransactionTestCase中找到更多详细信息
相关问题 更多 >
编程相关推荐