代码之家  ›  专栏  ›  技术社区  ›  Timo

py.试验全球范围的固定装置

  •  0
  • Timo  · 技术社区  · 8 年前

    我正在寻找一种方法来使用“全球固定装置”之类的东西 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
    

    我想得到的是,在运行所有测试之前,该服务的一个副本将被剥离,并在所有测试运行之后被剥离。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Enrique Saez    8 年前

    通过指定 scope="session" 您可以使用 setup-show Changelog

    正如@hoefling在评论中指出的,一旦设定 autouse=True 在测试中标记 usefixtures 不再需要了。

    推荐文章