相信大家都知道执行OpenStack的Unit Test,非常简单,只需要执行"run_tests.sh"就可以,但是有没有想过怎么 去调试这些Unit Test脚本呢,有个方法是使用"nosetests",具体如下:
1)修改你要调试的文件,加上"pdb.set_trace()",我现在用test_scheduler.py举例
class SchedulerDriverModuleTestCase(test.TestCase):
"""Test case for scheduler driver module methods."""
def setUp(self):
super(SchedulerDriverModuleTestCase, self).setUp()
self.context = context.RequestContext('fake_user', 'fake_project')
def test_encode_instance(self):
instance = {'id': 31337,
'test_arg': 'meow'}
import pdb
pdb.set_trace()
result = driver.encode_instance(instance, True)
expected = {'id': instance['id'], '_is_precooked': False}
self.assertThat(result, matchers.DictMatches(expected))
# Orig dict not changed
self.assertNotEqual(result, instance)
result = driver.encode_instance(instance, False)
expected = {}
expected.update(instance)
expected['_is_precooked'] = True
self.assertThat(result, matchers.DictMatches(expected))
# Orig dict not changed
self.assertNotEqual(result, instance)
2) 执行“nosetests nova/tests/scheduler/test_scheduler.py -s”
root@devstack:~/src/nova-ce/nova-1/nova# nosetests nova/tests/scheduler/test_scheduler.py -s
...................> /root/src/nova-ce/nova-1/nova/nova/tests/scheduler/test_scheduler.py(968)test_encode_instance()
-> result = driver.encode_instance(instance, True)
(Pdb) p instance
{'test_arg': 'meow', 'id': 31337}
(Pdb) n
> /root/src/nova-ce/nova-1/nova/nova/tests/scheduler/test_scheduler.py(969)test_encode_instance()
-> expected = {'id': instance['id'], '_is_precooked': False}
(Pdb)
OK,停下来了,现在可以调试了。