Home > Enterprise >  Python Code Coverage Failing with import statements
Python Code Coverage Failing with import statements

Time:10-22

I have the following project structure:

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

Then I run this pip3 install coverage.

Then coverage run e_test.py

It's giving me this error

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)

My question now is how to fix this. Also I want to be able to run:

  1. coverage run e_test.py b_test.py files_test.py
  2. python3 e_test.py
  3. python3 e.py

CodePudding user response:

mm, I think P Class is not declared yet, so you got an error like that

CodePudding user response:

#v/p.py

class P(object):
   def run(self): pass

You haven't defined what P is, the error is reporting that p.py does not have a P object.

  • Related