使用單元測試發現這不通過指定參數時,但它實現了你正在努力去做。
這是leveltest.py
。把它放在某處模塊搜索路徑(也許當前目錄或站點包):
import argparse
import sys
import unittest
# this part copied from unittest.__main__.py
if sys.argv[0].endswith("__main__.py"):
import os.path
# We change sys.argv[0] to make help message more useful
# use executable without path, unquoted
# (it's just a hint anyway)
# (if you have spaces in your executable you get what you deserve!)
executable = os.path.basename(sys.executable)
sys.argv[0] = executable + " -m leveltest"
del os
def _id(obj):
return obj
# decorator that assigns test levels to test cases (classes and methods)
def level(testlevel):
if unittest.level < testlevel:
return unittest.skip("test level too low.")
return _id
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--level', type=int, default=3)
ns, args = parser.parse_known_args(namespace=unittest)
return ns, sys.argv[:1] + args
if __name__ == "__main__":
ns, remaining_args = parse_args()
# this invokes unittest when leveltest invoked with -m flag like:
# python -m leveltest --level=2 discover --verbose
unittest.main(module=None, argv=remaining_args)
這裏是你如何在一個例子testproject.py文件中使用它:
import unittest
import leveltest
# This is needed before any uses of the @leveltest.level() decorator
# to parse the "--level" command argument and set the test level when
# this test file is run directly with -m
if __name__ == "__main__":
ns, remaining_args = leveltest.parse_args()
@leveltest.level(2)
class TestStringMethods(unittest.TestCase):
@leveltest.level(5)
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
@leveltest.level(3)
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
@leveltest.level(4)
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
# this invokes unittest when this file is executed with -m
unittest.main(argv=remaining_args)
然後,您可以運行測試通過直接運行testproject.py,如:
~roottwo\projects> python testproject.py --level 2 -v
test_isupper (__main__.TestStringMethods) ... skipped 'test level too low.'
test_split (__main__.TestStringMethods) ... skipped 'test level too low.'
test_upper (__main__.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK (skipped=3)
~roottwo\projects> python testproject.py --level 3 -v
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... skipped 'test level too low.'
test_upper (__main__.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK (skipped=2)
~roottwo\projects> python testproject.py --level 4 -v
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK (skipped=1)
~roottwo\projects> python testproject.py --level 5 -v
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
通過使用單元測試發現這樣的:
~roottwo\projects> python -m leveltest --level 2 -v
test_isupper (testproject.TestStringMethods) ... skipped 'test level too low.'
test_split (testproject.TestStringMethods) ... skipped 'test level too low.'
test_upper (testproject.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.003s
OK (skipped=3)
~roottwo\projects> python -m leveltest --level 3 discover -v
test_isupper (testproject.TestStringMethods) ... ok
test_split (testproject.TestStringMethods) ... skipped 'test level too low.'
test_upper (testproject.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK (skipped=2)
~roottwo\projects> python -m leveltest --level 4 -v
test_isupper (testproject.TestStringMethods) ... ok
test_split (testproject.TestStringMethods) ... ok
test_upper (testproject.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK (skipped=1)
~roottwo\projects> python -m leveltest discover --level 5 -v
test_isupper (testproject.TestStringMethods) ... ok
test_split (testproject.TestStringMethods) ... ok
test_upper (testproject.TestStringMethods) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
,或者通過指定測試用例來運行,如:
~roottwo\projects>python -m leveltest --level 3 testproject -v
test_isupper (testproject.TestStringMethods) ... ok
test_split (testproject.TestStringMethods) ... skipped 'test level too low.'
test_upper (testproject.TestStringMethods) ... skipped 'test level too low.'
----------------------------------------------------------------------
Ran 3 tests in 0.002s
OK (skipped=2)
所以,感謝您的回答,但我無法弄清楚這是否允許像'discover'這樣的功能來查看目錄中的所有文件,然後爲它們生成單個報告。 –
它使用'unittest'來完成所有的測試。所以,是的,它提供了與'unittest'相同的報告。我的答案中的示例使用-v(詳細)標誌進行單元測試,以提供有關所有測試的詳細信息,包括哪些測試級別太低而跳過了哪些測試。 – RootTwo
啊,我明白了 - 有趣。感謝您的回答 - 將更多地看待它。不勝感激! –