2017-09-04 92 views
0

我正在编写一些测试,依靠用户输入来决定它们是否已通过。使pytest等待用户输入

我有这样的功能:

def viewable(actual_proj): 
    print("\nCan you see %s projects named:\n"%len(actual_proj)) 
    for i in actual_proj: 
     print (i+"\n") 
    return input("(y/n)? : ") 

在:

def is_present(pytestconfig, project_not_present = 0): 

    actual_projects = all_projects.copy() 
    if (project_not_present!=0): 
     del_file = all_ini_files[project_not_present-1] 
     os.rename(del_file, del_file +'_tst') 
     del actual_projects[project_not_present-1] 
    capmanager = pytestconfig.pluginmanager.getplugin('capturemanager') 

    subprocess.Popen('./MultiPRM.exe') 
    capmanager.suspendcapture(in_=True) 

    decision = viewable(actual_projects) 
    capmanager.resumecapture() 
    if (project_not_present!=0): 
     os.rename(del_file+'_tst', del_file) 
    if (decision =='y'): 
     return True 
    else: 
     return False 

当我运行命令pytest name_of_test_file.py它运行良好,并且在每次试验后停下来获取用户输入。但是,我想用它设置了各种变量和标头日志文件(称为run_tests.py)的文件

# start the report 
print("Creating test report: " + os.path.abspath(report_filepath)) 
rep = open(report_filepath, "w") 
rep.write(report_header) 
rep.write("Test environment: \n"); 
rep.write(" Username: " + os.environ['USERNAME'] + "\n") 
rep.write("Testing started at: " + get_time() + "\n\n") 
rep.close() 

# get application version 
cmd = exe_under_test + " --help >> " + report_filepath 
os.system(cmd) 

# start the tests 
cmd = "pytest >> " + report_filepath 
os.system(cmd) 

# finalise the report 
rep = open(report_filepath, "a+") 
rep.write("\nTesting completed at: " + get_time() + "\n\n") 
rep.close() 

当我这样运行它,它不会停止或运行任何测试。

如果我可以写入日志文件,同时也写入相同的东西到终端(包括用户输入),这将是伟大的。否则,正确调用这个函数的方法也会起作用。

+3

单元测试的要点是它们不需要用户交互... –

+0

此功能可以被测试的唯一方法就是这样,因为它可能是不正确的 –

+1

您真的需要找到一种方法来模拟用户输入用于测试目的。如果您在测试过程中依赖用户输入,那么运行测试的其他人可能不会测试与您相同的内容。测试应该是确定性的。 – larsks

回答

2

你的测试应该尽可能的简单,让它可以随意执行。如果他们将依赖于外部(例如用户)输入和其他一些技巧来正常运行,那么从长远来看,没有人会执行它们。

如果你在你也许可以忍受它的项目的唯一开发商,但我要说,这种做法是不正确,不认为是最佳实践

应用:

的一切,如果你只是在控制台(这似乎是从你的代码段),等待用户的输入,那么就嘲笑input内置并将其设置为返回值,例如第一的.py

def my_input(prompt=''): 
    try: 
     return raw_input(prompt) 
    except NameError: 
     return input(prompt) 


def my_great_func(): 
    choice = my_input('y/n?: ') 
    return choice 

test.py

import unittest.mock as mock  
import pytest 

from app import my_great_func 

@pytest.yield_fixture 
def fake_input(): 
    with mock.patch('app.my_input') as m: 
     yield m 


def test_my_great_func(fake_input): 
    fake_input.return_value = 'y' 
    assert my_great_func() == 'y' 

测试执行:

$ pytest test.py -v 
============================= test session starts ============================== 
platform linux -- Python 3.5.2, pytest-3.2.1 
cachedir: .cache 
rootdir: /home/kris/projects/tmp, inifile: 
collected 1 item                 

test.py::test_my_great_func PASSED 

=========================== 1 passed in 0.01 seconds =========================== 

其次努力写您的应用程序逻辑和GUI是松耦合的代码 - 这样你就可以测试你的逻辑,无论在图形用户界面(无论是Web,桌面或移动应用) 。