2017-03-13 2 views
4

pytest (Python 3)를 사용하여 일부 하드웨어 장치 (전자 장치)를 테스트하는 테스트 제품군을 성공적으로 실행하고 있습니다. 이 테스트의 하위 집합 인 경우 테스터가 하드웨어 배열을 변경하고 나중에 다시 변경해야합니다. 내 접근 방식은 두 input 호출로, (별도의 모듈에있는 모든 것이) 문제의 시험에 부착 된 모듈 수준의 비품을 사용하는 것이었다 :pytest가 (수동) 사용자 작업을 기다리는 방법?

@pytest.fixture(scope="module") 
def disconnect_component(): 
    input('Disconnect component, then press enter') 
    yield # At this point all the tests with this fixture are run 
    input('Connect component again, then press enter') 

이 프로그램을 실행할 때, 나는 OSError: reading from stdin while output is captured를 얻을. 나는 pytest를 --capture=no으로 호출함으로써이를 피할 수 있으며, 내 접근법이 작동한다는 것을 확인했습니다. 즉, 문제의 테스트 하위 집합에서 첫 번째 쿼리를 얻었고, 두 번째 쿼리는 실행 한 후 첫 번째 쿼리를 얻음을 의미합니다.

큰 결점은 다른 테스트의 일부가 의존하는 전체 테스트 세트에 대한 stdin/stderr 캡처를 비활성화한다는 것입니다.

나는이

@pytest.fixture(scope="module") 
def disconnect_component(capsys): 
    with capsys.disabled(): 
     input('Disconnect component, then press enter') 
     yield # At this point all the tests with this fixture are run 
     input('Connect component again, then press enter') 

처럼 capsys.disabled (docs)를 사용하려하지만이를 실행할 때 나는 ScopeMismatch: You tried to access the 'function' scoped fixture 'capsys' with a 'module' scoped request object, involved factories를 얻을.

input 이외의 다른 방법으로 사용자 동작을 기다리는 pyTest를 만들 수 있습니까? 그렇지 않은 경우, 위의 조명기를 사용하여 테스트를 캡처하는 기능을 비활성화 할 수 있습니까?

@pytest.fixture(scope="module") 
def disconnect_component(pytestconfig): 
    capmanager = pytestconfig.pluginmanager.getplugin('capturemanager') 

    capmanager.suspendcapture(in_=True) 
    input('Disconnect component, then press enter') 
    capmanager.resumecapture() 

    yield # At this point all the tests with this fixture are run 

    capmanager.suspendcapture(in_=True) 
    input('Connect component again, then press enter') 
    capmanager.resumecapture() 

이 완벽 최대한 멀리 볼 수 작동합니다

답변

5

그래서, 내가있는 나는 기본적으로 capsys.disable() 함수가 무엇을 기반으로하는 pytest dev에 의해 hint을 발견했다. in_=True 비트를 잊지 마세요.

+1

"표준 입력이 자동으로 테스트에 필요하지 않습니다"입니다의 어떤 부분에서이 작업을 수행 할 수 있습니다 stoneage (Fortran 77로 작성). 나는 근원이 없기 때문에 그 (것)들을 다시 쓸 수 없다. 그래서 Popen을 사용하여 파이썬 코드를 작성하여이 오래된 exe를 툴 체인에 붙입니다. 이 오래된 실행 파일은 사용자 입력에 크게 의존합니다. 이는 거의 "계속 입력하라"입니다. 당신의 대답은 나에게 subprocess.communicate를 통한 입력에서 파이프를 사용하고 전체 툴체인을 테스트하기 위해 pytest를 사용하는 opertunity를 제공합니다. – Hatatister

+0

유용한 정보를 제공합니다. 접착제 언어로 사용되는 파이썬이 흔들립니다! :-D – Christoph

1

어쩌면 위의 솔루션이 조명기에있을 필요는 없습니다. 나는 그것을 위해 도우미 함수를 만들었어요 :

import pytest 

def ask_user_input(msg=''): 
    """ Asks user to check something manually and answer a question 
    """ 
    notification = "\n\n???\tANSWER NEEDED\t???\n\n{}".format(msg) 

    # suspend input capture by py.test so user input can be recorded here 
    capture_manager = pytest.config.pluginmanager.getplugin('capturemanager') 
    capture_manager.suspendcapture(in_=True) 

    answer = raw_input(notification) 

    # resume capture after question have been asked 
    capture_manager.resumecapture() 

    logging.debug("Answer: {}".format(answer)) 
    return answer 
+0

그래, 그게 도움이 될 수도있어. 우리의 경우에, 우리는 정착물에서 그것을 필요로합니다 : *이 테스트들의 부분 집합을 위해, 우리는 하드웨어 배열을 변경하기 위해 테스터가 필요하고 나중에 그것을 다시 변경합니다 * – Christoph

0

를 향후 참조를 들어, pytestinput를 사용해야합니다. 나는 일부 실행 파일을 사용해야합니다> 직장에서 - 당신은 등 당신의 pytest, setup_class, test_..., teardown_method,이 pytest > 3.3.x

import pytest 

capture_manager = pytest.config.pluginmanager.getplugin('capturemanager') 
capture_manager.suspend_global_capture(in_=True) 
answer = input('My reference text here') 
capture_manager.resume_global_capture() 
관련 문제