当单元测试失败时是否有抛出许多类似错误消息的约定?

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

我正在编写单元测试,并且提出了许多类似的错误消息,以至于代码看起来非常混乱。

类似这样的:

# Test that compare_pass1 is downstream to secondary_pass1
self.assertTrue(
     all(task in all_downstream_task_for_secondary_pass1_task for task in {compare_pass1}), f'One of {compare_pass1} not found in downstream to {secondary_pass1_task task}')

# Check that primary_pass1 is upstream to secondary_pass1
self.assertTrue(
   all(task in all_upstream_task_for_secondary_pass1_task for task in {primary_pass1_task}), f'One of {primary_pass1_task} not found in upstream to {secondary_pass1_task} task')

# Check that secondary_pass1 is upstream to compare_pass1
self.assertTrue(
   all(task in all_upstream_task_for_compare_pass1_task for task in {secondary_pass1_task}),f'one of {secondary_pass1_task} not found in upstream to {compare_pass1_task} task')

我指的是单元测试失败时抛出的错误消息,虽然不完全一样,但是消息是类似的。

是否有创建函数并在抛出错误消息时调用该函数的约定?

python unit-testing
1个回答
0
投票

您可以使用非 f 字符串格式来定义字符串模板,然后用变量填充:

compare_pass1 = "foo"
secondary_pass1_task = "bar"

s = 'One of {} not found in downstream to {}'

print(s.format(compare_pass1, secondary_pass1_task))

compare_pass2 = "fee"
secondary_pass2_task = "bas"

print(s.format(compare_pass2, secondary_pass2_task))
© www.soinside.com 2019 - 2024. All rights reserved.