質問

In my code, I have a line that says something like

print("Some string:", end=" ")

When I try to run pytest, I get the following:

ryansmacbook:scripts ryan$ py.test file.py
============================= test session starts ==============================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2
collected 0 items / 1 errors 

==================================== ERRORS ====================================
________________________ ERROR collecting cpp-allman.py ________________________
/Library/Python/2.7/site-packages/pytest-2.5.2-py2.7.egg/_pytest/python.py:451: in _importtestmodule
>           mod = self.fspath.pyimport(ensuresyspath=True)
/Library/Python/2.7/site-packages/py-1.4.20-py2.7.egg/py/_path/local.py:620: in pyimport
>           __import__(modname)
E             File "/Users/ryan/path/to/file.py", line 65
E               "Some string:", end=" ")
E                                     ^
E           SyntaxError: invalid syntax
=========================== 1 error in 0.07 seconds ============================

When I comment out the print statement, testing takes forever. I'm trying to test regexes (Testing regexes in Python using py.test), but this is what happens:

ryansmacbook:scripts ryan$ py.test file.py
============================= test session starts ==============================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2
collecting 0 items^C    
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/Users/ryan/magzor/scripts/file.py:134: KeyboardInterrupt
=============================  in 2397.55 seconds ==============================

Before I implemented that possible solution, testing took between 30 and 60 seconds, which still seems too long. What's going on?

========================================

Edit: I commented out a part of my code that read from one file and wrote to another but was not contained within a test_ prefixed function. Pytest now runs in about 0.03 seconds. Why is that? I thought tests were independent of program function.

役に立ちましたか?

解決

You need to have py.test installed in a python3 environment. A #! line with python3 will only be picked up if the script is run by the OS directly (i.e. ./file.py).

To be sure you're invoking the correct version of py.test you could invoke it as python3 -m pytest. Note the second line of py.test's output when running tests where it shows exactly which version of python is being used (2.7.5 in your case).

The speed issue is probably a separate question hard to answer without seeing the code involved. Somehow the commented out code must have been triggered before. This would be a mistake most likely at import time as py.test does not randomly start to run code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top