0

According to the pytest documentation, I can block a plugin using -p no:name. I confirmed that this works for other plugins. However, when I try this with hypothesis it has no effect:

(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                                                                                                                       

...                                                                                                                      

For context, the reason I want to block hypothesis is another problem. For reasons outside the scope of this question, I need to run pytest in a directory where I do not have write access. By default hypothesis wants to create an database, and failing that it falls back to a memory database but in the process emits a warning. That warning is not desirable for our integration tests and I could find no way to suppress the warning.

(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                                                                                                                       
...

As an aside, none of our tests use hypothesis but it ended up getting installed in the environment as a dependency of some other 3rd party package.

Tom Aldcroft
  • 2,339
  • 1
  • 14
  • 16

2 Answers2

1

The officially supported option is to use settings profiles to globally enable settings(database=None).

Alternatively, you could -p no:hypothesispytest, but note that this is not a documented and supported approach - it may be changed to the more obvious -p no:hypothesis, for example.

Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19
  • I tried this and it did not work for me. Please see the answer below for details. – Tom Aldcroft Mar 13 '22 at 21:25
  • The second fix of `-p no:hypothesispytest` did work and I think that is what we will use. Or maybe `-p no:hypothesispytest -p no:hypothesis` just to be safe. Thanks for the help! You might consider updating the documentation to mention this little trick since I had guessed `-p no:hypothesis` and was disappointed that it did not seem to work. – Tom Aldcroft Mar 14 '22 at 11:42
0

I tried using settings like this in a test runner script that later calls pytest.main(...):

    import hypothesis
    hypothesis.settings.register_profile('no_database', database=None)
    hypothesis.settings.load_profile('no_database')

This ended up bothering 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)
Tom Aldcroft
  • 2,339
  • 1
  • 14
  • 16
  • I'd expect that to work from `conftest.py`; with your setup I guess there's a tricky interaction about the order of imports and configuration. – Zac Hatfield-Dodds Mar 14 '22 at 04:57
  • Yes, that works from `conftest.py`, but unfortunately we have testing in about 30 separate packages developed in-house which are being impacted by this issue. The test runner is how we do integration testing on the full suite of packages (on latest `main` branch) to look for problems. So while it is possible to do this profile update on `conftest.py`, it is not very easy. I had been hoping for a simple global config file which would be automatically read in. – Tom Aldcroft Mar 14 '22 at 11:35