Skip to content

Test discovery should be fault tolerant and display all successfully imported files #13301

Description

Currently, pytest discovery fail for the entire project if a single import is missing on a single file:

E   ModuleNotFoundError: No module named 'xyz'
ERROR my_test.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
Exception: pytest discovery failed (exit code 2)

Only 1/600 file failed, but VS Code does not show the 599 tests which were correctly discovered.

Many other project have "optional" deps which add extra features. Those extra deps shouldn't be required when the full project do not need to be tested.
My project has many independent modules, each requiring a specific set of dependencies (pip install .[module0]). Contributors should not be forced to install the full dependencies of all modules / features when they only want to test a subpart.

Related issues:

Those issues are not duplicate but seems related:

Activity

  1. karthiknadig commented on Aug 6, 2020

    @karthiknadig
    Member

    Were you able to get pytest or unittest to show tests via command line, even with import errors?

  2. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Aug 6, 2020
  3. Conchylicultor commented on Aug 6, 2020

    @Conchylicultor
    Author

    Yes, pytest --collect-only correctly detect the valid tests:

    ============================= test session starts ==============================
    platform linux -- Python 3.8.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
    rootdir: /home/epot/__A__/Code/datasets, configfile: pytest.ini
    plugins: xdist-1.34.0, forked-1.3.0
    collected 1685 items / 1 error / 1684 selected
    
    <Package tensorflow_datasets>
      <Module import_test.py>
        <UnitTestCase ImportTest>
          <TestCaseFunction test_import>
          <TestCaseFunction test_session>
      <Module import_testing_test.py>
        <UnitTestCase ImportTestingTest>
    
    ...  # The 1684 detected tests are correctly displayed
    
    =========================== short test summary info ============================
    ERROR tensorflow_datasets/scripts/documentation/build_api_docs_test.py
    !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
    ======================== 5 warnings, 1 error in 35.20s =========================
    

    Additionally, pytest --continue-on-collection-errors correctly execute the valid tests.

  4. Conchylicultor commented on Aug 10, 2020

    @Conchylicultor
    Author

    Shouldn't the info needed label be removed ?

  5. karthiknadig commented on Aug 10, 2020

    @karthiknadig
    Member

    --continue-on-collection-errors makes test discovery bit better when there are issues with loading test files. However, adding support that will require a bit of work in the current test adapter. We have plans to update the adapter to be more robust. We also have to see if similar support can be provided on other supported test frameworks.

  6. added
    needs spikeLabel for issues that need investigation before they can be worked on.
    and removed on Aug 10, 2020
  7. 3 remaining items

  8. fnobis commented on Jan 3, 2023

    @fnobis

    I am facing the same error with the plugin. For manual test discovery in a terminal I can fix the error by executing:

    • pip install -e LOCAL_PACKAGE_PATH
    • source ~/ROS2_WORKSPACE

    Can I prepend these command somehow to the plugin, so that I could use the test discovery?

  9. eleanorjboyd commented on Jan 6, 2023

    @eleanorjboyd
    Member

    Hello! This should be fixed in the rewrite which is currently underway! The meta-issue is here and that will track progress but I will keep this issue open as well until resolved. Thanks!

  10. added
    needs PRReady to be worked on
    and removed
    needs spikeLabel for issues that need investigation before they can be worked on.
    on Jul 7, 2023
  11. eleanorjboyd commented on Jul 21, 2023

    @eleanorjboyd
    Member

    Hello! A fix for this is out on insiders! If someone could tests and let me know it worked that would be great!

    Verification steps:

    1. take following code and make two files with each block
    2. run pytest or unittest discovery
    3. check that there is a node with the tests that can be discovered and one saying there was a failure
    def test_example():
        assert 1 == 1
    
    def test_two():
        assert True
    
    import unittest
    import foo #doesn't exist so will error
    
    class OtherTest(unittest.TestCase):
        def testtwo(self):
            self.assertEquals(add(4, 5), 9)
    
    if __name__ == "__main__":
        unittest.main()
    
  12. locked as resolved and limited conversation to collaborators on Aug 25, 2023
  13. added 2 commits that reference this issue on Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions