Different behavior with mock to same class or different class
Ninguém assumiu esta issue ainda.
- 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:
import unittest
from unittest.mock import patch
class Number:
def __init__(self, value):
print(f'init: {value}')
self.value = value
def get_value(self):
return self.value
class MockNumber:
def __init__(self, value):
print(f'mock init: {value}')
self.value = value
def get_value(self):
return self.value
class TestNumber(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_number(self):
number = Number(1)
with patch.object(Number, '__new__', return_value=number):
test = Number(2)
print(test.get_value())
mock_number = MockNumber(1)
with patch.object(Number, '__new__', return_value=mock_number):
test = Number(2)
print(test.get_value())
if __name__ == '__main__':
unittest.main()
The output is
init: 1
init: 2
2
mock init: 1
1
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
When mock the function new of class Number to return an instance of MockNumber, the behavior is simple: just return the instance of MockNumber.
But when mock the function to return an instance of Number, the function of init will still be called.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece com o unittest fornecido e compare patch.object(Number, 'new') quando ele retorna um Number versus um MockNumber. Rastreie o comportamento de construção de objetos do Python em torno de new e init, então determine se a diferença é intencional; o trabalho estará concluído quando houver um comportamento confirmado e uma cobertura de regressão ou documentação apropriada.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- compilers
- Tipo de issue
- Bug
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 35/100