如何在Python单元测试中从测试__main__执行该方法?

时间:2014-09-27 22:49:56

标签: python testing automated-tests unit-testing

我试图在测试其他测试用例之前先设置测试的前提条件。但正如您在我的代码中看到的那样,precondition print命令不会被触发!你能解释一下原因和解决方法如何在该方法中使用SetPreConditionFirewall()来解决代码问题。

这是我的代码:

#! /usr/bin/env python
__author__ = 'Milson Munakami'
__revision__ = '0.0.2'

import json
import urllib
#import time
#from util import *
import httplib

#import sys
#from scapy.all import *

import unittest

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

import logging
import traceback

class testFirewallS1( unittest.TestCase ):
    def setUp(self):
        self.controllerIp="127.0.0.1"
    def tearDown(self):
        if self.failed:
            return
        duration = time.time() - self.startTime_
        self.cleanup(True)
        if self.reportStatus_:
            self.log.info("=== Test %s completed normally (%d sec)", self.name_, duration)

    def cleanup(self, success):
        sys.excepthook = sys.__excepthook__
        try:
            return
        except NameError:
            self.log.error("Exception hit during cleanup, bypassing:\n%s\n\n" % traceback.format_exc())
            pass
        else:

                fail("Expected a NameError")    

    def SetPreConditionFirewall(self):
        command = "http://%s:8080/wm/firewall/module/enable/json" % self.controllerIp
        urllib.urlopen(command).read()
        print self.controllerIp
        print "Test Pre-condition setting here"

    #Precondition Test
    def testPreConditionFirewall(self):
        print "Test pass"   

def suite():

        suite = unittest.TestSuite()

        suite.addTest(unittest.makeSuite(testFirewallS1))

        return suite


if __name__ == '__main__':

        suiteFew = unittest.TestSuite()

        testFirewallS1("SetPreConditionFirewall")

        suiteFew.addTest(testFirewallS1("testPreConditionFirewall"))


        #Testing the Test Cases Begins Here
        unittest.TextTestRunner(verbosity=2).run(suiteFew)

0 个答案:

没有答案