2017-04-23 96 views
0

我正在爲系統編寫集成測試,我可以通過Web服務調用自動化大部分測試,但由於遺留問題我無法更改,因此我需要完成幾個步驟通過手動測試人員。如何添加stdin與pytest的交互

我想使用pytest並創建一個基本上暫停測試執行並提示控制檯輸入的夾具(例如「在系統中執行XYZ;在完成時鍵入'完成')並繼續測試的其餘部分。

我固然沒有做過一噸黑客對這個還沒有,但我從pytest文檔看到:

...標準輸入被設置爲「空」對象,它將在嘗試失敗 從中讀取,因爲在運行自動化測試時很少需要等待交互式 輸入。

除了在我的情況,我真的想等待,除此之外,我的東西看起來是pytest的一個很好的用例。

仍在搜索提示的interwebs,但如果有人已經通過這個障礙已經我很想知道。

+0

注意:在發佈之前,我感覺很糟糕,不會嘗試使用sys.stdin,這將是我重新連接時所做的第一件事情(只是爲了確認我的擔憂已建立) –

回答

1

截至3版本,你可以temporarily disable捕獲:

def test_input(capsys): 
    with capsys.disabled(): 
     input("hit enter to continue: ") 
    print("this line is invisible as normal") 

(py36) [email protected]:~/coding$ py.test -v stdin.py 
========================================== test session starts =========================================== 
platform linux -- Python 3.6.0, pytest-3.0.7, py-1.4.32, pluggy-0.4.0 -- /home/dsm/sys/miniconda3/envs/py36/bin/python 
cachedir: .cache 
rootdir: /home/dsm/coding, inifile: 
plugins: cov-2.3.1 
collected 1 items 

stdin.py::test_input hit enter to continue: [here I hit enter] 
PASSED 

======================================= 1 passed in 23.11 seconds ========================================