首页 文章

pytest 2.3中的封装范围固定装置

提问于
浏览
7

在pytest的最新版本中,很容易创建像这样的函数,类,模块或会话范围的灯具:

@pytest.fixture(scope="module") 
def db():
     return DB()

这创建了一个仅为每个使用它的python模块调用一次的fixture .

但是每个python包需要调用一次的灯具呢? (使用鼻子,可以使用包的 __init__.py 中的setUp / tearDown方法完成)

1 回答

  • 5

    对于包或目录级别的fixture,您可以使用 scope='session' 在您需要的目录中的 conftest.py 文件中声明一个fixture . 一旦包/目录中的第一个测试使用它,就会实例化fixture . 这是an example但是,如果fixture函数注册了终结器,您可能会看到它在该目录中的最后一次测试之后没有直接执行 . 我认为pytest可以支持更急切的拆解或者在需要时引入"directory"范围 . 通常它's not a big problem if the teardown executes a little later as long as it doesn' t执行得太早了:)注意显然杰森intends to drop package-level setup/teardown support for nose

    无论如何,如果您需要更热切/精确的pytest拆解,请随时open an issue .

相关问题