我目前正在編寫一個與竹構建服務器交互的小lib。測試使用pytest完成。我被困在以下問題。我想測試一個while循環,直到某些狀態滿足爲止。閱讀pytest文檔,我試圖「嘲笑」/ monkeypatch的狀態,但它並沒有真正的工作。我可能做一些基本的錯誤在這裏: 這是有問題的,而循環:如何使用pytest測試無限while循環
# determine current status
running = self._is_a_build_running()
# turn on and off running powerplug while building
while running:
self.feedback.turn_off_success()
self.feedback.turn_on_running()
time.sleep(self.blinker_time)
self.feedback.turn_off_running()
self._update_builds_status()
running = self._is_a_build_running()
所以我試着用pytest是創造一個夾具,用於積極的,像這樣的負面_is_a_build_running
:
@pytest.fixture(scope='function')
def mock_is_a_build_running():
return False
然後使用這個測試方法使用ThreadPool(在這裏解釋how to get the return value from a thread in python?),因爲我也需要包含while循環的方法的結果。
def test_update_status_running(bamboopickups, monkeypatch,
mock_update_overall_data_positive,
mock_update_builds_status_positive,
mock_is_a_build_running):
monkeypatch.setattr('BambooPickup._update_overall_data', lambda x: mock_update_overall_data_positive)
monkeypatch.setattr('BambooPickup._update_builds_status', lambda x: mock_update_builds_status_positive)
pool = ThreadPool(processes=1)
async_result = pool.apply_async(bamboopickups.update_status())
monkeypatch.setattr('BambooPickup._update_overall_data', lambda x: mock_update_overall_data_positive)
monkeypatch.setattr('BambooPickup._is_a_build_running', lambda x: mock_is_a_build_running)
actual = async_result.get()
expected = True
assert actual == expected
這可能是很容易與pytest-模擬完成,但到目前爲止,我只使用此處描述的首選方式:http://pytest.org/latest/monkeypatch.html。
聽起來像是另一個有效的方法來解決這個問題:) – maiksensi