如何在尝试断言块(或将断言块插入循环)中插入for循环,以避免重复断言?

时间:2019-07-14 16:19:46

标签: python for-loop testing refactoring assert

我想避免针对以下每种情况重复一次断言语句:

na000 ='567890123456789012345678901234567890123456789012345678901234567890'
na003 = '45678901234567890123456789012345678901234567890123456789012345678'
na099 = '012345678901234567890123456789012345678901234567890123456789012345'
na098 = '67890123456789012345678901234567890XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
na050 = '12XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
na002 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

我特别想避免重复这样的断言:

try:
    assert levenshteinize(na000, nai03, 1)[0] == 4, 'testing 3 substitut and 1 insert error' 
    assert levenshteinize(na000, no099, 1)[0] == 99, 'testing 99 omisions error'
    assert levenshteinize(na000, no098, sil)[0] == 98, 'testing 98 omisions error'
    assert levenshteinize(na000, no050, sil)[0] == 50, 'testing 50 omisions error'
    assert levenshteinize(na000, no002, sil)[0] == 2, 'testing 2 omisions error'
except AssertionError as e
...

我已将字符串插入到字典中,并以键作为var名称(数字)。但是重要的是,为了避免断言,我创建了一个在try... assert... except中包含for-loop的函数。我尝试了不同的版本,类似于下面的版本,但是似乎都没有问题(运行之前检测到无效的语法错误)。 编辑:我只是想念一个简单的def。正在考虑是否要立即删除问题...

def test_lev(nerrors = defaultlist, silent = 0, levreturn: int = 0):

try:
    for num in nerrors: 
        assert nnocrate.levenshteinize(na['000'], na[numto3st(num)], silent)[levreturn] == num, f'testing {num} error'
except AssertionError as e:
    print(f'levenshtein() has failed with na when {e.args[0]}\n')
else:
    print('\nAll na assert OK')

我该怎么做?我该如何插入for循环或其他东西以避免重复断言?

0 个答案:

没有答案