【发布时间】:2017-09-04 11:08:49
【问题描述】:
我正在编写一些依赖于用户输入来决定它们是否通过的测试。
我有这个功能:
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()
当我以这种方式运行它时,它不会停止或运行任何测试。
如果我可以写入日志文件,同时也可以将相同的内容写入终端(包括用户输入),那就太好了。否则,正确调用此函数的方法也将起作用。
【问题讨论】:
-
单元测试的重点在于它们不需要用户交互...
-
唯一可以测试这个函数的方法就是这样,可能不正确
-
您确实需要找到一种方法来模拟用户输入以进行测试。如果您在测试期间依赖用户输入,那么运行您的测试的其他人可能不会与您测试相同的东西。测试应该是确定性的。
-
@PeterKentish 编写您自己的
input()实现,返回确定性结果。 -
@NilsWerner 你是什么意思?它在 GUI 中测试一些东西,我没有 GUI 测试软件。
标签: python user-input pytest gui-testing