简体   繁体   English

为什么在使用 unittest 测试 class 方法时收到以下 AttributeError:'class' object has no attribute 'testing method'?

[英]Why do I receive the following AttributeError when testing a class method with unittest: 'class' object has no attribute 'testing method'?

For the sake of practicing to code, I am trying to program a very simple game.为了练习编码,我正在尝试编写一个非常简单的游戏。 When trying to run a test on a (private) class method, I receive the following error: AttributeError: 'LivingBeing' object has no attribute '_Test_Class_Methods__lose_hp'尝试对(私有)class 方法运行测试时,我收到以下错误: AttributeError: 'LivingBeing' object has no attribute '_Test_Class_Methods__lose_hp'

More elaborate:更详细:

I have a class named "LivingBeing" in the file classes.py.我在文件 classes.py 中有一个名为“LivingBeing”的 class。

class LivingBeing:

def __init__(self, name, hp=100):
    self.name = name
    self.hp = hp

def __lose_hp(self, number=20, decimal_percentage=1):
    self.hp = self.hp - (21 - number)*decimal_percentage
    return self.hp

Now I would like to test said __lose_hp method, and I tried this with the following code:现在我想测试所说的 __lose_hp 方法,我用下面的代码试了一下:

import sys, unittest
sys.path.append("/Users/angel/OneDrive/Dokumente/Studium/CL/Programmieren/git_projects/fantasticgame/")

from classes import LivingBeing

class Test_Class_Methods(unittest.TestCase):

    def setUp(self):
        self.livingbeing = LivingBeing("Life")

    def test_lose_hp(self):
        self.assertEqual(self.livingbeing.__lose_hp(20), 99, "Should be 99")

if __name__ == "__main__":
    unittest.main()

What I tried so far:到目前为止我尝试了什么:

  • I found some information that it was most likely an ImportError, so I tried the "sys.path.append"-statement according to how a similar problem was solved.我查了一些资料说很可能是ImportError,所以我根据类似问题的解决方法尝试了“sys.path.append”-statement。 So, if I were to leave it out, I would receive the same error message.因此,如果我将其遗漏,我将收到相同的错误消息。 The file "classes.py" and the testing file are in this same folder.文件“classes.py”和测试文件位于同一个文件夹中。

  • At first, I did not have the setUp method, however it had solved a somewhat similar problem for someone else ( namely here ), so I tried it - same error message for me however.起初,我没有 setUp 方法,但是它已经为其他人( 即这里)解决了一个有点类似的问题,所以我尝试了它 - 但是对我来说是同样的错误消息。

  • I also didn't have the return statement in the class method itself at first, about which I very much understand why it would throw an Error by now (because the return value would be None).起初我也没有在 class 方法本身中有 return 语句,对此我非常理解为什么它现在会抛出错误(因为返回值将是 None)。

  • The very classic try to just restart (the computer) and retry before posting also did not help.非常经典的尝试只是重新启动(计算机)并在发布前重试也没有帮助。

  • There was a point at which I tried to make "Test_Class_Methods"有一点我试图制作“Test_Class_Methods”
    inherit from both, the unittest.Testcase and LivingBeing, but this继承自 unittest.Testcase 和 LivingBeing,但这
    caused the same error again.再次导致同样的错误。

Looked like this:看起来像这样:

class Test_Class_Methods(unittest.TestCase, LivingBeing):

    def setUp(self):
        self.livingbeing = LivingBeing("Life")

    def test_lose_hp(self):
        self.assertEqual(self.livingbeing.__lose_hp(20), 99, "Should be 99")

This being my first question, I hope it's as complete and concise as it should be, and I am thankful for any pointers to help with said error.这是我的第一个问题,我希望它尽可能完整和简洁,我感谢任何有助于解决上述错误的指示。

EDIT: I changed the method to being public instead of private, and with that change, it now works.编辑:我将方法更改为公开而不是私有,并且通过这种更改,它现在可以工作了。 If I happen to find a good guide on how to write tests for private methods, I might link it here for future answer-seekers.如果我碰巧找到了一个关于如何为私有方法编写测试的好指南,我可能会在此处链接它以供将来寻求答案的人使用。

I know it's a bit too late but I was facing the same AttributeError as yours.我知道有点太晚了,但我面临着和你一样的 AttributeError。 Removing double underscore from the beggining of the function I wanted to test helped me.从我想测试的 function 的开头删除双下划线对我有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError 'class name' object 没有属性 'method' - AttributeError 'class name' object has no attribute 'method' 属性错误:类型对象“类”没有属性“方法” - AttributeError: type object 'class' has no attribute 'method' 发生异常:AttributeError&#39;Class&#39;对象没有属性&#39;Method&#39; - Exception has occurred: AttributeError 'Class' object has no attribute 'Method' 测试调用实例变量的类方法-AttributeError - Testing class method that calls an instance variable - AttributeError 我不能在 Python 中调用 class 方法; AttributeError: 'check_class' object 没有属性 'check_1' - I cant call a class method in Python; AttributeError: 'check_class' object has no attribute 'check_1' AttributeError:“求解器”对象在实现类求解器 ODE 中没有属性“方法” - AttributeError: 'Solver' object has no attribute 'method' at implementig class solver ODE "<i>Getting an AttributeError:<\/i>获取属性错误:<\/b><class> <i>has no attribute<\/i>没有属性<\/b><method>" - Getting an AttributeError: <class> has no attribute <method> &#39;class&#39;对象没有属性&#39;method&#39; - 'class' object has no attribute 'method' AttributeError: 'function' object 在测试 Flask 请求时没有属性 'get' - AttributeError: 'function' object has no attribute 'get' when testing Flask requests 获取错误:AttributeError:类 <CLASS NAME> 没有属性“ <METHOD NAME> &#39; - Getting error: AttributeError: class <CLASS NAME> has no attribute '<METHOD NAME>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM