习题51
本节自动化测试部分看不大懂,自己每步都打印出来,帮助理解。(代码标红部分为自己加入调试为打印变量值所用)
tests/tools.py
from nose.tools import *
import re
def pt(resp,contains=None,matches=None,headers=None,status=None):
print 'resp:'.resp
print ' resp.data:',resp.data #个人加入调试,为打印出每次调用变量值
print 'contains:',contains
print 'matches:',matches
print 'headers:',headers
print 'status:',status
print '******************************************************************'
def assert_response(resp, contains=None, matches=None, headers=None, status="200"):
assert status in resp.status, "Expected response %r not in %r" % (status, resp.status)
if status == "200":
assert resp.data, "Response data is empty."
if contains:
assert contains in resp.data, "Response does not contain %r" % contains
if matches:
reg = re.compile(matches)
assert reg.matches(resp.data), "Response does not match %r" % matches
if headers:
assert_equal(resp.headers, headers)
tests/app_tests.py
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
from tests.tools import pt #个人加入调试,为打印出每次调用变量值
def test_index():
# check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status="404")
pt(resp) #个人加入调试,为打印出每次调用变量值
# test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
pt(resp) #个人加入调试,为打印出每次调用变量值
# make sure default values work for the form
resp = app.request("/hello", method="POST")
assert_response(resp, contains="Nobody")
pt(resp) #个人加入调试,为打印出每次调用变量值
# test that we get expected values
data = {'name': 'Zed', 'greet': 'Hola'}
resp = app.request("/hello", method="POST", data=data)
assert_response(resp, contains="Zed")
pt(resp) #个人加入调试,为打印出每次调用变量值
test_index() #个人加入调试,为打印出每次调用变量值
我将每一次调用assert_response(),各个变量的值打印出来:(**********表示第一次调用结束)
resp: <Storage {'status': '404 Not Found', 'headers': {'Content-Type': 'text/html'}, 'header_items': [('Content-Type', 'text/html')], 'data': 'not found'}>
resp.data: not found
contains: None
matches: None
headers: None
status: None
********************************************************
resp: <Storage {'status': '200 OK', 'headers': {'Content-Type': 'text/html; charset=utf-8'}, 'header_items': [('Content-Type', 'text/html; charset=utf-8')], 'data': '<h1>Fill Out This Form</h1> <form action="/hello" method="POST"> A Greeting: <input type="text" name="greet"> <br/> Your Name: <input type="text" name="name"> <br/> <input type="submit"> </form> '}>
resp.data: <h1>Fill Out This Form</h1>
<form action="/hello" method="POST">
A Greeting: <input type="text" name="greet">
<br/>
Your Name: <input type="text" name="name">
<br/>
<input type="submit">
</form>
contains: None
matches: None
headers: None
status: None
********************************************************
resp: <Storage {'status': '200 OK', 'headers': {'Content-Type': 'text/html; charset=utf-8'}, 'header_items': [('Content-Type', 'text/html; charset=utf-8')], 'data': ' I just wanted to say <em style="color: green; font-size: 2em;">Hello, Nobody</em>. '}>
resp.data:
I just wanted to say <em style="color: green; font-size: 2em;">Hello, Nobody</em>.
contains: None
matches: None
headers: None
status: None
********************************************************
resp: <Storage {'status': '200 OK', 'headers': {'Content-Type': 'text/html; charset=utf-8'}, 'header_items': [('Content-Type', 'text/html; charset=utf-8')], 'data': ' I just wanted to say <em style="color: green; font-size: 2em;">Hola, Zed</em>. '}>
resp.data:
I just wanted to say <em style="color: green; font-size: 2em;">Hola, Zed</em>.
contains: None
matches: None
headers: None
status: None
********************************************************
___________________________________________________
assert断言语句
assert 1==2,‘不等于!’
若表达式为真则无回显,若不为真,则抛出异常。
app.request("/"):为发送一个url请求后,值为服务器返回的响应。