2017-09-04 2 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

unittest의 요점은 사용자 상호 작용이 필요하지 않다는 것입니다. –

+0

이 기능을 테스트 할 수있는 유일한 방법은 다음과 같습니다. 정확하지 않은 경우 –

+1

사용자를 시뮬레이트하는 방법을 찾아야합니다 테스트 목적을위한 입력. 테스트 중 사용자 입력에 의존한다면 테스트를 실행하는 다른 사람이 당신과 동일한 것을 테스트하지 않을 수 있습니다. 테스트는 결정 론적이라고 가정합니다. – larsks

답변

2

테스트를 실행하기가 쉽기 때문에 테스트를 실행하는 것이 쉽습니다. 외부 (예 : 사용자)의 입력과 제대로 실행되는 다른 트릭에 의존하는 경우 아무도 장기적으로 실행하지 않습니다.

프로젝트에서 독점 개발자가 있다면 함께 참여할 수 있지만이 접근법은 정확하지 않으며 모범 사례으로 간주되지 않습니다.

응용 프로그램 : 당신은 그냥 (코드 조각에서처럼 보인다) 콘솔에서 사용자의 입력을 기다리는 모든 경우에, 그럼 그냥 예를 들어, input 내장을 조롱하고 값을 반환 설정의

우선 평

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' 

테스트 실행 : - (웹, 데스크톱 또는 모바일 앱이라고해도) 당신이 GUI에 관계없이 당신의 논리를 테스트 할 수 있습니다이 방법을

$ 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가 느슨하게 결합 된 코드를 작성하기 위해 노력하고 .

관련 문제