diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2018-11-15 21:26:15 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2018-11-15 21:26:15 +0300 |
commit | eff760bd2b79f6db1de00a9d871e45ada673c8fc (patch) | |
tree | 13a051936dc60d006d19ac0c289d49f06eeac0c7 /test/unit.py | |
parent | c1fd6cb589d08a5feae5399d176e6e02599c823d (diff) | |
download | unit-eff760bd2b79f6db1de00a9d871e45ada673c8fc.tar.gz unit-eff760bd2b79f6db1de00a9d871e45ada673c8fc.tar.bz2 |
Tests: added command line arguments parsing in tests.
Added the following command line arguments:
-d, --detailed: Show detailed output for tests
Usage examples:
./test/run.py --detailed
python3 test/test_access_log.py --detailed
python3 test/test_access_log.py -d TestUnitAccessLog.test_access_log_ipv6
-l, --log: Save unit.log after the test execution
Usage examples:
./test/run.py -l
python3 test/test_access_log.py -l
python3 test/test_access_log.py --log TestUnitAccessLog.test_access_log_ipv6
Diffstat (limited to 'test/unit.py')
-rw-r--r-- | test/unit.py | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/test/unit.py b/test/unit.py index a5f96968..0415d83a 100644 --- a/test/unit.py +++ b/test/unit.py @@ -7,6 +7,7 @@ import time import shutil import socket import select +import argparse import platform import tempfile import unittest @@ -19,6 +20,27 @@ class TestUnit(unittest.TestCase): architecture = platform.architecture()[0] maxDiff = None + detailed = False + save_log = False + + def __init__(self, methodName='runTest'): + super().__init__(methodName) + + if re.match(r'.*\/run\.py$', sys.argv[0]): + args, rest = TestUnit._parse_args() + + TestUnit._set_args(args) + + @staticmethod + def main(): + args, rest = TestUnit._parse_args() + + sys.argv = sys.argv[:1] + rest + + TestUnit._set_args(args) + + unittest.main() + def setUp(self): self._run() @@ -49,7 +71,7 @@ class TestUnit(unittest.TestCase): # remove unit.log - if '--leave' not in sys.argv and success: + if not TestUnit.save_log and success: shutil.rmtree(self.testdir) else: @@ -227,6 +249,22 @@ class TestUnit(unittest.TestCase): return ret + @staticmethod + def _parse_args(): + parser = argparse.ArgumentParser(add_help=False) + + parser.add_argument('-d', '--detailed', dest='detailed', + action='store_true', help='Detailed output for tests') + parser.add_argument('-l', '--log', dest='save_log', + action='store_true', help='Save unit.log after the test execution') + + return parser.parse_known_args() + + @staticmethod + def _set_args(args): + TestUnit.detailed = args.detailed + TestUnit.save_log = args.save_log + def _print_path_to_log(self): print('Path to unit.log:\n' + self.testdir + '/unit.log') @@ -296,7 +334,7 @@ class TestUnitHTTP(TestUnit): sock.sendall(req) - if '--verbose' in sys.argv: + if TestUnit.detailed: print('>>>', req, sep='\n') resp = '' @@ -305,7 +343,7 @@ class TestUnitHTTP(TestUnit): enc = 'utf-8' if 'encoding' not in kwargs else kwargs['encoding'] resp = self.recvall(sock).decode(enc) - if '--verbose' in sys.argv: + if TestUnit.detailed: print('<<<', resp.encode('utf-8'), sep='\n') if 'raw_resp' not in kwargs: |