我是Python新手。我试图了解发现选项python unittest模块。我编写了两个测试脚本,其中包含相同的测试(复制相同的文件,测试发现选项),其内容如下:。
testFirst.py
testError (testFirst.Test) ... ERROR
testFailure (testFirst.Test) ... FAIL
testPass (testFirst.Test) ... ok
testError (testSecond.Test) ... ERROR
testFailure (testSecond.Test) ... FAIL
testPass (testSecond.Test) ... ok
======================================================================
ERROR: testError (testFirst.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testFirst.py", line 21, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
ERROR: testError (testSecond.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testSecond.py", line 21, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
FAIL: testFailure (testFirst.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testFirst.py", line 18, in testFailure
self.assertFalse(True)
AssertionError: True is not false
======================================================================
FAIL: testFailure (testSecond.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testSecond.py", line 18, in testFailure
self.assertFalse(True)
AssertionError: True is not false
----------------------------------------------------------------------
Ran 6 tests in 0.000s
FAILED (failures=2, errors=2)
testError (testFirst.Test) ... ERROR
testFailure (testFirst.Test) ... FAIL
testPass (testFirst.Test) ... ok
testSecond (unittest.loader.ModuleImportFailure) ... ERROR
======================================================================
ERROR: testError (testFirst.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testFirst.py", line 21, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
ERROR: testSecond (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: testSecond
Traceback (most recent call last):
File "C:\Python27\lib\unittest\loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "C:\Python27\lib\unittest\loader.py", line 232, in _get_module_from_name
__import__(name)
File "testSecond.py", line 28, in <module>
unittest.main()
File "C:\Python27\lib\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\Python27\lib\unittest\main.py", line 234, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: True
======================================================================
FAIL: testFailure (testFirst.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testFirst.py", line 18, in testFailure
self.assertFalse(True)
AssertionError: True is not false
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=1, errors=2)
testError (testFirst.Test) ... ERROR
testFailure (testFirst.Test) ... FAIL
testPass (testFirst.Test) ... ok
testError (testSecond.Test) ... ERROR
testFailure (testSecond.Test) ... FAIL
testPass (testSecond.Test) ... ok
======================================================================
ERROR: testError (testFirst.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testFirst.py", line 21, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
ERROR: testError (testSecond.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testSecond.py", line 21, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
FAIL: testFailure (testFirst.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testFirst.py", line 18, in testFailure
self.assertFalse(True)
AssertionError: True is not false
======================================================================
FAIL: testFailure (testSecond.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testSecond.py", line 18, in testFailure
self.assertFalse(True)
AssertionError: True is not false
----------------------------------------------------------------------
Ran 6 tests in 0.000s
FAILED (failures=2, errors=2)
testFirst (unittest.loader.ModuleImportFailure) ... ERROR
testError (testSecond.Test) ... ERROR
testFailure (testSecond.Test) ... FAIL
testPass (testSecond.Test) ... ok
======================================================================
ERROR: testFirst (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: testFirst
Traceback (most recent call last):
File "C:\Python27\lib\unittest\loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "C:\Python27\lib\unittest\loader.py", line 232, in _get_module_from_name
__import__(name)
File "testFirst.py", line 28, in <module>
unittest.main()
File "C:\Python27\lib\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\Python27\lib\unittest\main.py", line 234, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: True
======================================================================
ERROR: testError (testSecond.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testSecond.py", line 21, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
FAIL: testFailure (testSecond.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testSecond.py", line 18, in testFailure
self.assertFalse(True)
AssertionError: True is not false
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=1, errors=2)
EE
======================================================================
ERROR: testFirst (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: testFirst
Traceback (most recent call last):
File "C:\Python27\lib\unittest\loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "C:\Python27\lib\unittest\loader.py", line 232, in _get_module_from_name
__import__(name)
File "testFirst.py", line 28, in <module>
unittest.main()
File "C:\Python27\lib\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\Python27\lib\unittest\main.py", line 234, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: True
======================================================================
ERROR: testSecond (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: testSecond
Traceback (most recent call last):
File "C:\Python27\lib\unittest\loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "C:\Python27\lib\unittest\loader.py", line 232, in _get_module_from_name
__import__(name)
File "testSecond.py", line 28, in <module>
unittest.main()
File "C:\Python27\lib\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\Python27\lib\unittest\main.py", line 234, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: True
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=2),
testSecond.py
import matplotlib
matplotlib.get_backend()
输出
现在,当我从命令提示符运行“python -m unittest discover”命令时,我得到以下输出:
Qt4Agg
这给人的印象是测试执行了不止一次。为什么会这样?我做错了什么?
答案 0 :(得分:1)
如果您删除每个文件中的最后3行,而是在调用-v
时传递详细选项 unittest
,则只运行一次测试:
python -m unittest discover -v
结果:
testError (test_first.Test) ... ERROR
testFailure (test_first.Test) ... FAIL
testPass (test_first.Test) ... ok
testError (test_second.Test) ... ERROR
testFailure (test_second.Test) ... FAIL
testPass (test_second.Test) ... ok
======================================================================
ERROR: testError (test_first.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_first.py", line 9, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
ERROR: testError (test_second.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_second.py", line 9, in testError
0 / 0
ZeroDivisionError: integer division or modulo by zero
======================================================================
FAIL: testFailure (test_first.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_first.py", line 6, in testFailure
self.assertFalse(True)
AssertionError: True is not false
======================================================================
FAIL: testFailure (test_second.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_second.py", line 6, in testFailure
self.assertFalse(True)
AssertionError: True is not false
----------------------------------------------------------------------
Ran 6 tests in 0.004s
FAILED (failures=2, errors=2)