Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Stop mocking Anki modules globally #145

Closed
aplaice opened this issue Nov 14, 2021 · 0 comments · Fixed by #175
Closed

Stop mocking Anki modules globally #145

aplaice opened this issue Nov 14, 2021 · 0 comments · Fixed by #175
Labels

Comments

@aplaice
Copy link
Collaborator

aplaice commented Nov 14, 2021

(This is mostly to remind myself for the future.)

Currently, the presence of mock_anki_modules() in test/config/config_settings_spec.py causes the anki imports to be mocked, for all tests (including those that don't call mock_anki_modules(), when the tests are run together.

To reproduce

Steps

  1. Install all dependencies (including dev):
pipenv install --dev
  1. Run all mamba tests together:
pipenv run mamba ./
  1. Run the tests in test/export/note_sorter_spec.py by themselves:
pipenv run mamba ./test/export/note_sorter_spec.py

Expected result

Both for 2 and 3 all tests pass.

Actual result

For 2 all tests pass:

31 examples ran (3 pending) in 1.2636 seconds

For 3 the tests crash:

Traceback (most recent call last):
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/bin/mamba", line 8, in <module>
    sys.exit(main())
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/mamba/cli.py", line 18, in main
    runner.run()
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/mamba/runners.py", line 29, in run
    modules = self.example_collector.modules()
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/mamba/example_collector.py", line 20, in modules
    with self._load_module_from(path) as module:
  File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/mamba/example_collector.py", line 52, in _load_module_from
    yield self._module_from_ast(name, path)
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/mamba/example_collector.py", line 70, in _module_from_ast
    exec(code, module.__dict__)
  File "./test/export/note_sorter_spec.py", line 5, in <module>
    from aqt import mw
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/aqt/__init__.py", line 22, in <module>
    from aqt.qt import *
  File "/home/x/.local/share/virtualenvs/CrowdAnki-4rfLFX4y/lib/python3.7/site-packages/aqt/qt.py", line 16, in <module>
    from PyQt5.QtWebEngineWidgets import *  # type: ignore
ModuleNotFoundError: No module named 'PyQt5.QtWebEngineWidgets'

(The fact that the tests crash is another issue, but they should either always crash or always succeed, so the fact that their completion depends on the way they're called is problematic.)

Alternative approach (to make sure)

To reproduce

  1. In test/config/config_settings_spec.py (the file containing the mock_anki_modules() call), comment out everything after mock_anki_modules(). (We're not interested in the behaviour of config_settings_spec.py in the absence of module mocking.)

  2. Run all mamba tests:

pipenv run mamba ./
  1. Comment out mock_anki_modules() in test/config/config_settings_spec.py.

  2. Run all mamba tests again.

Expected result

The same result is returned in 2 and 4.

Actual result

For 2 the tests all succeed:

25 examples ran (3 pending) in 1.2638 seconds

For 4 the tests crashed again.


Presumably, given that config comes before export alphabetically, the patching of sys.module persists throughout the testing.

(I think that I haven't missed anything obvious, for why the behaviour is different.)

The main short-term conclusion (mainly for me) is that the behaviour of the tests is different, depending on whether they're called individually, or all together.

aplaice added a commit to aplaice/CrowdAnki that referenced this issue Nov 6, 2022
Ideally, we'd stop mocking them _at all_ but this is a good first
step, as it's annoying and surprising (with the current/previous
_global_ mocking, the test is/was run differently depending on whether
it was run individually or as part of a batch).

Fix Stvad#145.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant