调试django测试

时间:2014-06-04 04:46:41

标签: django django-tests

我看到TestCase有一个方法Debug(),但我找不到任何关于如何实现它的示例。据我所知,没有任何作用。

任何人都可以提供一些如何使用它的代码吗?

1 个答案:

答案 0 :(得分:0)

debugunit.py

from unittest import TestCase

class MyTest(TestCase):
    def test1(self):
        print 'before'
        self.assertEquals(2+2, 5)
        print 'after'

运行

  

python -i debugunit.py

要以交互方式运行测试,请创建一个TestCase实例,并将测试名称作为参数。要运行它,请调用生成的对象。

>>> print MyTest('test1')()
before
None

单位测试机器消耗“2 + 2!= 5”异常。要设置运行(使用setUptearDown等),请运行debug()方法:

>>> MyTest('test1').debug()
before
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/unittest/case.py", line 400, in debug
    getattr(self, self._testMethodName)()
  File "debugunit.py", line 6, in test1
    self.assertEquals(2+2, 5)
  File "/usr/lib/python2.7/unittest/case.py", line 515, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python2.7/unittest/case.py", line 508, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 4 != 5
相关问题