假设我在localhost
上运行mongodb。我正在尝试使用py.test
数据库在项目上运行tmpdb
套件,该数据库在拆解期间将被删除。我的conftest.py
是:
import pytest
import mongoengine as me
from pymongo import MongoClient
@pytest.fixture(autouse=True)
def tmpdb():
cc: MongoClient = me.connect('tmpdb')
yield cc
cc.drop_database('tmpdb')
cc.close()
和一个简单的测试:
import pytest
import random
import typing
import mongoengine as me
import shapely.geometry as geo
class Points(me.Document):
p = me.PointField()
mp = me.MultiPointField()
Pair = typing.Tuple[float, float]
IterPair = typing.Iterable[Pair]
def generator(i: int) -> IterPair:
for _ in range(i):
x = random.uniform(-179, 180)
y = random.uniform(-89, 90)
yield x, y
@pytest.fixture
def point() -> geo.Point:
return geo.Point(list(generator(1)))
@pytest.fixture
def multipoint() -> geo.MultiPoint:
return geo.MultiPoint(list(generator(2)))
def test_init_and_save(point: geo.Point, multipoint: geo.MultiPoint):
# me.connect('tmpdb')
# Points.drop_collection()
new = Points()
new.p = geo.mapping(point)
new.mp = geo.mapping(multipoint)
new.save()
def test_query_within_box():
box = [[-180, -90], [180, 90]]
qry = Points.objects(p__geo_within_box=box)
obj = qry.first()
assert obj is not None
由于某些原因,test_query_within_box
失败;也就是说,查询无法返回结果(就像在两次测试之间删除了数据库一样)。这应该不会发生,因为tmpdb
夹具不应该被拆除直到所有测试完成使用tmpdb
夹具退出范围的至少一个测试功能?
如果我注释掉conftest.py
并取消注释test_init_and_save
中的行,则所有测试都通过。
这是怎么回事?