前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[笨方法学python]习题51自动化测试笔记

[笨方法学python]习题51自动化测试笔记

作者头像
逆向小白
发布2018-09-12 15:50:52
6790
发布2018-09-12 15:50:52
举报
文章被收录于专栏:py+seleniumpy+selenium

习题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>\n\n<form action="/hello" method="POST">\n    A Greeting: <input type="text" name="greet">\n    <br/>\n    Your Name: <input type="text" name="name">\n    <br/>\n    <input type="submit">\n</form>\n'}>

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': '\nI just wanted to say <em style="color: green; font-size: 2em;">Hello, Nobody</em>.\n'}>

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': '\nI just wanted to say <em style="color: green; font-size: 2em;">Hola, Zed</em>.\n'}>

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请求后,值为服务器返回的响应。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档