python / python/cpython

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

Aberta
#100,290 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Reproduza o problema com um package/init.py contendo o TestCase mostrado e execute python3 -m unittest; comece pelo ponto de entrada TestLoader.discover(). Está concluído quando os testes em arquivos que não correspondem ao padrão test*.py, incluindo package/init.py, não forem descobertos; verifique o PR vinculado gh-100487 para conferir o trabalho já em andamento.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
testing-qa
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Claramente especificada
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.