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

大概是这样的:

# 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',
)

我所关注的是当单元测试失败时抛出的错误消息,虽然它们不完全相同,但消息是相似的.

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

推荐答案

您可以使用non-f-string formatting来定义一个字符串模板,然后用变量填充该模板:

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))

Python相关问答推荐

@Property方法上的inspect.getmembers出现意外行为,引发异常

如何删除索引过go 的lexsort深度可能会影响性能?' &>

在Google Colab中设置Llama-2出现问题-加载判断点碎片时Cell-run失败

Pandas 都是(),但有一个门槛

NP.round解算数据后NP.unique

当我try 在django中更新模型时,模型表单数据不可见

在嵌套span下的span中擦除信息

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

基于另一列的GROUP-BY聚合将列添加到Polars LazyFrame

pysnmp—lextudio使用next()和getCmd()生成器导致TypeError:tuple对象不是迭代器''

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

Pandas—堆栈多索引头,但不包括第一列

pandas:在操作pandora之后将pandora列转换为int

python的文件. truncate()意外地没有截断'

如何使用加速广播主进程张量?

来自Airflow Connection的额外参数

TypeError:';Locator';对象无法在PlayWriter中使用.first()调用

以极轴表示的行数表达式?

如何在Python中画一个只能在对角线内裁剪的圆?

基于2级列表的Pandas 切片3级多索引