我正在寻找一种在py.test
中使用类似“global fixtures”的方法。似乎scope="session"
最接近我需要的,但它的工作方式似乎与scope="module"
级别的选项类似。fixture总共启动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()
我用它就像
^{2}$我想要的是,在运行所有测试之前,该服务的一个副本将被启动,而在所有测试运行之后,它将被删除。在
通过在fixture中指定
scope="session"
,您将拥有一个会话范围的实例。 您可以使用setup-show
cli标志检查fixture的设置和拆卸,如3.0Changelog所示同样,正如@hoefling在评论中指出的,一旦设置
autouse=True
用usefixtures
标记测试就不再需要了。在相关问题 更多 >
编程相关推荐