运行unittest时,Pycharm和python命令行输出显示不同的结果

时间:2019-04-15 21:04:05

标签: python python-3.x pycharm

在pycharm和命令行中运行python脚本时,结果不同。为什么以及如何解决?

我正在运行带有两个测试的单元测试,希望其中一个通过并且失败:

import unittest


class TestSum(unittest.TestCase):

    def test_sum(self):
        self.assertEqual(sum([1, 2, 3]), 6, "Should be 6")

    def test_sum_tuple(self):
        self.assertEqual(sum((1, 2, 2)), 6, "Should be 5")


if __name__ == '__main__':
    unittest.main()

pycharm输出:

Testing started at 22:53 ...
"C:\Users\python\pyrequests\venv\Scripts\python.exe" "C:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pycharm\_jb_unittest_runner.py" --target test_sum_unittest.TestSum.test_sum_tuple


Ran 1 test in 0.000s


Launching unittests with arguments python -m unittest test_sum_unittest.TestSum.test_sum_tuple in C:\Users\python\pyrequests
OK

Process finished with exit code 0

Powershell输出:

PS C:\Users\python\pyrequests> python .\test_sum_unittest.py
.F
======================================================================
FAIL: test_sum_tuple (__main__.TestSum)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\test_sum_unittest.py", line 11, in test_sum_tuple
    self.assertEqual(sum((1, 2, 2)), 6, "Should be 5")
AssertionError: 5 != 6 : Should be 5

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)

0 个答案:

没有答案