jeudi 15 février 2018

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!

Aucun commentaire:

Enregistrer un commentaire