我正在寻找一种在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


我想拥有的是,该服务的一个副本将在运行所有测试之前被自动旋转,而在一切运行之后被拆除。

最佳答案

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

就像@hoefling在注释中指出的那样,一旦设置了autouse=True,就不再需要用usefixtures标记测试。

关于python - 具有全局范围的py.test固定装置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52254079/

10-12 18:21