简体   繁体   中英

How to test Python 2 code using nose2

I've asked this question before ( Force nose2 to use Python 2.7 instead of Python 3.5 ) but didn't get an answer, and thought I might give it another try. I'm trying to run tests using the command

nose2

but I'm getting an error that ends with

SyntaxError: Missing parentheses in call to 'print'

It seems like nose2 assumes that the code is in Python 3, whereas in this case it is in Python 2. Is there any way to make nose2 work on Python 2 code? (For example by changing its configuration)?

nose2 takes whatever python is configured in the shebang line.

To test a python2 project use (executable and path might differ on your machine):

python2.7 /usr/local/bin/nose2

verified with this example:

test.py :

def test_the_program():
    print "foo"

with python3 :

$ 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)

with python2.7 :

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

OK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM