2014-10-03 108 views
0

我有帶testFirewallS8類的S8Test.py類和一些方法。我想訪問在這個類中聲明的方法形式的主要方法。並從該方法設置變量另一個python文件包含相同的變量進行修改。我怎樣才能做到這一點:如何從main訪問python類的方法並在另一個python文件中傳遞和訪問變量

#! /usr/bin/env python  
    __author__ = 'Milson Munakami'  
    __revision__ = '0.0.2'  
    import json  
    import urllib  
    import httplib  
    from scapy.all import * 

    import unittest 
    import os, sys, socket, struct, select, time 
    from threading import Thread  

    import logging  
    import traceback 

    from mininet.net import Mininet  
    from mininet.node import OVSSwitch, OVSKernelSwitch, Controller, RemoteController  
    from mininet.log import setLogLevel, info  
    from mininet.cli import CLI 

    class TestFirewallS8(unittest.TestCase): 
     def setUp(self): 
      self.controllerIp="127.0.0.1"  
      self.switch = "00:00:00:00:00:00:00:01"  
      self.destinationIp = "10.0.0.1"  
      self.startTime_ = time.time()  
      self.failed = False  
      self.reportStatus_ = True 
      self.name_ = "Firewall"  
      self.log = logging.getLogger("unittest") 

      "Create an empty network and add nodes to it."  
      self.net = Mininet(controller=RemoteController)  
      #Want to move this method call from outside the setUp method because it need to be initiated only once for the whole test but 
      #it need to access the class variables and pass it to another python file i.e. Events.py to perform some task on the object i.e. self  
      #self.CreateNet()  

     def createNet(self):  
      print "Me" 

      info('*** Adding controller\n')  
      self.net.addController('c0' , controller=RemoteController,ip= "127.0.0.1", port=6633)  
      info('*** Adding hosts\n')  
      h1 = self.net.addHost('h1', ip='10.0.0.1')  
      h2 = self.net.addHost('h2', ip='10.0.0.2')  
      h3 = self.net.addHost('h3', ip='10.0.0.3') 
      info('*** Adding switch\n')  
      s1 = self.net.addSwitch('s1')  
      info('*** Creating links\n')  
      self.net.addLink(h1, s1)  
      self.net.addLink(h2, s1)  
      self.net.addLink(h3, s1)  

     def setFinalcondition(self):  
      Precondition.SetFinalcondition(self) 
      info('*** Stopping network')  
      self.net.stop() 

     def testCreateFlow(self):  
      Events.createFlow(self) 

def suite(): 
     suite = unittest.TestSuite() 
     suite.addTest(unittest.makeSuite(TestFirewallS8)) 
     return suite 

    if __name__ == '__main__':  
     #How to get run the method of testFirewallS8 class and set the variable of it like self.net 
     suiteFew = unittest.TestSuite(testCreateFlow) 
     TestFirewallS8("createNet") 

在另一個Events.py我:

def createFlow(self): 
    info('*** Starting network\n') 
    self.net.start() 

    info('*** Testing network connecivity\n') 
    #self.net.pingAll() 
    h1 = self.net.get('h1') 
    h3 = self.net.get('h3') 
    h1.cmd('ping -c1 %s' % h3.IP()) 
+1

請按照使用'UpperCase' Python的約定和名稱類的,否則他們是容易的樣子功能。 – 2014-10-03 22:39:53

+0

根據你的描述,不清楚它的意圖是什麼或問題是什麼。您的示例代碼將受益於[如何創建一個最小化,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – wwii 2014-10-03 23:12:37

回答

0

我不是完全清楚你問什麼,但如果你有一個類定義:

class MyClass(object): 
    def __init__(self, name): 
     self.name = name 

    def big_name(self): 
     print(self.name.upper()) 

將實例化它(MyClass('Bob'))後,將其分配給一個變量。

bob = MyClass('Bob') 

然後,您可以通過訪問它的屬性來執行該類實例的方法。

bob.big_name # accesses the "big_name" attribute on "bob": a bound method 
bob.big_name() # executes the bound method 

屬性也可以是變量,你可以自由地訪問和重新分配,以及:

bob.name # accesses the "name" attribute on "bob": a string 
bob.name = 'Robert' # reassigns the "name" attribute 
+0

我認爲用unittest做這件事存在一些問題! 我想將setUp()方法內的createNet()分離到類的外部,並且每個測試用例只運行一個! – Milson 2014-10-03 22:53:01

+0

@Milson它有點不清楚你在做什麼,但是'setUp()'只在每個測試套件中調用一次,所以你可以使用它來掛起實例上的對象並在每個測試用例中訪問它們。 – 2014-10-03 22:57:10

相關問題