Unable to run unittest's main function in ipython/jupyter notebook

眉间皱痕 提交于 2019-11-30 05:01:58

unittest.main looks at sys.argv by default, which is what started IPython, hence the error about the kernel connection file not being a valid attribute. You can pass an explicit list to main to avoid looking up sys.argv.

In the notebook, you will also want to include exit=False to prevent unittest.main from trying to shutdown the kernel process:

unittest.main(argv=['first-arg-is-ignored'], exit=False)

You can pass further arguments in the argv list, e.g.

unittest.main(argv=['ignored', '-v'], exit=False)

We can try TestLoader to load test cases from TestCaseClass

and attach those testcases to TextTestRunner then run it.

import unittest
suite = unittest.TestLoader().loadTestsFromTestCase(Samples)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!