Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

doctest: fix autouse fixtures possibly not getting picked up #11941

Merged
merged 2 commits into from
Feb 7, 2024

Commits on Feb 6, 2024

  1. doctest: don't open code the module import

    Currently, `DoctestModule` does `import_path` on its own. This changes
    it to use `importtestmodule` as `Module` does. The behavioral changes
    are:
    
    - Much better error messages on import errors.
    
    - Handles a few more error cases (see `importtestmodule`). This
      technically expands the cover of `--doctest-ignore-import-errors` but
      I think it makes sense.
    
    - Considers `pytest_plugins` in the module.
    
    - Populates `self.obj` as properly (without double-imports) as is
      expected from a `PyCollector`.
    
    This is also needed for the next commit.
    bluetech committed Feb 6, 2024
    Configuration menu
    Copy the full SHA
    6e5008f View commit details
    Browse the repository at this point in the history

Commits on Feb 7, 2024

  1. doctest: fix autouse fixtures possibly not getting picked up

    Fix pytest-dev#11929.
    
    Figured out what's going on. We have the following collection tree:
    
    ```
    <Dir pyspacewar>
      <Dir src>
        <Package pyspacewar>
          <Package tests>
            <DoctestModule test_main.py>
              <DoctestItem pyspacewar.tests.test_main.doctest_main>
    ```
    
    And the `test_main.py` contains an autouse fixture (`fake_game_ui`) that
    `doctest_main` needs in order to run properly. The fixture doesn't run!
    It doesn't run because nothing collects the fixtures from (calls
    `parsefactories()` on) the `test_main.py` `DoctestModule`.
    
    How come it only started happening with commit
    ab63ebb? Turns out it mostly only
    worked accidentally. Each `DoctestModule` is also collected as a normal
    `Module`, with the `Module` collected after the `DoctestModule`. For
    example, if we add a non-doctest test to `test_main.py`, the collection
    tree looks like this:
    
    ```
    <Dir pyspacewar>
      <Dir src>
        <Package pyspacewar>
          <Package tests>
            <DoctestModule test_main.py>
              <DoctestItem pyspacewar.tests.test_main.doctest_main>
            <Module test_main.py>
              <Function test_it>
    ```
    
    Now, `Module` *does* collect fixtures. When autouse fixtures are
    collected, they are added to the `_nodeid_autousenames` dict.
    
    Before ab63ebb, `DoctestItem` consults
    `_nodeid_autousenames` at *setup* time. At this point, the `Module` has
    collected and so it ended up picking the autouse fixture (this relies on
    another "accident", that the `DoctestModule` and `Module` have the same
    node ID).
    
    After ab63ebb, `DoctestItem` consults
    `_nodeid_autousenames` at *collection* time (= when it's created). At
    this point, the `Module` hasn't collected yet, so the autouse fixture is
    not picked out.
    
    The fix is simple -- have `DoctestModule.collect()` call
    `parsefactories`. From some testing I've done it shouldn't have negative
    consequences (I hope).
    bluetech committed Feb 7, 2024
    Configuration menu
    Copy the full SHA
    9cd14b4 View commit details
    Browse the repository at this point in the history