python / python/cpython

unittest: TestLoader.discover() returns tests in package/__init__.py

オープン
#100,290 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

示されている TestCase を含む package/init.py で問題を再現し、python3 -m unittest を実行してください。調査は TestLoader.discover() のエントリーポイントから始めてください。完了条件は、package/init.py を含め、test*.py パターンに一致しないファイル内のテストが検出されないことです。すでに進行中の作業については、関連する PR gh-100487 を確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
testing-qa
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。