简体   繁体   English

如何在unittest Python中创建继承的类的实例,以便我可以在simpleXMLRPCServer中注册该类的实例

[英]How to create an instance of an inherited class in unittest Python so that I can register the instance of that class in simpleXMLRPCServer

from __future__ import with_statement
from sikuli.Sikuli import *

import os
import unittest
import this
import xmlrpclib
import SimpleXMLRPCServer, SocketServer, threading

class SimpleThreadedXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer):
    pass

class ABC(object):
    def A(self):
      ...........
    def B(self):
      ...........
    def C(self):
      ...........     


class XYZ(unittest.TestCase,ABC):
    def setUp(self):
         print "Inside setup"
         pass      
    def tearDown(self):
         print "Inside tearDown" 
         pass
    def test_1(self):
        self.A()
        self.B()
        self.C()


    def D(self):    
        print "Inside D"
        return True    


suite = unittest.TestLoader().loadTestsFromTestCase(XYZ)

class ServerThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.server = SimpleThreadedXMLRPCServer(("x.x.x.1", 8000))
        #self.server.register_instance() #How to Register an instance of XYZ here.
    def run(self):
        self.server.serve_forever()

server = ServerThread()
server.start()

So my question is that how to Register an instance of XYZ here. 所以我的问题是如何在这里注册XYZ实例。 In the commented line above so that it can be called from the XMLRPC client like: 在上面的注释行中,因此可以从XMLRPC客户端调用它,例如:

client = xmlrpclib.ServerProxy("http://x.x.x.2:8000")
handraise = client.D() #Or any other possible way

I know it works with register_function(D) but I want the whole class XYZ to be exposed using register_instance() . 我知道它可以与register_function(D)一起工作,但我希望使用register_instance()公开整个XYZ类。

Theoretically this should work: 从理论上讲,这应该起作用:

self.server.register_instance(XYZ())    # but it throwed some runTest Error for me

Anyways, passing the method name explicitly as an arg in the Classname inside register_instance like: 无论如何,在register_instance内的Classname中显式地将方法名称作为arg传递:

self.server.register_instance(XYZ("D"))

worked as expected for me. 符合我的预期。
And from the xmlrpc client, call the D() like: 然后从xmlrpc客户端调用D(),如下所示:

handraise = client.D()

暂无
暂无

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

相关问题 如何创建unittest测试类的实例? - How to create an instance of unittest test class? When using Generics types, how can I call an Attribute of an inherited class on the Base class instance with Python / Django? - When using Generics types, how can I call an Attribute of an inherited class on the Base class instance with Python / Django? 在 Python 中,如何覆盖继承的 class 的 class 属性,该属性本身就是 model 的一个实例? - In Python how can I overwrite the class attribute of an inherited class, where that attribute is itself an instance of a model? 如何创建多个继承class的实例? - How to create instance of multiple inherited class? 如何定义一个方法,以便返回当前类的实例而不是Python中继承的类的实例? - How to define a method so that return the instance of the current class not of the class where it was inherited in Python? 如何从列表Python创建类实例 - How can I create a class instance from a list Python Python,etree:如何将实例复制到继承类的新实例中? - Python, etree: how to copy instance into new instance of a inherited class? 我是否需要创建要用unittest测试的类的实例? - Do I need to create an instance of class to be tested with unittest? 如何创建python类的实例 - how to create an instance of class python 如何将可调用对象传递给 class 以便它可以访问 Python 中的实例属性? - How to pass a callable to a class so that it can access instance attributes in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM