Different behavior with mock to same class or different class
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l'unittest fornito e confronta patch.object(Number, 'new') quando restituisce un Number rispetto a un MockNumber. Traccia il comportamento di costruzione degli oggetti di Python relativo a new e init, quindi determina se la differenza è intenzionale; il lavoro è completato quando il comportamento è confermato e dispone di un'adeguata copertura di regressione o documentazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100