python / python/cpython

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

Abierto
#100,290 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce el problema con un package/init.py que contenga el TestCase mostrado y ejecuta python3 -m unittest; comienza en el punto de entrada TestLoader.discover(). Se considera terminado cuando no se descubran los tests en archivos que no coincidan con el patrón test*.py, incluido package/init.py; comprueba el PR enlazado gh-100487 para ver el trabajo que ya está en curso.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
testing-qa
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.