- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我认为 py.test
在某种意义上是“独立的”,它“按原样”处理 test_*.py
文件,并且只导入这些文件中指定的模块文件,不考虑任何周围的文件。看来我错了。这是我与 py.test
的对话:
$ ls
__init__.py test_pytest.py
$ cat __init__.py
$ cat test_pytest.py
def test_pytest():
assert True
$ py.test test_pytest.py
========================================================= test session starts ==========================================================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 0 items / 1 errors
================================================================ ERRORS ================================================================
___________________________________________________ ERROR collecting test_pytest.py ____________________________________________________
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.5-py2.7.egg/py/_path/local.py:529: in pyimport
> mod = __import__(modname, None, None, ['__doc__'])
E ImportError: No module named test_pytest
======================================================= 1 error in 0.01 seconds ========================================================
$ rm __init__.py
$ py.test test_pytest.py
========================================================= test session starts ==========================================================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
test_pytest.py .
======================================================= 1 passed in 0.01 seconds =======================================================
$
如何使 py.test
正常工作并保留我的 __init__.py
文件?
更新
在评论中,Holger Krekel 问道,父目录的名称是什么。事实证明,我可以重现上面的错误,只需要一个特定的父目录名称(例如,与安装的软件包之一相同的名称,如 distutils
)。看这里:
~/test_min
$ tree
.
└── distutils
├── __init__.py
└── test_pytest.py
1 directory, 2 files
~/test_min
$ cat distutils/__init__.py
~/test_min
$ cat distutils/test_pytest.py
def test_pytest():
assert True
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 0 items / 1 errors
=============================== ERRORS ===============================
_____________ ERROR collecting distutils/test_pytest.py ______________
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.5-py2.7.egg/py/_path/local.py:529: in pyimport
> mod = __import__(modname, None, None, ['__doc__'])
E ImportError: No module named test_pytest
====================== 1 error in 0.01 seconds =======================
~/test_min
$ rm distutils/__init__.py
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
distutils/test_pytest.py .
====================== 1 passed in 0.01 seconds ======================
~/test_min
$ touch __init__.py
~/test_min
$ ls
__init__.py distutils
~/test_min
$ touch distutils/__init__.py
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
distutils/test_pytest.py .
====================== 1 passed in 0.02 seconds ======================
~/test_min
$ rm __init__.py
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 0 items / 1 errors
=============================== ERRORS ===============================
_____________ ERROR collecting distutils/test_pytest.py ______________
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.5-py2.7.egg/py/_path/local.py:529: in pyimport
> mod = __import__(modname, None, None, ['__doc__'])
E ImportError: No module named test_pytest
====================== 1 error in 0.01 seconds =======================
~/test_min
$ mv distutils foobar
~/test_min
$ py.test foobar/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
foobar/test_pytest.py .
====================== 1 passed in 0.01 seconds ======================
~/test_min
$
希望这些附加信息能有所帮助。
最佳答案
看起来 py.test 正在使用 py._path.pyimport
打开您的文件。如果目录中有 __init__.py
文件,它会将您的文件视为一个模块,否则它会打开该文件。长话短说,删除 __init__.py
或将测试放在项目代码之外的另一个目录中(<--- 好主意)。
https://py.readthedocs.io/en/latest/path.html#py._path.local.LocalPath.pyimport
关于python - `py.test` 和 `__init__.py` 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8244277/
为什么正是是 A.__init__() B.__init__() D.__init__() 由以下代码打印?特别是: 为什么是C.__init__() 未打印? 为什么是C.__init__()如果我
目前我有这样的事情: @dataclass(frozen=True) class MyClass: a: str b: str c: str d: Dict[str, str] ...
我正在尝试从父类继承属性: class Human: def __init__(self,name,date_of_birth,gender,nationality): self.name =
如何扩展基类的 __init__,添加更多要解析的参数,而不需要 super().__init__(foo, bar) 在每个派生类中? class Ipsum: """ A base ips
这是我试图解决的一个非常简单的例子: class Test(object): some_dict = {Test: True} 问题是我无法在 Test 仍在定义时引用它 通常,我会这样做:
我在 Objective-C 中使用过这个结构: - (void)init { if (self = [super init]) { // init class }
我有一个类层次结构,其中 class Base 中的 __init__ 执行一些预初始化,然后调用方法 calculate。 calculate 方法在 class Base 中定义,但预计会在派生类
这是我在多种语言中都怀念的一个特性,想知道是否有人知道如何在 Python 中完成它。 我的想法是我有一个基类: class Base(object): def __init__(self):
我正在对 threading.Thread 类进行子类化,它目前看起来像这样: class MyThread(threading.Thread): def __init__(self:
我正在用 cython 写一些代码,我有一些 "Packages “within” modules" . — 这实际上是对我在那里的问题的跟进,结构应该是一样的。问题是这是 cython,所以我处理的
class AppendiveDict(c.OrderedDict): def __init__(self,func,*args): c.OrderedDict.__init_
看完this回答,我明白 __init__ 之外的变量由类的所有实例和 __init__ 内的变量共享每个实例都是唯一的。 我想使用所有实例共享的变量,随机给我的类实例一个唯一的参数。这是我尝试过的较
在下面的代码中: import tkinter as tk class CardShuffling(tk.Tk): background_colour = '#D3D3D3'
我正在覆盖类的 __new__() 方法以返回具有特定 __init__() 集的类实例。 Python 似乎调用类提供的 __init__() 方法而不是特定于实例的方法,尽管 Python 文档在
从内置类型和其他类派生时,内置类型的构造函数似乎没有调用父类(super class)构造函数。这会导致 __init__ 方法不会被 MRO 中内置函数之后的类型调用。 例子: class A:
答: super( BasicElement, self ).__init__() 乙: super( BasicElement, self ).__init__( self ) A 和 B 有什么区
class A(object): def __init__(self): print('A.__init__()') class D(A): def __init__(
到目前为止我已经成功地做了什么: 我创建了一个 elem 类来表示 html 元素(div、html、span、body 等)。 我可以像这样派生这个类来为每个元素创建子类: class elem:
我一直在努力理解 super() 在多重继承的上下文中的行为。我很困惑为什么在 test2.py 的父类中调用 super() 会导致为父类调用 __init__()? test1.py #!/usr
为什么我在 Python 代码中看不到以下内容? class A: def __init__(self, ...): # something important class B
我是一名优秀的程序员,十分优秀!