python - Django - 编写一个在所有测试用例之前运行一次的测试函数
问题描述
我在这里有这段代码,它在表中创建条目,因为它们是创建新帖子所必需的。您需要一个用户以及一个目标和目标类别。我听说 setUp() 在每次测试之前都会运行,所以这是一个问题,因为它可以尝试已经存在的出色实例。我想在运行所有测试之前运行一次 setUp() 。我该怎么做呢?
class PostTest(TestCase):
def setUp(self) -> None:
GoalCategory.objects.create(category='other', emoji_url='url')
self.user = UserFactory()
self.goal = GoalFactory()
def test_create_post(self):
response = self.client.post(reverse('post'),
data=json.dumps({'creator_id': str(self.user.uuid),
'goal_id': str(self.goal.uuid),
'body': 'Some text #Test'}),
content_type='application/json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_no_goal_create_post(self):
response = self.client.post(reverse('post'),
data=json.dumps({'creator_id': str(self.user.uuid),
'goal_id': 'some messed up UUID',
'body': 'Some text #Test'}),
content_type='application/json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_no_user_create_post(self):
response = self.client.post(reverse('post'),
data=json.dumps({'creator_id': 'messed up user',
'goal_id': str(self.goal.uuid),
'body': 'Some text #Test'}),
content_type='application/json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
解决方案
推荐阅读
- javascript - 基于对数和纬度计算最近位置(来自数据库)的算法
- matlab - 如何获得 1 像素宽的骨架?
- c# - 如何使用 LibGit2Sharp 查看文件的简化历史记录?
- c# - VStudio 2019 中缺少“合并工具中的更改”、“获取服务器版本”等按钮
- service - inf ServiceBinary - 带空格的引用路径
- install4j - 使用外部软件读取 ApplicationID
- r - 按数字对列名进行排序
- azure-cosmosdb - CosmosDB 中的索引数组
- c# - 用 Sprache 解析短语(以空格分隔的单词)
- duration - 如何在 splunk 的一个字段中实现值 1 和 0 之间的持续时间的计算?