ndex.py ---- web.py主文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
urls = ("/.*", "hello")
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world!'
app.wsgifunc()
nose_test.py ---- 测试文件脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import index
app = None
class TestIndex(object):
def setUp(self):
print 'init in class'
global app
self.app = app
def test_index(self):
print 'test in class'
r = self.app.request('/')
assert r.status == '200 OK'
def setUp():
print 'init in func'
global app
app = index.app
def test_index():
print 'test in func'
global app
r = app.request('/')
assert r.status == '200 OK'
测试指令
nosetests nose_test.py -v
nosetests参数说明:
- -v:查看测试详细信息
- -s:显示脚本print信息,默认是print的信息是不输出的
nose会查找脚本中 test_命名的函数和Test_命名的类
运行测试脚本时,首先会运行脚本func级别的setUp()函数,
这时候初始化web.py的app
之后会执行class级别的setUp(self)函数,
这时候初始self的app变量为之前初始化的app
#这时候类的__init__()函数是不起作用的
更详细的测试用例可以在test函数中编写,
数据库之类的初始化可以再setUp()函数中编写
如果需要在执行完毕清理资源可以使用tearDown()函数