测试函数方法

! /usr/bin/env python

coding:utf-8

函数get_formatted_name()将名和姓合并成姓名,在名和姓之间加上一个空格,并将它们的首字母都大写,再返回结果

def get_formatted_name(first,last,middle=''):
# Generate a neatly formatted full name.
if middle:
full_name = first + ' '+ middle +' ' + last
else:
full_name = first + ' ' + last
return full_name.title()
# return ''

print get_formatted_name('li','shuxiang','liu')

!/usr/bin/env python

coding:utf-8

核实get_formatted_name()像期望的那样工作,我们来编写一个使用这个函数的程序。

程序names.py让用户输入名和姓,并显示整洁的全名

from namefunction import get_formatted_name

print ("Enter 'q' any time to quit ")

while True:
first = (raw_input("\nPlease input a first name:"))
if first == 'q':
break
last = raw_input("\nPlease input a last name:")
if last == 'q':
break
formated_name=get_formatted_name(first,last)
print ("\nNeatly formatted name:"+formated_name+'!')

!/usr/bin/env python

coding:utf-8

import unittest
from namefunction import get_formatted_name

import perfm.pf1

class NamesTestCase(unittest.TestCase):
#测试namefuncton.py 这些方法由Python自动调用,你根本不用编写调用它们的代码
def test_firstlast_name(self):
#能正确处理像lilili shuxiang这样的姓名吗
formatted_name = get_formatted_name('lilili','shuxiang') #函数参数
try:
self.assertEqual('Lilili Shuxiang', formatted_name,msg='')
except Exception as e:
print (Exception,e)
def test_firstlastmiddle(self):
# 能正确处理像Niu Yang Tiantian这样的姓名吗,第三个参数是middle

       formatted_middle_name = get_formatted_name('niu','tiantian','yang')
       self.assertEqual('Niu Yang Tiantian',formatted_middle_name,msg='测试带有中间名的名和姓,三个含first,middle,last')


def runTest(self):

     pass

if name == 'main':
NamesTestCase()

'''
【补充】测试过程中,还曾出现【unbound method create() must be called with SocialUrl instance as first argument】这种错误,
原因是没有将引用的类给实例化。也就是说如果我现在调用TestA的话,需要使用testA().test_one()方法。而不是testA.test_one()

例如:

!/usr/bin/env python

coding:utf-8

requests模块,unittest模块

import requests
import unittest
import json,urllib
from pprint import pprint
from requests.sessions import Session
import logging

class Login():
def _session(self):
rq = self._login()
rs = requests.Session()
#print rs
return rs

def _login(self):
identifier = ''
password = ''
host = 'http://loan-collection.test.91gfd.cn'
url =host + '/auth/local'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
params = {
'identifier': identifier,
'password': password,
'next': ''
}

    response = requests.post(url=url,data=params,headers=headers)
    #response = session.post(url=url, data=params, headers=headers)


    print ("StatusCode:", response.status_code)
    #print(response.text)
    return response

    # # lt = re.findall(r'name="lt" value="(.*?)"', response.text)
    # # print lt[0]
    # cooki = response.cookies
    # print cooki
    # cookie = response.headers["Set-Cookie"]
    # # print cookie
    # Js = re.findall(r"(JSESSIONID=.*?);", cookie)
    # print Js[0]
    #
    # # 将CookieJar转为字典:
    # cookies = requests.utils.dict_from_cookiejar(response.cookies)
    #
    # # # 将字典转为CookieJar:
    # # cook = requests.utils.cookiejar_from_dict(cookies, cookiejar=None, overwrite=True)
    # #
    # # # 其中cookie_dict是要转换字典
    # #
    # # # 转换完之后就可以把它赋给cookies
    # # # 并传入到session中了:
    # #
    # # s = requests.Session()
    # # s.cookies = cook
    # print cookies

class loginTestCase(unittest.TestCase):

def test_login(self):
    self.assertEqual(Login()._login().status_code,200)#此处Login需要实例化
    #self.assertEqual(login.rep.status_code,200)
    Login()._session()  #此处Login需要实例化
    logging.info("1 登录成功")

'''

posted @ 2018-03-21 20:27  easymoneySniper  阅读(294)  评论(0编辑  收藏  举报