0

我最近从使用nose 更改为nose2,但是我的很多测试代码似乎在此过程中已损坏。特别是我在测试类“self.mir_axis”中放入的初始化变量给出了这个错误:

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

这曾经适用于nose,但是对于nose2,我的init变量由于某种原因不再注册。我在这里错过了什么吗?我使用 python 2.7.3,并将 eclipse 作为 IDE 顺便说一句。

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

1 回答 1

1

我发现您发布的代码段存在两个问题:

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

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

编辑

根据Coverage.py名声的Ned Batchelder的说法,你应该使用setUp而不是__init__不管。:)

于 2017-02-09T06:57:46.347 回答