如何阻止假设pytest插件

How to block the hypothesis pytest plugin

根据 pytest 文档,我可以使用 -p no:name 阻止插件。我确认这适用于其他插件。但是,当我用 hypothesis 尝试这样做时,它没有任何效果:

(ska3-next) ➜  Chandra.Maneuver git:(master) pytest -v -p no:hypothesis Chandra/Maneuver
========================================================== test session starts ==========================================================
platform darwin -- Python 3.8.12, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/aldcroft/miniconda3/envs/ska3-next/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/Users/aldcroft/git/Chandra.Maneuver/.hypothesis/examples')
rootdir: /Users/aldcroft/git/Chandra.Maneuver, configfile: pytest.ini
plugins: remotedata-0.3.3, doctestplus-0.11.2, arraydiff-0.3, anyio-2.2.0, hypothesis-6.29.3, mock-3.6.1, filter-subpackage-0.1.1, openfiles-0.5.0, astropy-header-0.1.2, cov-3.0.0
collected 4 items                                                                                                                       

...                                                                                                                      

对于上下文,我之所以要阻止hypothesis是另一个问题。由于超出此问题范围的原因,我需要 运行 pytest 在我没有写权限的目录中。默认情况下 hypothesis 想要创建一个数据库,如果失败,它会回退到内存数据库,但在此过程中会发出警告。该警告对于我们的集成测试来说是不可取的,我找不到抑制该警告的方法。

(ska3-next) ➜  site-packages pytest -v -p no:hypothesis Chandra/Maneuver
========================================================== test session starts ==========================================================
platform darwin -- Python 3.8.12, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/aldcroft/miniconda3/envs/ska3-next/bin/python
/Users/aldcroft/miniconda3/envs/ska3-next/lib/python3.8/site-packages/hypothesis/database.py:60: HypothesisWarning: The database setting is not configured, and the default location is unusable - falling back to an in-memory database for this session.  path='/Users/aldcroft/miniconda3/envs/ska3-next/lib/python3.8/site-packages/.hypothesis/examples'
  warnings.warn(
cachedir: .pytest_cache
hypothesis profile 'default' -> database=InMemoryExampleDatabase({})
rootdir: /Users/aldcroft/miniconda3/envs/ska3-next/lib/python3.8/site-packages
plugins: remotedata-0.3.3, doctestplus-0.11.2, arraydiff-0.3, anyio-2.2.0, hypothesis-6.29.3, mock-3.6.1, filter-subpackage-0.1.1, openfiles-0.5.0, astropy-header-0.1.2, cov-3.0.0
collected 4 items                                                                                                                       
...

顺便说一句,none 我们的测试使用 hypothesis 但它最终作为其他一些第 3 方包的依赖项安装在环境中。

官方支持的选项是use settings profiles全局启用settings(database=None)

或者,您可以 -p no:hypothesispytest,但请注意,这不是一个记录和支持的方法 - 例如,它可能会更改为更明显的 -p no:hypothesis

我尝试在稍后调用 pytest.main(...):

的测试运行器脚本中像这样使用 settings
    import hypothesis
    hypothesis.settings.register_profile('no_database', database=None)
    hypothesis.settings.load_profile('no_database')

这最终困扰了 pytest:

=============================== warnings summary ===============================
_pytest/config/__init__.py:1114
  /Users/aldcroft/miniconda3/envs/ska3-next/lib/python3.8/site-packages/_pytest/config/__init__.py:1114: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: hypothesis
    self._mark_plugins_for_rewrite(hook)