Python:在一个函数中定义的变量在另一个函数中无法识别

2024-06-28 15:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

class TestUM(unittest.TestCase):
    def test_Dynasty_init(self):
        try:
            self.p = ModelInterface(r"E:\HIL\07_Tests\AT_Dynasty_models\AT745\RTM_AT_745.dml") #any dml file should do here
        except Exception:
            self.fail("Dynasty initialization raised ExceptionType unexpectedly!")

    def test_Dynasty_check(self):
        self.p.check()

在第9行,我得到以下错误:

"AttributeError: 'TestUM' object has no attribute 'p'"

我不明白。^在test_Dynasty_check函数中,{}未被识别。你知道吗


Tags: testselfinitdefcheckunittesttestcaseat
2条回答

这可能取决于是否在类实例的test\u dianary\u check()之前调用test\u init()。你知道吗

您应该使用setUp()tearDown()作为初始化/销毁代码。你知道吗

如果我没记错的话,unittest库按字母顺序运行以test开头的所有函数。意思是test_Dynasty_checktest_Dynasty_init之前运行。编辑:顺序无关紧要,因为每个测试都将从TestCase类的新的实例运行。你知道吗

相关问题 更多 >