python - pytest 无法从 File1 导入类,但不能从同一文件导入类 A

标签 python unit-testing testing pytest

我有一个很奇怪的问题,我可能知道原因,但不知道解决方案。

首先我有以下文件结构。

../../
├── 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 的实现上。在让 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 解释器都可以正常运行,我决定为这个类编写一些测试,不幸的是测试无法导入 ClassB ......他们将导入 ClassA 就好了......所以它不是路径问题..(通常你应该先写测试但是......抱歉!!)

如果有人遇到过这个问题,或者对此有所了解,我将不胜感激!谢谢!

最佳答案

我找到了答案。看来我运行测试代码很好,但针对的是旧版本的模块。在我以前的代码版本中,ClassB 不存在,而 ClassA 存在。更新我的代码后,我应该运行以下命令来安装它。

sudo pip install ./--upgrade

安装更新的模块后,运行 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 =====================================

关于python - pytest 无法从 File1 导入类,但不能从同一文件导入类 A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48805457/

相关文章:

android - 类型推断失败: Not enough information to infer parameter T in fun <T : Context!> getApplicationContext(): T! Please specify it explicitly

asp.net-mvc - "App_GlobalResources"未在单元测试用例中加载

java - 自动化性能测试框架

java - 尝试组合 hamcrest 匹配器时出现编译错误

python - 使用 nltk 查找二元组时出错

python - 使用 setuptools 更改默认 dist 文件夹

c# - 如何将 HttpRequest 接收到 MVC 3 Controller 操作中?

visual-studio-2010 - Autoit 不生成从列表中选择项目后调用的事件

python - 如何找到字典和列表之间的匹配项以从匹配项创建新列表?

python - AppEngine 推送部署和模块