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
This commit is contained in:
Andrey Zelenkov
2018-11-15 21:26:15 +03:00
parent c1fd6cb589
commit eff760bd2b
15 changed files with 55 additions and 17 deletions

View File

@@ -305,4 +305,4 @@ Connection: close
'reopen 2')
if __name__ == '__main__':
unittest.main()
TestUnitAccessLog.main()

View File

@@ -218,4 +218,4 @@ class TestUnitConfiguration(unit.TestUnitControl):
}), 'no port')
if __name__ == '__main__':
unittest.main()
TestUnitConfiguration.main()

View File

@@ -151,4 +151,4 @@ class TestUnitGoApplication(unit.TestUnitApplicationGo):
'arguments empty')
if __name__ == '__main__':
unittest.main()
TestUnitGoApplication.main()

View File

@@ -163,4 +163,4 @@ a
self.assertEqual(resp['status'], 200, 'transfer encoding chunked')
if __name__ == '__main__':
unittest.main()
TestUnitHTTPHeader.main()

View File

@@ -167,4 +167,4 @@ class TestUnitPerlApplication(unit.TestUnitApplicationPerl):
self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
if __name__ == '__main__':
unittest.main()
TestUnitPerlApplication.main()

View File

@@ -205,4 +205,4 @@ class TestUnitPHPApplication(unit.TestUnitApplicationPHP):
'ini value repeat')
if __name__ == '__main__':
unittest.main()
TestUnitPHPApplication.main()

View File

@@ -139,4 +139,4 @@ class TestUnitPHPBasic(unit.TestUnitControl):
'delete app again')
if __name__ == '__main__':
unittest.main()
TestUnitPHPBasic.main()

View File

@@ -349,4 +349,4 @@ Connection: close
self.assertEqual(self.get()['body'], '0123456789', 'write')
if __name__ == '__main__':
unittest.main()
TestUnitPythonApplication.main()

View File

@@ -149,4 +149,4 @@ class TestUnitPythonBasic(unit.TestUnitControl):
'delete app again')
if __name__ == '__main__':
unittest.main()
TestUnitPythonBasic.main()

View File

@@ -125,4 +125,4 @@ class TestUnitPythonEnvironment(unit.TestUnitApplicationPython):
})['body'], pwd_default, 'restore default')
if __name__ == '__main__':
unittest.main()
TestUnitPythonEnvironment.main()

View File

@@ -245,4 +245,4 @@ class TestUnitPythonProcman(unit.TestUnitApplicationPython):
self.assertEqual(len(self.pids_for_process()), 0, 'stop all')
if __name__ == '__main__':
unittest.main()
TestUnitPythonProcman.main()

View File

@@ -284,4 +284,4 @@ class TestUnitRubyApplication(unit.TestUnitApplicationRuby):
self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
if __name__ == '__main__':
unittest.main()
TestUnitRubyApplication.main()

View File

@@ -170,4 +170,4 @@ Content-Length: %d
'settings'), 'settings negative value')
if __name__ == '__main__':
unittest.main()
TestUnitSettings.main()

View File

@@ -417,4 +417,4 @@ Connection: close
self.assertEqual(resp['body'], '0123456789', 'application respawn body')
if __name__ == '__main__':
unittest.main()
TestUnitTLS.main()

View File

@@ -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: