在测试类中找不到__init__变量?

时间:2017-02-09 06:47:09

标签: python-2.7 class testing nose2

我最近从使用nose改为nose2,但是我的测试代码很多都在这个过程中出现了问题。有一件事特别是我在测试类中放入的 init 变量" self.mir_axis"给出了这个错误:

mirror_index = mirror_matrix.index(self.mir_axis)
AttributeError: 'TestConvert' object has no attribute 'mir_axis'

这曾经用于鼻子,但是使用nose2我的 init 变量由于某种原因不再注册。我在这里错过了什么吗?我使用python 2.7.3,eclipse作为IDE btw。

from nose2.compat import unittest
from nose2.tools import params
from nose2 import session
from nose2.events import ReportTestEvent
from nose2.plugins import testid
from nose2.tests._common import (FakeStartTestEvent, FakeLoadFromNameEvent,
                                 FakeLoadFromNamesEvent, TestCase)#
# Import maya modules
import maya.cmds as mc
# Absolute imports of other modules
from neo_autorig.scripts.basic import name
from neo_autorig.scripts.basic import utils

# Test class for converting strings
class TestConvert(TestCase):
    counter = 0 # counter to cycle through mir_axes

    def _init__(self):
        mir_axes = ['xy', '-xy', 'yz', '-yz'] # different axes to be applied
        self.mir_axis = mir_axes[self.__class__.counter]
        self.__class__.counter += 1 # increase counter when run
        if self.__class__.counter > 3:
            self.__class__.counter = 0 # if counter reaches max, reset
        self.utils = utils.Utils(self.mir_axis, False) # pass module variables

    def setUp(self): # set up maya scene
        side_indicator_l = mc.spaceLocator(n='side_indicator_left')[0]
        side_indicator_r = mc.spaceLocator(n='side_indicator_right')[0]
        mirror_matrix = ['xy', '-xy', 'yz', '-yz']
        trans_matrix = ['tz', 'tz', 'tx', 'tx']
        side_matrix = [1, -1, 1, -1]
        mirror_index = mirror_matrix.index(self.mir_axis)
        mc.setAttr(side_indicator_l+'.'+trans_matrix[mirror_index], side_matrix[mirror_index])
        mc.setAttr(side_indicator_r+'.'+trans_matrix[mirror_index], side_matrix[mirror_index]*-1)

    def tearDown(self): # delete everything after
        mc.delete('side_indicator_left', 'side_indicator_right')

     def test_prefix_name_side_type(self): # test string
            nc = name.Name('prefix_name_side_type')
            existing = nc.get_scenenames('transform')
            self.assertEqual(nc.convert('test', 'empty', self.utils.find_side('side_indicator_left'),
                'object', existing), 'test_empty_l_object')
            self.assertEqual(nc.convert('test', 'empty', self.utils.find_side('side_indicator_right'),
                'object', existing), 'test_empty_r_object')

# run if script is run from inside module
if __name__ == '__main__':
    import nose2
    nose2.main()

1 个答案:

答案 0 :(得分:1)

我发布了您发布的代码段的两个问题:

第一个是def _init__(self):缺少下划线;它应该是def __init__(self):

第二个(似乎是错误的原因)是_init__mir_axes = ['xy', '-xy', ...中的第一行应该是self.mir_axes = ...

修改

根据Ned Batchelder成名的Coverage.py,您应该使用setUp代替__init__。 :)