Skip to content
Advertisement

Python Unittest and object initialization

My Python version is 3.5.1

I have a simple code (tests.py):

JavaScript

If I run it with command ‘python tests.py’ I will get the results:

JavaScript

Why it is happening? And how to fix it. I expect that each tests run will be independent (each test should pass), but it is not as we can see.

Advertisement

Answer

The array is shared by all instances of the class. If you want the array to be unique to an instance you need to put it in the class initializer:

JavaScript

For more information take a look at this question: class variables is shared across all instances in python?

Advertisement