unittest: TestLoader.discover() returns tests in package/__init__.py
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
According to the docs:
TestLoader.discover(start_dir, pattern='test*.py', top_level_dir=None)
Find all the test modules by recursing into subdirectories from the specified start directory, and return a TestSuite object containing them. Only test files that match pattern will be loaded.
However, tests are also loaded from __init__.py. This does not match the pattern test*.py.
To reproduce, create package/__init__.py with the following contents:
import unittest
class Test(unittest.TestCase):
def test(self):
self.fail('')
Then run python3 -m unittest. The expected output is Ran 0 tests. But it actually is:
F
======================================================================
FAIL: test (package.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "package/__init__.py", line 5, in test
self.fail('')
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
So the test was discovered, despite being located in a file that does not match the pattern test*.py.
The same happens when the TestCase subclass is imported in any __init__.py file.
I tested the above with Python 3.9.2. It does not happen for me with Python 2.7.18.
Linked PRs
- gh-100487
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
使用包含所示 TestCase 的 package/init.py 重現此問題,並執行 python3 -m unittest;從 TestLoader.discover() 進入點開始。完成的定義是:不探索出位於不符合 test*.py 模式的檔案中的測試,包括 package/init.py;查看已連結的 PR gh-100487,以了解已在進行的工作。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- testing-qa
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100