[英]discover command not working in python unittest
I am new to Python. 我是Python的新手。 I am trying to understand discover option python unittest module.
我试图了解发现选项python unittest模块。 I have written two test scripts, which contains same tests(the same file is replicated, to test discover option), whose contents are given below : .
我编写了两个测试脚本,其中包含相同的测试(复制了相同的文件以测试Discover选项),其内容如下:
testFirst.py testFirst.py
import unittest
class Test(unittest.TestCase):
def testFailure(self):
self.assertFalse(True)
def testError(self):
0 / 0
def testPass(self):
self.assertTrue(True)
import sys
sys.argv.append("-v")
unittest.main()
testSecond.py testSecond.py
import unittest
class Test(unittest.TestCase):
def testFailure(self):
self.assertFalse(True)
def testError(self):
0 / 0
def testPass(self):
self.assertTrue(True)
import sys
sys.argv.append("-v")
unittest.main()
Output 输出量
Now when I run "python -m unittest discover" command from command prompt, I get the following output : 现在,当我从命令提示符运行“ python -m unittest discover”命令时,得到以下输出:
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),
Which gives the impression that the tests get executed more than one time. 给人的印象是测试执行了多次。 Why is this happening?
为什么会这样呢? What do I do wrong?
我做错了什么?
If you remove the last 3 lines in each file and instead pass the verbose option -v
when calling unittest
, it runs the tests exactly once: 如果您删除每个文件的最后3行,而是在调用
unittest
时通过详细选项 -v
,则它将只运行一次测试:
python -m unittest discover -v
Result: 结果:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.