Outputting data from unit test in Python

前端 未结 14 2446
孤城傲影
孤城傲影 2020-12-02 05:45

If I\'m writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error?

14条回答
  •  离开以前
    2020-12-02 06:11

    Another option - start a debugger where the test fails.

    Try running your tests with Testoob (it will run your unittest suite without changes), and you can use the '--debug' command line switch to open a debugger when a test fails.

    Here's a terminal session on windows:

    C:\work> testoob tests.py --debug
    F
    Debugging for failure in test: test_foo (tests.MyTests.test_foo)
    > c:\python25\lib\unittest.py(334)failUnlessEqual()
    -> (msg or '%r != %r' % (first, second))
    (Pdb) up
    > c:\work\tests.py(6)test_foo()
    -> self.assertEqual(x, y)
    (Pdb) l
      1     from unittest import TestCase
      2     class MyTests(TestCase):
      3       def test_foo(self):
      4         x = 1
      5         y = 2
      6  ->     self.assertEqual(x, y)
    [EOF]
    (Pdb)
    

提交回复
热议问题