具有全局范围的py.test灯具

问题描述 投票:0回答:1

我正在寻找一种在py.test中使用类似“全球装置”的方法。似乎scope="session"最接近我的需要,但它似乎与scope="module"级别选项类似。夹具总共启动了n次,其中n是模块的数量。

基本上,我有这种缓慢的初始化和资源匮乏的服务,进行形态分析

@pytest.fixture(scope='session', autouse=True)
def morfanalyzer():
    from myapp.nlp.morfservice import MorfAnalyzerService
    morfservice = MorfAnalyzerService()

    def f():
        morfservice.run(debug=True)

    thread = Thread(target=f)
    thread.start()

    yield morfservice

    morfservice.stop()
    thread.join()

而且我喜欢它

@pytest.mark.usefixtures(morfanalyzer.__name__)
def test_this_stage(morfanalyzer):
    assert False

我想要的是,在运行所有测试之前,将完全删除该服务的一个副本,并在所有测试运行后进行拆除。

python testing pytest fixtures
1个回答
1
投票

通过在fixture中指定scope="session",您将拥有一个会话范围的实例。您可以使用setup-show cli标志检查灯具的设置和拆卸,如3.0 Changelog所示。

另外正如@hoefling在评论中指出的那样,一旦用autouse=True设置usefixtures标记测试就不再需要了。

© www.soinside.com 2019 - 2024. All rights reserved.