Budget App: test_module.py issues

Hi, I’m facing some issues with the test_modules.py file and I believe the tests are written wrong. I know that is a large statement to be made by someone doing a project finished by many, but hear me out.
I finished all of the Category class functions in the budget.py. I tested them with the sample python code available in the main.py. Then I tested the functions by uncommenting the unit test code in main.py and almost all of my tests failed.
So then I picked each of the test-case and tried running it through main.py class (of course removing the self references of the UnitTests class). And guess what, almost all of them passed.
Coming to my earlier statement, as far as I know, the objects created in the setUp function are shared by all tests. Therefore, the ledger list is supposed to be common too. Hence, if two tests with different expected outputs refer to the same element of this sharedledger list, they will get the same output. Many functions share this behavior such as (test_deposit and test_deposit_no_description), (test_withdraw and test_withdraw_no_description).
I understand that I could be wrong and there might be a piece of code I wrote in the budget.py that would have produced wrong test outputs, and therefore, I didn’t change anything in the test_module.py file. I hope someone will help me identify where the problem is. I’m sharing my repl.it project link with this post.
Budget App

Update: I believe the problem is not the test cases but the ledger list is being shared between classes when it shouldn’t be. The only thing I found yet to solve it is to declare the variables in the init function as follows:
self.var_name = value
which even though gets the job done, but I don’t think this is the way it should always be. It would be great if someone could maybe share a python class specific best practices guide or article.

Hi @MukeshAngrish ,
It seems the only problem with your code is the spacing. This happened to me as well.
Although my results were visually ok, I was misplacing blanks here and there.

What I did to notice it, is redirect the output from my script to a file (1) and compare it side by side (2) with the expected output. Then, I realized what I was doing wrong.
(1) $ python main.py > my_output.txt
(2) I used Notepad++ to do the comparison

Hope you can fix it.
By the way, there is a syntax error on line 80 (just a trailing dot)
Take care!

Hi @jdelarubia, thanks for the fast reply. I found the problem was the class variable as I mentioned in the comment above. And now I just wish to know what I’m doing is correct way to write code or not. But thanks for your reply anyways. Stay safe!
BTW which file has the syntax error? You mentioned the line number but not the file.

It looks like you updated your code so it’s all good now; it was on the section where you were creating the output though.

Yeah I was constantly analyzing and updating my code to solve the problem.

1 Like