Tests succeed, still get traceback

后端 未结 5 1239
悲哀的现实
悲哀的现实 2021-01-04 01:53

I\'m using Python\'s unittest library and all the tests succeed, but I still get a traceback and I can\'t understand how I can fix the problem.

         


        
5条回答
  •  时光取名叫无心
    2021-01-04 02:23

    sys.exit(not self.result.wasSuccessful())

    I ran into this when I assumed my __main__.py in my Python package would always have the __name__, "__main__" - but when I ran my unittests through discovery there, I found that they would be executed but with a different __name__ - "package.__main__".

    Therefore I did need the following in my __main__.py just like in simple Python scripts:

    if __name__ == '__main__':
        main()
    

    instead of just

    main()
    

    If the main is unittest.main, call it with

    if __name__ == '__main__':
        main(exit=False)
    

    if you want the process to stick around in interactive mode if you're calling

    python -im package_name
    

    If you're using:

    python -m unittest discover 
    

    then I don't think exit=False should matter.

提交回复
热议问题