【问题标题】:Debug django tests调试 django 测试
【发布时间】:2014-06-04 04:46:41
【问题描述】:

我看到TestCase 有一个方法Debug(),但我找不到任何关于如何实现它的示例。据我尝试,没有任何效果。

谁能提供一些关于如何使用它的代码?

【问题讨论】:

    标签: django django-tests


    【解决方案1】:

    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
    

    【讨论】:

    • 当然,我一直在使用pdb。我只是好奇TestCase.Debug() 是否提供了更好的东西。你能解释一下如何手动启动测试吗?
    • @ben 我用如何使用debug() 替换了答案——希望这会有所帮助!
    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 2019-11-13
    • 2017-10-10
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多