[問題] 如何測試 sub function

作者: chan15 (ChaN)   2018-05-18 13:57:47
# -*- coding: utf-8 -*-
def run():
def the_name():
if not hasattr(the_name, 'name'):
the_name.name = 'test'
return the_name.name
for i in range(3):
print(the_name())
run()
print('')
run()
print('')
run()
各位好,我想針對上面的 code 寫單元測試
主要是想判斷第一次執行時還沒有 the_name.name
執行第一次之後便存在這個 property 因此直接回覆 the_name.name
本是想用 self.assertFalse(getattr(run.the_name.name))
不過顯然不能這樣用,不知道怎麼可以達成需求
作者: ThxThx (洗洗睡)   2018-05-19 05:48:00
個人認為sub function到需要測的程度是架構的問題....例如說改寫成function factory就能測回傳的function了

Links booklink

Contact Us: admin [ a t ] ucptt.com