为什么Python的unittest检查的值与驱动代码中打印的值不同?

huangapple go评论63阅读模式
英文:

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

问题

以下是您要翻译的内容:

我为我的学生的作业制作了一堆单元测试,以便他们可以检查他们的代码是否正常工作,但我遇到了一个问题,我不知道该如何解决。

单元测试代码如下:

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)

最后的3个测试是学生没有通过的,他们声称这些值分别为-10,-10和-30,即未调用unequip方法时所期望的值的负数。因此,我尝试通过用测试替换他们的驱动代码并打印出我正在检查的变量的值来弄清楚发生了什么,但当打印到控制台时,我得到了0,0,0;这些是我预期的值。为什么控制台中打印的值与单元测试检查时的值不同?当我运行单元测试以测试自己的作业解决方案时,我没有遇到这个问题。

注意:所有这些都是在Replit上完成的,所以这可能是问题的一部分。

尝试找出为什么会发生这种情况,但我的搜索一直没有结果,所以我在这里。

英文:

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.

答案1

得分: 0

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.

英文:

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.

huangapple
  • 本文由 发表于 2023年4月13日 22:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定