2017-01-11 7 views
1

이 질문을하기 전에 (Force nose2 to use Python 2.7 instead of Python 3.5) 물어 보았지만 대답을 얻지 못했고 다른 시도를 할 수 있다고 생각했습니다. 나는 명령nose2를 사용하여 Python 2 코드를 테스트하는 방법

nose2 

를 사용하여 테스트를 실행하기 위해 노력하고있어하지만 난이에 반해, 그것은 nose2처럼 보인다

SyntaxError: Missing parentheses in call to 'print' 

로 끝나는 코드를 파이썬 3에 있다고 가정 오류를 받고 있어요 파이썬 2에있는 경우 nose2을 파이썬 2 코드에서 사용할 수있는 방법이 있습니까? (예를 들어 구성을 변경하여)?

+2

파이썬 2.6, 2.7을 지원한다 [이 (http://nose2.readthedocs.io/en/latest/differences.html) 문서 nose2 따르면 , 3.2 이상. 파이썬 2 인터프리터를 사용하고 있습니까? python2와 python3 사이에는 많은 차이점이 있습니다 (http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html). 언급 한 구문 오류는 python3 해석기로 python2 코드를 실행 해보십시오. –

+0

http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime 다음에'print (sys.version_info [0])'를 포함하는 스크립트를 사용하여 파이썬 버전을 검사하고'2 '. –

+0

덧붙여 말하자면, '있는 그대로'작동하는 또 다른 테스트 프로그램은 [pytest] (http://doc.pytest.org/en/latest/)입니다. 그것의 테스트 발견 방법은 약간 다르다. (예를 들어,'test_'로 시작하거나'_test'로 끝나기 위해 테스트 스크립트의 이름을 바꾸어야했다.) 터미널 출력은'nose2'보다 더 사용자 친화적이다. –

답변

3

nose2는 shebang 행에 구성된 모든 파이썬을 가져옵니다.

는 (실행 파일과 경로를 컴퓨터에 다를 수 있습니다)를 python2 프로젝트 사용을 테스트하려면 :

test.py :

def test_the_program(): 
    print "foo" 

python2.7 /usr/local/bin/nose2 

이 예 확인 with python3 :

python2.7와
$ python3 /usr/local/bin/nose2 
====================================================================== 
ERROR: test (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: test 
    (...) 
    print "hans" 
      ^
SyntaxError: Missing parentheses in call to 'print' 


---------------------------------------------------------------------- 
Ran 1 test in 0.000s 

FAILED (errors=1) 

:

$ python2.7 /usr/local/bin/nose2 
foo 
. 
---------------------------------------------------------------------- 
Ran 1 test in 0.000s 

OK