这种方法无法通过unittest discover来传递参数,但它可以达到你想要实现的目的。
这里是leveltest.py
代码。将它放在模块搜索路径中的某个位置(如当前目录或site-packages):
import argparse
import sys
import unittest
if sys.argv[0].endswith("__main__.py"):
import os.path
executable = os.path.basename(sys.executable)
sys.argv[0] = executable + " -m leveltest"
del os
def _id(obj):
return obj
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()
unittest.main(module=None, argv=remaining_args)
以下是如何在示例testproject.py文件中使用它:
import unittest
import leveltest
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'])
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
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)
unittest
之上构建自己的单元测试包,而不是依赖于一些本地的my_unittest.py
。不过,这仍然是一个好主意。谢谢! - Ami Tavorymy_unittest
的内容,我将很高兴接受您的答案(并授予您奖励)。 - Ami Tavory