Python 导入语句导致代码覆盖失败
Python Code Coverage Failing with import statements
我有以下项目结构:
v/b.py
v/e.py
v/e_test.py
v/p.py
v/__init__.py
#v/e.py
from b import B
from p import P
class E(object):
def run(self): pass
#v/p.py
class P(object):
def run(self): pass
#v/b.py
from p import P
class B(object):
def run(self): pass
#v/e_test.py
from e import E
import unittest
class ETest(unittest.Testcase):
def testSomething(self): pass
if __name__ == '__main__':
unittest.main()
那我运行这个pip3 install coverage
.
然后coverage run e_test.py
它给我这个错误
Traceback (most recent call last):
File "e_test.py", line 4, in <module>
from e import E
File "~/v/e.py", line 1, in <module>
from b import B
File "~/v/b.py", line 1, in <module>
from p import P
ImportError: cannot import name 'P' from 'p' (/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/p.py)
我现在的问题是如何解决这个问题。我也希望能够 运行:
- 覆盖率运行e_test.pyb_test.pyfiles_test.py
- python3 e_test.py
- python3 e.py
mm,我想 P Class 还没有声明,所以你得到了这样的错误
#v/p.py
class P(object):
def run(self): pass
你还没有定义P是什么,报错p.py没有P对象
终于找到答案了。如果我在包含 v 文件夹的父文件夹中,我可以 运行 以下命令。
PYTHONPATH=./v:$PYTHONPATH coverage run -m unittest discover -s v -p "*_test.py"
或无覆盖
PYTHONPATH=./v:$PYTHONPATH python3 -m unittest discover -s v -p "*_test.py"
如果我在 v 文件夹中,我可以 运行
python3 e.py
和
python3 e_test.py
我有以下项目结构:
v/b.py
v/e.py
v/e_test.py
v/p.py
v/__init__.py
#v/e.py
from b import B
from p import P
class E(object):
def run(self): pass
#v/p.py
class P(object):
def run(self): pass
#v/b.py
from p import P
class B(object):
def run(self): pass
#v/e_test.py
from e import E
import unittest
class ETest(unittest.Testcase):
def testSomething(self): pass
if __name__ == '__main__':
unittest.main()
那我运行这个pip3 install coverage
.
然后coverage run e_test.py
它给我这个错误
Traceback (most recent call last):
File "e_test.py", line 4, in <module>
from e import E
File "~/v/e.py", line 1, in <module>
from b import B
File "~/v/b.py", line 1, in <module>
from p import P
ImportError: cannot import name 'P' from 'p' (/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/p.py)
我现在的问题是如何解决这个问题。我也希望能够 运行:
- 覆盖率运行e_test.pyb_test.pyfiles_test.py
- python3 e_test.py
- python3 e.py
mm,我想 P Class 还没有声明,所以你得到了这样的错误
#v/p.py
class P(object):
def run(self): pass
你还没有定义P是什么,报错p.py没有P对象
终于找到答案了。如果我在包含 v 文件夹的父文件夹中,我可以 运行 以下命令。
PYTHONPATH=./v:$PYTHONPATH coverage run -m unittest discover -s v -p "*_test.py"
或无覆盖
PYTHONPATH=./v:$PYTHONPATH python3 -m unittest discover -s v -p "*_test.py"
如果我在 v 文件夹中,我可以 运行
python3 e.py
和
python3 e_test.py