Unit Test with same class names failing

I'm having a weird issue and couldn't find anything similar on Google.

I have a python project with unit tests. Basically the project has a folder architecture that looks like this (leaving aside non-relevant files):

project_name/__init__.py

project_name/common/__init__.py

project_name/common/some_lib.py

project_name/common/utils/__init__.py

project_name/common/utils/some_lib.py

tests/__init__.py

tests/common/__init__.py

tests/common/test_some_lib.py

tests/common/utils/__init__.py

tests/common/utils/test_some_lib.py


The important part is that we have modules called project_name.common.some_lib and project_name.common.utils.some_lib
with "some_lib" being the same word for both.

Each test_some_lib.py contains code that looks like this:

tests/common/test_some_lib.py:

from unittest import TestCase

class TestSomeLib(TestCase):

def test1(self)

 

tests/common/utils/test_some_lib.py:

from unittest import TestCase

class TestSomeLib(TestCase):

def test2(self)

 

The problem is that when we ask PyCharm to run test2, we get an error that says:

Error
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/usr/lib/python3.6/unittest/loader.py", line 34, in testFailure
raise self._exception
File "/usr/lib/python3.6/unittest/loader.py", line 168, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: type object 'TestSomeLib' has no attribute 'test2'

It looks like there is a confusion between the two classes tests.common.TestSomeLib and tests.common.utils.TestSomeLib and that it loads the first one instead of the second. When we change the name of either class, the unit tests runs without any problem.

Any help would be appreciated...

Thanks

Please sign in to leave a comment.