15

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.

Igor
  • 2,674
  • 1
  • 25
  • 42
  • 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 May 28 '14 at 10:57
  • *need to write a test that will check a local variable value inside a static function* why? what value does that add? – Tim May 28 '14 at 11:15
  • Thank you. I realized that it was a quite bad idea at all. – Igor May 30 '14 at 11:16
  • 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 https://docs.python.org/3/library/unittest.mock.html – Kfcaio Oct 08 '21 at 13:20

1 Answers1

25

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.

Jayanth Koushik
  • 9,006
  • 1
  • 38
  • 49