Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unittest local variable in Python

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.

like image 421
Igor Avatar asked May 28 '14 10:05

Igor


People also ask

What does unittest main () do?

Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.

How does unittest work in Python?

Unit testing is a method for testing software that looks at the smallest testable pieces of code, called units, which are tested for correct operation. By doing unit testing, we can verify that each part of the code, including helper functions that may not be exposed to the user, works correctly and as intended.

Does Python have JUnit?

The Python unit testing framework, dubbed 'PyUnit' by convention, is a Python language version of JUnit, by smart cookies Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent's Smalltalk testing framework. Each is the de facto standard unit testing framework for its respective language.


1 Answers

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.

like image 73
Jayanth Koushik Avatar answered Oct 09 '22 19:10

Jayanth Koushik