简体   繁体   中英

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. In the commented line above so that it can be called from the XMLRPC client like:

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() .

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:

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

worked as expected for me.
And from the xmlrpc client, call the D() like:

handraise = client.D()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM