Search code examples
pythonunit-testingreplit

Why is the value checked by python's unittest different from the value when printed in driver code?


I made a bunch of unit tests for my students assignments so they can check if their code is working correctly and I ran into an issue I don't know how approach.

The unit test code is this:

hero = Hero("Test Hero")

for item in items:
    hero.pickUp(item)
    hero.equip(item)
    hero.unequip(item)
    self.assertTrue(item.name in hero.backpack.__str__())
    self.assertFalse(item.name in hero.__str__())

self.assertEqual(hero.damage, 0)
self.assertEqual(hero.block, 0)
self.assertEqual(hero.armor, 0)

The last 3 tests are the ones the student isn't passing, stating that the values are -10,-10 and -30 respectively, the negative of the value expected had the unequip method not been called. So I try to figure out what is going on by replacing their driver code with the test and printing out the values of the variables I'm checking but when printed to the console I get 0,0,0; the values I'm expecting. How can the value printed to the console be different from the value when checked by the unit test? I don't get this issue when running the unit tests against my own solution to the assignment.

Note: This is all being done on Replit so that might be part of the issue.

Tried to find some sort of reason why this is happening but my searches have come up empty handed, so here I am.


Solution

  • Found the issue, student was inconsistent with how they were referencing the dictionary holding what items were currently equipped. Not sure how it was resulting in a difference between the printed value and the unit test but after correcting the issue was resolved.