为什么我的循环没有迭代?

时间:2013-04-12 15:50:56

标签: python unit-testing metaprogramming

为什么外循环只执行最后一次迭代?

class LanguageSpecificTest(unittest.TestCase):

    # Body omitted

for style in [  "inline", "inline_scripted",
                "external", "external_scripted",
                "internal", "internal_scripted",
            ]:
    print style

    for language, text_id, direction, text, check_text in (
                ("french", "reply_1", "с французского", "жизнь", "В большом дворце, в Ферраре, в один зимний вечер"),
            ):

        test_name = 'test_translation_test_phrases_for_%s_pages' % language
        def my_test_generator(language, text_id, check_text):
            def ubergenerator(self):
                #####
            return ubergenerator
        t = my_test_generator(language, text_id, check_text)
        t.__name__ = test_name
        setattr(LanguageSpecificTest, test_name, t)

当我启动测试时,它会打印出来:

inline
inline_scripted
external
external_scripted
internal
internal_scripted
http://127.0.0.1:5000/translate?lang=french&style=internal_scripted

只进行一次测试,而不是六次测试。我为什么不看

http://127.0.0.1:5000/translate?lang=french&style=inline
http://127.0.0.1:5000/translate?lang=french&style=inline_scripted
http://127.0.0.1:5000/translate?lang=french&style=external
http://127.0.0.1:5000/translate?lang=french&style=external_scripted
http://127.0.0.1:5000/translate?lang=french&style=internal
http://127.0.0.1:5000/translate?lang=french&style=internal_scripted

天哪,除了星期五晚上。

1 个答案:

答案 0 :(得分:3)

    test_name = 'test_translation_test_phrases_for_%s_pages' % language
    setattr(LanguageSpecificTest, test_name, t)

您只在language中使用test_name,因此当您转到不同的style时,您会覆盖相同的属性。