动画实例未清理

时间:2018-09-07 01:00:43

标签: kivy

环境

  • Python 3.6.3
  • 基辅大师
  • 操作系统:Linux Mint 18.2(基于Ubuntu 16.04 LTS)

代码

您好,我正在写kivy.animation的单元测试。当我运行下面的代码

import unittest
from time import time, sleep
from kivy.animation import Animation
from kivy.uix.widget import Widget
from kivy.clock import Clock


class AnimationTestCase(unittest.TestCase):

    SLEEP_DURATION = .3
    TIMES = 2

    def sleep(self, t):
        start = time()
        while time() < start + t:
            sleep(.01)
            Clock.tick()

    def test_animation(self):
        for index in range(self.TIMES):
            print('----------------------------------')
            with self.subTest(index=index):
                w = Widget()
                a = Animation(x=100, d=.2)
                print('a:', a)
                a.start(w)
                self.sleep(self.SLEEP_DURATION)
                print('instances_:', Animation._instances)
                self.assertEqual(len(Animation._instances), 0)

输出是

----------------------------------
a: <kivy.animation.Animation object at 0x7f0afb31c660>
instances_: set()
----------------------------------
a: <kivy.animation.Animation object at 0x7f0afc20b180>
instances_: {<kivy.animation.Animation object at 0x7f0afc20b250>, <kivy.animation.Animation object at 0x7f0afb31c660>}

 ======================================================================
 FAIL: test_animation (kivy.tests.test_animations.AnimationTestCase) (index=1)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/tmp/firefox/kivy/kivy/tests/test_animations.py", line 34, in test_animation
     self.assertEqual(len(Animation._instances), 0)
 AssertionError: 2 != 0

 ----------------------------------------------------------------------
 Ran 1 test in 0.822s

 FAILED (failures=1)

任何一个

  • 增加SLEEP_DURATION(例如SLEEP_DURATION = 2)或
  • TIMES = 1

将解决此错误。

这是正确的行为还是错误?

1 个答案:

答案 0 :(得分:0)

此错误的原因是kivy.modules.inspector

config.ini删除此行后,

[modules]
inspector =     # <= remove this line

程序的行为符合我的期望。似乎ScrollView中的inspector在内部创建了Animation,这使我的测试失败了。