Python - 属性错误 : 'OnDemand' object has no attribute 'calc'

标签 python class error-handling

确实发生了一些事情,我几乎一天都无法解决

示例如下:尝试从一个类到另一个类的简单方法调用来找出问题,因为我今天早上也遇到了臭名昭著的问题......所以尝试了一个简单的方法调用检查......
二类:
主页单独
一经请求

HomePageAlone - 在 OnDemand 中定义了一个测试“def test_E_Access(self):”调用方法我得到了以下错误。

代码如下:

主页单独

from sikuli import *
from OnDemand import *
import OnDemand

class homePage(unittest.TestCase):

 def setUp(self):
  print("Test")   


 def test_E_Access(self):
  callMethod = OnDemand()
  callMethod.calc() # Line#15


suite = unittest.TestSuite()
suite.addTest(homePage('test_E_Access'))
unittest.TextTestRunner(verbosity=2).run(suite)

一经请求
from sikuli import *

class OnDemand(object):

    def setUp(self):
        print("setup")


    def calc(self):
        print ("This is calling")

日志消息

==================================================== =====================

错误:test_E_Access ( .homePage)

回溯(最近一次通话最后):
文件“C:\DOCUME~1\Senthil.S\LOCALS~1\Temp\sikuli-6018543662740221054.py”,第 15 行,在 test_E_Access
callMethod.calc(self) # Line#15
AttributeError: 'OnDemand' 对象没有属性 'calc'

在 0.016 秒内运行 1 次测试

失败(错误=1)

另一个尝试:我尝试按照您的建议使用以下代码段 - 它总是抛出 AttributeError: 'OnDemandPopular' object has no attribute 'calc'

导入 OnDemandPopular
ondemand = OnDemandPopular.OnDemandPopular()
ondemand.calc()

请帮忙

最佳答案

您应该导入 按需 来自 的类(class)按需 模块;

from OnDemand import OnDemand

首页单独
import unittest
from OnDemand import OnDemand

class homePage(unittest.TestCase):

 def setUp(self):
  print("Test")   

 def test_E_Access(self):
  callMethod = OnDemand()
  callMethod.calc() # Line#15


suite = unittest.TestSuite()
suite.addTest(homePage('test_E_Access'))
unittest.TextTestRunner(verbosity=2).run(suite)

按需
class OnDemand(object):

    def setUp(self):
        print("setup")

    def calc(self):
        print ("This is calling")

输出是;
This is calling
test_E_Access (HomePageAlone.homePage) ... ok

Test
This is calling
----------------------------------------------------------------------

Ran 1 test in 0.000s

OK
Test
This is calling

关于Python - 属性错误 : 'OnDemand' object has no attribute 'calc' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24406130/

相关文章:

Python:tf-idf-cosine:查找文档相似度

python - 如何使用python对一个变量应用过滤器考虑其他两个变量

c++ - 在 C++ 中,为什么仅包含 union 及其基类实例的派生类占用的内存大于 union 的大小?

c++ - 无法使用 C++ catch(...) 捕获访问冲突异常

php - 如何在.htaccess文件中添加错误页面

python - 纹理更改后更新 Kivy 中的纹理矩形

python - PIL 图像未按正确顺序渲染输入的像素值

Java泛型类型删除方法签名问题

python - 以相同的名称调用不同的函数,具有相同的 Action

error-handling - NestJS - 如何构建代码以优雅地处理错误?