rustysys-dev
rustysys-dev

Reputation: 863

pytest can import ClassA from File1 but not ClassB from the same file

I have a rather peculiar problem, that I may know the cause, but not the solution.

First I have the following file structure.

../../
├── Folder1
│   ├── Folder2
│   │   ├── File3.py
│   │   ├── __init__.py
│   │   ├── import_test.py
│   │   ├── File2.py
│   │   ├── File1.py
│   │   └── util.pyc
│   ├── __init__.py
│   └── main.py
├── tests
│   ├── func
│   │   └── __init__.py
│   ├── unit
│   │   ├── __init__.py
│   │   ├── test_File2.py
│   │   └── test_File1.py
│   ├── conftest.py
│   ├── pytest.ini
├── __init__.py
└── setup.py

The contents of test_File1.py is similar to the following (although sanitized).

import pytest, signal, json
import Folder1.Folder2.File1 as file1
from Folder1.Folder2.File1 import ClassA
from Folder1.Folder2.File1 import ClassB

@pytest.fixture()
def sample_data1():
    data = "yyyyy"
    return data

@pytest.fixture()
def sample_data2():
    data = 'xxxxx'
    return data

def test_func1(sample_data1):

def test_func2(sample_data1):

class TestClassB():

    def test_func1(self, sample_data2):

When the above test is being loaded collected into pytest, the following error occurs.

=========================================================== ERRORS ===========================================================
__________________________________________ ERROR collecting tests/unit/test_File1.py _________________________________________
ImportError while importing test module '/home/procyclinsur/Documents/Projects/xxxxxx/xxxxxx/xxxxxx/tests/unit/test_File1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../tests/unit/test_File1.py:4: in <module>
    from Folder1.Folder2.File1 import ClassB
E   ImportError: cannot import name ClassB
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================== 1 error in 0.20 seconds ===================================================

That being said, I have a feeling that the problem lies in ClassB's implementation. I had problems before getting class B to work without some finagling. To explain what I mean, below is what is in File1.

import json
from datetime import datetime
from File3 import ClassC as classc

CLSC = classc()

class ClassA(object):

    def __init__(self):
        ...

    def func1(self, var1, var2):
        ...

    def func2(self, var1):
        ...

    def func3(self, var1):
        ...

class ClassB(object):

    def __init__(self, data_dict={}):
        ...

    def func1(self):
        ...

    def func2(self):
        ...

    def func3(self, var1=CLSC.var1):
        ...

    ...


def func1(var1):
    ...

def func2(var2):
    ...

I initially had trouble getting ClassB to even work due to CLSC not being defined (Initially I had ClassC in the same file). I was able to fix this by creating the CLSC object after importing the ClassC code.

Now that the code runs properly from both command line, and the python interpreter, I decided to write some tests for this class unfortunately the tests could not import ClassB... they will import ClassA just fine though... so its not a path issue.. (typically you are supposed to write tests first but... sorry!!)

If anyone has run into this issue, or knows anything about it, I would be grateful! Thank you!

Upvotes: 0

Views: 73

Answers (1)

rustysys-dev
rustysys-dev

Reputation: 863

I found, the answer. It seems I was running the test code just fine, but against an old version of the module. In the previous version of my code, ClassB did not exist, while ClassA did. After updating my code, I should have run the following to install it.

sudo pip install ./ --upgrade

Upon installing the updated module running pytest produced the correct results (because i was using the correct code base).

[procyclinsur@localhost unit]$ pytest
============================================== test session starts =============================================
platform linux2 -- Python 2.7.14, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/procyclinsur/Documents/Projects/xxxxxx/xxxxxx/xxxxxx/tests, inifile: pytest.ini
collected 9 items                                                                                               

test_File2.py ssss..
test_File1.py ...

====================================== 5 passed, 4 skipped in 0.87 seconds =====================================

Upvotes: 1

Related Questions