Eric IDE运行单元测试报错:suite(unittest.loader._FailedTest)排查
I’ve run into similar IDE-specific unittest quirks before, so let’s break down why this is happening and how to fix it:
The Root Cause
Eric IDE’s test runner has an explicit expectation: it looks for a suite() function in your test runner script that returns a unittest.TestSuite object. Your current runner.py skips this step and directly executes tests via testRunner.run(tests).
While the tests do run (you see output in the console), the IDE’s test loading logic can’t find the expected suite object. It interprets this as a failed suite load, hence the _FailedTest error—even though the tests themselves execute successfully.
Solution: Modify runner.py to Work With Eric IDE
Update your tests/runner.py to expose a suite() function, while keeping it fully functional for command-line execution:
import os import unittest def suite(): # Use absolute path to avoid working directory mismatches in the IDE test_dir = os.path.dirname(os.path.abspath(__file__)) loader = unittest.TestLoader() # Discover all test files in the tests directory tests = loader.discover(test_dir) return tests if __name__ == '__main__': # Preserve command-line test running functionality testRunner = unittest.runner.TextTestRunner() testRunner.run(suite())
Why This Works
suite()Function: Eric IDE will call this function to retrieve the test suite directly, eliminating the load error caused by missing expected structure.- Absolute Path for Discovery: Using
os.path.abspath(__file__)ensures the test loader always finds your test files, regardless of what working directory the IDE uses (this avoids silent path issues that might break test discovery).
Steps to Apply
- Replace your existing
runner.pycode with the version above. - In Eric IDE, re-select
runner.pyas your test suite file. - Run the tests again—you should no longer see the
_FailedTesterror, and the IDE will properly recognize and display test results.
内容的提问来源于stack exchange,提问作者Alexey Orlov

