Budget App - Unit tests order makes difference

In Budget App project I’m having the error in the pic (red). Also there you can see the test implementation (green) and the string of the object “food” printed by the “print(food)” that I’ve put inside the test in line (yellow):

The test tries to see the deposit made in the test as it was the first deposit. However, since unit test order is random there could be other deposits before the one in the test. This leads to the error I’m getting which to me doesn’t make sense…

Any thoughts on this?

Before each test is run, the code in setUp method is executed, assigning fresh Category instances to use them in test.

If two instances, which should be separate, are somehow sharing ledger contents, that’s an implementation error.

If you’d need more specific help with solving this issue, please share your code.


Thank you! With you saying that I realized that I had to initialize the class variables in the constructor. I had assumed that it was enough to initialize when creating them. Thanks a lot!

1 Like

That’s actually how class variables works. When variable is defined below the class line, it is shared between all objects of that class. For example:

class X:
    class_variable = 'x'

Then it can be used with X.class_variable syntax.

Variables defined in the __init__ method (or actually in any method), with the self.name syntax, are instance variables.

There’s however one more detail, that can make it much more confusing. When self.x is used, variable is searched in multiple places. First in the instance variables, if not found python will look in class variables, if still there’s no variable with such name found, parent classes are checked.