Unit test script returns exit code = 0 even if tests fail

后端 未结 2 1199
悲&欢浪女
悲&欢浪女 2020-12-14 07:19

My testing script looks as follows:

import os
import sys
from unittest import defaultTestLoader as loader, TextTestRunner

path_to_my_project = os.path.dirnam         


        
相关标签:
2条回答
  • 2020-12-14 08:03

    I had some trouble getting TextTestRunner results. For those like me, here is how it works:

    """Run all tests inside of *_test.py modules located in the same directory."""
    
    
    import sys
    import unittest
    
    
    if __name__ == '__main__':
        test_suite = unittest.defaultTestLoader.discover('.', '*_test.py')
        test_runner = unittest.TextTestRunner(resultclass=unittest.TextTestResult)
        result = test_runner.run(test_suite)
        sys.exit(not result.wasSuccessful())
    
    0 讨论(0)
  • 2020-12-14 08:18

    The code is not using unittest.main. You need to check the result using TestResult.wasSuccessful and call sys.exit manually.

    import sys
    
    ....
    
    ret = not runner.run(suite).wasSuccessful()
    sys.exit(ret)
    
    0 讨论(0)
提交回复
热议问题