From 91504fbc2c716e18f7221886f2738d5927199336 Mon Sep 17 00:00:00 2001 From: Massimo Date: Thu, 21 Mar 2013 12:04:39 -0500 Subject: [PATCH] better test_web.py, thanks Niphlod --- VERSION | 2 +- gluon/tests/test_web.py | 58 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index b1b82412..ff589fc6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.5-stable+timestamp.2013.03.21.11.29.00 +Version 2.4.5-stable+timestamp.2013.03.21.12.03.58 diff --git a/gluon/tests/test_web.py b/gluon/tests/test_web.py index ab12c70d..e5e68605 100644 --- a/gluon/tests/test_web.py +++ b/gluon/tests/test_web.py @@ -11,11 +11,58 @@ else: sys.path.append(os.path.realpath('../')) import unittest +import subprocess +import time +import signal from contrib.webclient import WebClient +webserverprocess = None -class TestWeb(unittest.TestCase): - def testWebClient(self): +def startwebserver(): + global webserverprocess + webserverprocess = subprocess.Popen([sys.executable, 'web2py.py', '-a', 'testpass']) + print 'Sleeping before web2py starts...' + for a in range(1,11): + time.sleep(1) + print a, '...' + print '' + +def terminate_process(pid): + #Taken from http://stackoverflow.com/questions/1064335/in-python-2-5-how-do-i-kill-a-subprocess + # all this shit is because we are stuck with Python 2.5 and \ + #we cannot use Popen.terminate() + if sys.platform.startswith('win'): + import ctypes + PROCESS_TERMINATE = 1 + handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, pid) + ctypes.windll.kernel32.TerminateProcess(handle, -1) + ctypes.windll.kernel32.CloseHandle(handle) + else: + os.kill(pid, signal.SIGKILL) + +def stopwebserver(): + global webserverprocess + print 'Killing webserver' + if sys.version_info < (2,6): + terminate_process(webserverprocess.pid) + else: + webserverprocess.terminate() + + +class LiveTest(unittest.TestCase): + + @classmethod + def setUpClass(cls): + startwebserver() + + @classmethod + def tearDownClass(cls): + stopwebserver() + + + +class TestWeb(LiveTest): + def testRegisterAndLogin(self): client = WebClient('http://127.0.0.1:8000/welcome/default/') client.get('index') @@ -40,7 +87,7 @@ class TestWeb(unittest.TestCase): # check registration and login were successful client.get('index') - print client.text + self.assertTrue('Welcome Homer' in client.text) client = WebClient('http://127.0.0.1:8000/admin/default/') @@ -48,9 +95,7 @@ class TestWeb(unittest.TestCase): client.get('site') client.get('design/welcome') - -class TestStaticCacheControl(unittest.TestCase): - def testWebClient(self): + def testStaticCache(self): s = WebClient('http://127.0.0.1:8000/welcome/') s.get('static/js/web2py.js') assert('expires' not in s.headers) @@ -61,5 +106,6 @@ class TestStaticCacheControl(unittest.TestCase): assert('expires' in s.headers) assert(s.headers['cache-control'].startswith('max-age')) + if __name__ == '__main__': unittest.main()