运行测试用例时出错

时间:2011-02-11 21:17:22

标签: python

我需要在下面测试我的代码。我正在使用一个测试来查看它是否正常工作。但是不知道我应该在测试代码中作为参数传递什么。请在最后看到测试代码,请指导我应该在这里传递什么价值?因为无论我在这里写什么,它都会给我一个错误。

from brisa.core.reactors import install_default_reactor
reactor = install_default_reactor()


import os
import unittest

from brisa.upnp.device import Device, Service
from brisa.upnp.device.service import StateVariable


class SwitchPower(Service):

    def __init__(self):
        Service.__init__(self,
                         'SwitchPower',
                         'urn:schemas-upnp-org:service:SwitchPower:1',
                         '',
                         os.getcwd() + '/SwitchPower-scpd.xml')
        self.target = False
        self.status = False


    def SetTarget(self, *args, **kwargs):
        self.target = kwargs['NewTargetValue']
        self.status = self.target
        self.set_state_variable('Status', self.target)
        print 'Light switched ', {'1': 'on', '0': 'off'}.get(self.target, None)
        return {}

    def GetTarget(self, *args, **kwargs):
        return {'RetTargetValue': self.target}

    def soap_GetStatus(self, *args, **kwargs):
        return {'ResultStatus': self.status}


class BinaryLight(object):

    def __init__(self):
        self.server_name = 'Binary Light device'
        self.device = None
    def _create_device(self):
        project_page = 'https://garage.maemo.org/projects/brisa'
        self.device = Device('urn:schemas=upnp-org:device:BinaryLight:1',
                            self.server_name,
                            manufacturer = 'Ankit',
                            model_name = 'Binary Light Device',
                            model_description = 'Test Device',
                            model_number = '1.0',
                            model_url=project_page)
    def _add_service(self):
        switch = SwitchPower()
        self.device.add_service(switch)
    def start(self):
        self._create_device()
        self._add_services()
        self.device.start()
        reactor.add_after_stop_func(self.device.stop)
        reactor.main()


# Here's our "unit tests".

class IsOddTests(unittest.TestCase):



    def testOne(self):

        self.failUnless(_create_device('urn:schemas=upnp-org:device:BinaryLight:1'))


if __name__ == '__main__':
         unittest.main()      



    if __name__ == '__main__':
        device = BinaryLight()
        device.start()   


Ran 1 test in 0.000s

错误是: -

ERROR: testOne (__main__.IsOddTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "binary_light.py", line 67, in testOne
    self.failUnless(_create_device('urn:schemas=upnp-org:device:BinaryLight:1'))
NameError: global name '_create_device' is not defined

----------------------------------------------------------------------

2 个答案:

答案 0 :(得分:1)

testOne()方法中,您正在调用_create_device(),这不是一个全局函数,它是SwitchPower对象的一种方法。

您可能想要创建一个新的SwitchPower对象。

答案 1 :(得分:1)

_create_device()中未定义

IsOddTests。它在BinaryLight中定义。

我对此并不完全确定,但改变如何:

  

self.failUnless(_create_device( '瓮:模式= UPnP的组织:设备:BinaryLight:1'))

为:

bl = BinaryLight()
self.failUnless(bl._create_device())

我删除了:

  

'瓮:模式= UPnP的组织:设备:BinaryLight:1'

因为它在_create_device()的定义中。