pytest 无法从 File1 导入类,但不能从同一文件导入类 A
pytest can import ClassA from File1 but not ClassB from the same file
我有一个比较特殊的问题,我可能知道原因,但不知道解决方案。
首先我有以下文件结构。
../../
├── 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
test_File1.py 的内容类似于以下内容(尽管已消毒)。
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):
将上述测试加载到pytest中时,出现以下错误。
=========================================================== 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 ===================================================
话虽如此,我感觉问题出在 ClassB 的实现上。在让 class B 不费吹灰之力地工作之前,我遇到了一些问题。为了解释我的意思,下面是 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):
...
由于未定义 CLSC,我最初甚至无法让 ClassB 工作(最初我在同一个文件中有 ClassC)。我能够通过在导入 ClassC 代码后创建 CLSC 对象来解决这个问题。
既然代码 运行 可以从命令行和 python 解释器中正确使用,我决定为此编写一些测试 class 不幸的是测试无法导入 ClassB ...他们会导入 ClassA 就好了...所以这不是路径问题...(通常您应该先编写测试但是...抱歉!!)
如果有人 运行 了解这个问题,或者对此有所了解,我将不胜感激!谢谢!
我找到了答案。看来我 运行ning 测试代码很好,但针对的是旧版本的模块。在我以前的代码版本中,ClassB 不存在,而 ClassA 存在。更新我的代码后,我应该 运行 以下内容来安装它。
sudo pip install ./ --upgrade
安装更新后的模块 运行ning pytest 产生了正确的结果(因为我使用了正确的代码库)。
[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 =====================================
我有一个比较特殊的问题,我可能知道原因,但不知道解决方案。
首先我有以下文件结构。
../../
├── 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
test_File1.py 的内容类似于以下内容(尽管已消毒)。
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):
将上述测试加载到pytest中时,出现以下错误。
=========================================================== 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 ===================================================
话虽如此,我感觉问题出在 ClassB 的实现上。在让 class B 不费吹灰之力地工作之前,我遇到了一些问题。为了解释我的意思,下面是 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):
...
由于未定义 CLSC,我最初甚至无法让 ClassB 工作(最初我在同一个文件中有 ClassC)。我能够通过在导入 ClassC 代码后创建 CLSC 对象来解决这个问题。
既然代码 运行 可以从命令行和 python 解释器中正确使用,我决定为此编写一些测试 class 不幸的是测试无法导入 ClassB ...他们会导入 ClassA 就好了...所以这不是路径问题...(通常您应该先编写测试但是...抱歉!!)
如果有人 运行 了解这个问题,或者对此有所了解,我将不胜感激!谢谢!
我找到了答案。看来我 运行ning 测试代码很好,但针对的是旧版本的模块。在我以前的代码版本中,ClassB 不存在,而 ClassA 存在。更新我的代码后,我应该 运行 以下内容来安装它。
sudo pip install ./ --upgrade
安装更新后的模块 运行ning pytest 产生了正确的结果(因为我使用了正确的代码库)。
[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 =====================================