Я новичок в Python. Я пытаюсь понять, что можно найти в модуле python unittest. Я написал два тестовых скрипта, которые содержат те же тесты (один и тот же файл реплицируется, чтобы проверить параметр обнаружения), содержимое которого приведено ниже:.команда открытия не работает в python unittest
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
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()
Выход
Теперь, когда я бегу "питон -m UnitTest открыть" команду из командной строки , Я получаю следующий вывод:
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),
Из-за этого создается впечатление, что тесты выполняются более одного раза. Почему это происходит? Что я делаю неправильно?
You вызывают 'unittest.main()' в конце каждого модуля. Когда вы используете обнаружение, он импортирует два модуля, тем самым запуская в них код → выполняются единичные тесты, а затем вызов unittest discovery выполняет все модульные тесты. Вот почему они работают дважды. – BlackJack
Но почему я получаю абсурдные тесты, например, тесты Ran 6 в 0.000s, Ran 4 тесты в 0.000s, Ran 6 тестов в 0.000s, Ran 4 теста в 0.000s, Ran 2 тесты в 0.000s. Он ясно показывает, что он выполняет 22 метода тестирования, в то время как в соответствии с вашим комментарием, даже если он выполняет каждый тестовый пример, общее выполнение теста должно быть 12, а не 22, из которых выполняются дополнительные 10 методов? –
, что приведены ниже ошибки означают: ОШИБКА: testSecond (unittest.loader.ModuleImportFailure) ImportError: Не удалось импортировать модуль тестирования: testSecond Traceback (самый последний вызов последнего): Файл «C: \ python27 \ Lib \ UnitTest \ loader.py "строка 254, в _find_tests модуль = self._get_module_from_name (имя) Файл "C: \ python27 \ Lib \ UnitTest \ loader.py", строка 232, в _get_module_from_name __import __ (имя) Файл" testSecond .py», строка 28, в unittest.main() Файл "C: \ python27 \ Lib \ UnitTest \ main.py", строка 234, в runTests sys.exit (не self.result. wasSuccessful()) SystemExit: True –