密切相關的:In python, is there a good idiom for using context managers in setup/teardownpy.test - 如何使用上下文管理器在funcarg /夾具
我有一個在測試中使用固定的時間/時區上下文管理。我想要一個pytest funcarg(或夾具,我們使用pytest
2.2.3,但我可以向後翻譯)。我可以這樣做:
def pytest_funcarg__fixedTimezone(request):
# fix timezone to match Qld, no DST to worry about and matches all
# Eastern states in winter.
fixedTime = offsetTime.DisplacedRealTime(tz=' Australia/Brisbane')
def setup():
fixedTime.__enter__()
return fixedTime
def teardown(fixedTime):
# this seems rather odd?
fixedTime.__exit__(None, None, None)
...但它有點噁心。在相關的Q jsbueno中指出:問題是如果發生異常,代碼沒有規定可以正確調用對象的__exit__
方法。
His answer使用元類方法。但是這對於pytest來說並不是那麼有用,在pytest中,測試通常只是函數,而不是類。那麼解決這個問題的最好方法是什麼?涉及runtest hooks的內容?
我擔心的不是'__exit__'不能被調用,但它不會被[右值]調用(http://docs.python.org/2/reference/datamodel的.html#上下文經理)。 '__exit__'通常被調用,其值與'with'塊中提出的任何異常有關(或者在這種情況下它將是測試主體)。 – pfctdayelise 2013-04-05 01:19:02