[英]How can I make py.test tests accept interactive input?
我將 py.test 用於一個有點非常規的應用程序。 基本上,我希望通過 print() 和 input()(這是 Python 3.5)在測試中進行用戶交互。 最終目標是對硬件和多層軟件進行半自動化測試,即使在原則上也無法進行自動測試。 一些測試用例會要求測試技術人員做某事(通過控制台上的輸入或按任意鍵或類似方式進行確認)或要求他們進行簡單的測量或視覺確認某事(在控制台上輸入)。
我(天真地)想做的示例:
def test_thingie():
thingie_init('red')
print('Testing the thingie.')
# Ask the testing technician to enter info, or confirm that he has set things up physically
x = int(input('Technician: How many *RED* widgets are on the thingie? Enter integer:'))
assert x == the_correct_number
這適用於使用 pytest -s 調用測試文件以防止 stdin 和 stdout 捕獲,但with capsys.disabled()
文檔中記錄的方法( with capsys.disabled()
)不起作用,因為它們只影響 stdout 和 stderr。
使用py.test 模塊中的代碼,沒有命令行選項,最好是按測試進行這項工作的好方法是什么?
就其價值而言,該平台是 Windows,我不希望有這種破壞或被標准輸入/輸出/嵌套外殼、不常見外殼等導致的任何結果的包裝破壞。
沒有命令行選項
使用 pytest.ini 選項或 env變量避免每次都使用命令行選項。
理想情況下每次測試?
使用函數作用域夾具來獲取用戶輸入。 示例代碼:
# contents of conftest.py
import pytest
@pytest.fixture(scope='function')
def take_input(request):
val = input(request.param)
return val
#Content of test_input.py
import pytest
@pytest.mark.parametrize('prompt',('Enter value here:'), indirect=True)
def test_input(take_input):
assert take_input == "expected string"
有一個包完全符合要求。
https://github.com/wooey/Wooey
Wooey 是一個簡單的 Web 界面,用於運行命令行 Python 腳本。 將其視為在網絡上發布腳本的簡單方法
演示站點: https : //wooey.herokuapp.com/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.