unittest colored output

前端 未结 9 2081
予麋鹿
予麋鹿 2020-12-13 17:39

I use unittest (actually unittest2) for Python testing, together with Python Mock for mocking objects and nose to run all tests in a single pass.

相关标签:
9条回答
  • 2020-12-13 18:12

    If you could change just the line of your test imports, you could use redgreenunittest. It's a clone I made of unittest, but it has colorized output.

    If you want to use it without updating any of the meat of your code, you can just use it like so:

    import redgreenunittest as unittest
    

    It's not a clone of unittest2, so it wouldn't work out-of-the-box with Andrea's code, but its source is right there, so a unittest2 fork of redgreenunittest wouldn't be out of the question.

    Also, any "you're doing it wrong" comments are welcome, so long as they contain some reasoning. I'd love to do it right instead.

    0 讨论(0)
  • 2020-12-13 18:14

    If you are running pytest this way:

    python -m unittest test_my.py
    

    Change it to:

    pytest test_my.py
    

    And you get colors for free

    0 讨论(0)
  • 2020-12-13 18:16

    Make a class that inherits from unittest.TestResult (say, MyResults) and implements a bunch of methods. Then make a class that inherits from unittest.TextTestRunner (say, MyRunner) and override _makeResult() to return an instance of MyResults.

    Then, construct a test suite (which you've probably already got working), and call MyRunner().run(suite).

    You can put whatever behavior you like, including colors, into MyResults.

    0 讨论(0)
提交回复
热议问题