python / python/cpython

Mocked value at method level not overriding class-level mock in unittest

Aberta
#122,726 2 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

Bug report

Bug description:
Description

I encountered an issue where mocking a value at the method level does not override the class-level mock in unit tests. Even though the method-level mock should take precedence, the value from the class-level mock is still being used.

Steps to Reproduce

Create a test class with a class-level mock using @mock.patch.
Add a method-level mock to one of the test methods to override the class-level mock.
Run the tests and observe that the class-level mock value is still used instead of the method-level mock.

Expected Behavior

The method-level mock should override the class-level mock, and the value set in the method-level mock should be used in the test.

Actual Behavior

The class-level mock value is used, and the method-level mock is ignored.

Sample code
import unittest
from unittest import mock

# Function to be mocked
def get_value():
    return "original"

# Class-level mock
@mock.patch('module.get_value', return_value='class-level')
class TestMock(unittest.TestCase):

    def test_class_level_mock(self, mock_get_value):
        # This should return 'class-level'
        result = get_value()
        self.assertEqual(result, 'class-level')

    @mock.patch('module.get_value', return_value='method-level')
    def test_method_level_mock(self, mock_get_value):
        # This should return 'method-level', but it returns 'class-level'
        result = get_value()
        self.assertEqual(result, 'method-level')

if __name__ == '__main__':
    unittest.main()

CPython versions tested on:

3.10

Operating systems tested on:

No response

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

Comece reproduzindo o exemplo com unittest.mock.patch aplicado tanto no nível da classe quanto no nível do método e, em seguida, inspecione os decoradores de unittest.mock.patch e sua interação com os métodos de unittest.TestCase. Confirme se o valor de retorno no nível do método é ignorado e adicione ou atualize um teste de regressão focado que mostre a sobrescrita esperada no nível do método.

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
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

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