Ответ 1
Это не пропускает args, используя unittest discovery, но выполняет то, что вы пытаетесь сделать.
Это 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 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
Используя unittest discovery, например:
~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)