better test_web.py, thanks Niphlod

This commit is contained in:
Massimo
2013-03-21 12:04:39 -05:00
parent 4b17684980
commit 91504fbc2c
2 changed files with 53 additions and 7 deletions
+1 -1
View File
@@ -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
+52 -6
View File
@@ -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()