unittest: TestLoader.discover() returns tests in package/__init__.py
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện vấn đề với một package/init.py chứa TestCase được nêu và chạy python3 -m unittest; bắt đầu tại điểm vào TestLoader.discover(). Hoàn tất có nghĩa là các test trong những tệp không khớp với mẫu test*.py, bao gồm package/init.py, không được phát hiện; kiểm tra PR được liên kết gh-100487 để xem công việc đang được tiến hành.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- testing-qa
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100