Cómo hacer una prueba unitaria de la variable local en Python
Frecuentes
Visto 12,167 equipos
16
I need to write a test that will check a local variable value inside a static function. I went through all the unittest
docs but found nothing yet.
1 Respuestas
29
You can't. Local variables are local to the function and they cannot be accessed from the outside.
But the bigger point is that you shouldn't actually be trying to test the value of a local variable. Functions should be treated as black boxes from the outside. The are given some parameters and they return some values and/or change the external state. Those are the only things you should check for.
contestado el 28 de mayo de 14 a las 11:05
Hey But code coverage shows local variables assignments as missing what for that ? - codificaciónbruh
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python unit-testing or haz tu propia pregunta.
Show us the static function you are going to test. Generally, local variable is local, so it is not supposed to be tested from outside. Anyway, you may add asserts and other testing stuff inside the function, but this is not something one could call unittest. - Jan Vlcinsky
need to write a test that will check a local variable value inside a static function why? what value does that add? - Tim
Thank you. I realized that it was a quite bad idea at all. - Igor
Just for reference: if your function calls another one and that variable is one of its parameters, you can mock that second function and assert it's called with certain values. Check docs.python.org/3/library/unittest.mock.html - Kfcaio