python / python/cpython

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

Aperta
#100,290 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
36k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il problema con un package/init.py contenente il TestCase mostrato ed esegui python3 -m unittest; inizia dal punto di ingresso TestLoader.discover(). Il lavoro è completato quando i test nei file che non corrispondono al pattern test*.py, incluso package/init.py, non vengono individuati; controlla la PR collegata gh-100487 per il lavoro già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
testing-qa
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.