Skip to main content
 首页 » 编程设计

python之测试具有用户输入的 python 函数的问题 (pytest)

2024年11月24日7www_RR

我的测试有一些问题。我想用一些简单的脚本(在这种情况下是 BMI 计数器)开始我的 pytest 冒险。我想测试一次功能,但测试将进入所有功能。如果我对输入值进行了注释,则测试通过。

输出:

$ pytest test_bmi.py 
=========================================================================================== test session starts ============================================================================================ 
platform darwin -- Python 3.7.1, pytest-4.0.2, py-1.7.0, pluggy-0.8.0 
rootdir: /Users/mateusz/Documents/Code_Me_Python/zajecia_python/Zajecia_1, inifile: 
plugins: remotedata-0.3.1, openfiles-0.3.1, doctestplus-0.2.0, arraydiff-0.3 
collected 0 items / 1 errors 
 
================================================================================================== ERRORS ================================================================================================== 
_______________________________________________________________________________________ ERROR collecting test_bmi.py _______________________________________________________________________________________ 
test_bmi.py:1: in <module> 
    from bmi import count_bmi 
bmi.py:14: in <module> 
    main() 
bmi.py:11: in main 
    (mass, height) = users_data() 
bmi.py:2: in users_data 
    mass = float(input("Your weight: ")) 
../../../../anaconda3/lib/python3.7/site-packages/_pytest/capture.py:656: in read 
    raise IOError("reading from stdin while output is captured") 
E   OSError: reading from stdin while output is captured 
--------------------------------------------------------------------------------------------- Captured stdout ---------------------------------------------------------------------------------------------- 
Your weight: 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
========================================================================================= 1 error in 0.16 seconds ========================================================================================= 

我已经尝试过“monkeypatch”(如代码片段中所述),将值输入变量以将它们传递给测试。没有任何帮助。

体重指数

def users_data(): 
    mass = float(input("Your weight: ")) 
    height = float(input("Your height: ")) 
    return mass, height 
 
def count_bmi(mass, height): 
    bmi = round(mass / (height**2), 2) 
    return bmi 
 
def main(): 
    (mass, height) = users_data() 
    print(count_bmi(mass, height)) 
 
main() 

test_bmi.py

from bmi import count_bmi 
 
def test_count_bmi(monkeypatch): 
    ans1 = '60' 
    ans2 = '1.7' 
    ans3 = '20.76' 
    with monkeypatch.context() as m: 
        m.setattr('builtins.input', lambda x: ans1, ans2) 
        result = count_bmi(ans1, ans2) 
 
    assert result == ans3 

如果我注释输入变量

 def test_count_bmi(self): 
     count_bmi.input = lambda: '' 
     output = count_bmi(60, 1.7)   
     assert output == '20.76' 

我希望通过考试。

请您参考如下方法:

问题是模块级别的调用main()。这将在 bmi 模块导入时执行 main 函数;使用常见的习语来规避这一点:

# bmi.py 
 
def users_data(): 
    ... 
 
def count_bmi(mass, height): 
    ... 
 
def main(): 
    ... 
 
if __name__ == "__main__": 
    # execute only if run as a script 
    main() 

见题目__main__ — Top-level script environment在 Python 文档中,如果你想了解更多;另外,SO 对此有一个很好的问题:What does if __name__ == “__main__”: do?