电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

瀏覽:6日期:2022-07-13 08:11:32

1)環(huán)境準(zhǔn)備:

接口測(cè)試的方式有很多,比如可以用工具(jmeter,postman)之類,也可以自己寫代碼進(jìn)行接口測(cè)試,工具的使用相對(duì)來說都比較簡(jiǎn)單,重點(diǎn)是要搞清楚項(xiàng)目接口的協(xié)議是什么,然后有針對(duì)性的進(jìn)行選擇,甚至當(dāng)工具不太適合項(xiàng)目時(shí)需要自己進(jìn)行開發(fā)。

在我們項(xiàng)目的初期,我們采用的是jmeter進(jìn)行接口測(cè)試,當(dāng)時(shí)覺得這個(gè)工具上手簡(jiǎn)單,團(tuán)隊(duì)成員學(xué)習(xí)成本低,并且接口測(cè)試的腳本稍微調(diào)整一下還可以用來做性能測(cè)試。

不過隨著項(xiàng)目規(guī)模、團(tuán)隊(duì)人數(shù)的不斷增長(zhǎng),漸漸的這個(gè)工具有適應(yīng)不了當(dāng)前項(xiàng)目的需求了,為此我們項(xiàng)目也重新開發(fā)了相關(guān)接口自動(dòng)化的平臺(tái)。但是,但是。。。可能是我讓大家中毒太深,現(xiàn)在很多同學(xué)一提到接口測(cè)試關(guān)聯(lián)到j(luò)meter,為此,我深深感到不安。畢竟jmeter只是個(gè)工具,換個(gè)項(xiàng)目換個(gè)協(xié)議你是否還能玩轉(zhuǎn)接口測(cè)試呢?session和cookie有什么區(qū)別?工具又是怎么實(shí)現(xiàn)的呢?

比如session如何保存,接口依賴如何處理,case如何管理及執(zhí)行順序,測(cè)試數(shù)據(jù)如何管理等等題,這個(gè)過程也有助于我們更加深刻的理解接口測(cè)試和http協(xié)議。

本文主要采用python語言,python中http協(xié)議接口相關(guān)的庫(kù)有urllib,urllib2以及reqeusts庫(kù),這其中reqeusts庫(kù)用來起來最方便,因此我也主要采用requests庫(kù)來做http協(xié)議的接口測(cè)試。首先來看下需要哪些環(huán)境信息:

一、安裝python

mac下自帶安裝了python,這個(gè)不多說了。

二、安裝虛擬環(huán)境:

我們?cè)谝慌_(tái)機(jī)器上可以安裝多個(gè)python版本,為了使每個(gè)版本的環(huán)境相互不受干擾,可以安裝虛擬環(huán)境,安裝方法如下:

1、安裝virtualenv:pip install virtualenv

2、新建名為venv的虛擬環(huán)境:virtualenv venv

3、進(jìn)入新環(huán)境:source venv/bin/activate

4、退出:deactivate

三、安裝requests庫(kù)

>>>pip install requests

ps:用python做http協(xié)議的接口測(cè)試會(huì)用到這個(gè)庫(kù)。

四、http測(cè)試工具:

一個(gè)使用 Python + Flask 編寫的 HTTP 請(qǐng)求和響應(yīng)服務(wù),該服務(wù)主要用于測(cè)試 HTTP 庫(kù)。后續(xù)測(cè)試我們都基于這個(gè)網(wǎng)站。

http://httpbin.org

五、在本地搭建httpbin:

考慮到測(cè)試時(shí)要不斷訪問 httpbin 網(wǎng)站,請(qǐng)求過多擔(dān)心被拉到黑名單,我們自己在本志搭建一套httpbin服務(wù)。

1、安裝:pip install gunicorn

2、安裝:pip install httpbin

3、啟動(dòng):gunicorn httpbin:app

至此,環(huán)境搭建已經(jīng)完畢,可以開始玩了~

(2)requests.get()

環(huán)境搭建好后,接下來我們先來了解一下requests的一些簡(jiǎn)單使用,主要包括:

requests常用請(qǐng)求方法使用,包括:get,postrequests庫(kù)中的Session、Cookie的使用其它高級(jí)部分:認(rèn)證、代理、證書驗(yàn)證、超時(shí)配置、錯(cuò)誤異常處理等。

本節(jié)首先來了解一下requests庫(kù)中如何發(fā)送get請(qǐng)求:

一、看下方法定義:

1、到官方文檔去了下requests.get()方法的定義,如下:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

2、點(diǎn)擊右上角的【source】,看一下它的源碼如下:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

看到最后一行return,get方法最后是通過調(diào)用 requests.request方法實(shí)現(xiàn)的,其實(shí)在其它的請(qǐng)求方法如post,put,head,delete等方法都是調(diào)用的request方法,然后把請(qǐng)求方法的類型傳遞給request方法第一個(gè)參數(shù)。

3、HTTP協(xié)議是一個(gè)基于請(qǐng)求/響應(yīng)模式的、無狀態(tài)的,應(yīng)用層協(xié)議。既然有請(qǐng)求,就有響應(yīng),來看下resquest中常用的響應(yīng)信息:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

二、get方法簡(jiǎn)單使用:

1、不帶參數(shù)的get:

# -*- coding:utf-8 -*-#不帶參數(shù)的getimport requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'get'url = ’’.join([host,endpoint])r = requests.get(url)#response = r.json()print type(r.text)print (eval(r.text))

輸出:

{ ’origin’: ’183.14.133.88’, ’headers’: { ’Connection’: ’close’, ’Host’: ’httpbin.org’, ’Accept-Encoding’: ’gzip, deflate’, ’Accept’: ’*/*’, ’User-Agent’: ’python-requests/2.18.1’ }, ’args’: { }, ’url’: ’http: //httpbin.org/get’}

2、 帶參數(shù)的get:

# -*- coding:utf-8 -*-#帶參數(shù)的getimport requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'get'url = ’’.join([host,endpoint])params = {'show_env':'1'}r = requests.get(url=url,params=params)print r.url

輸出:

http://httpbin.org/get?show_env=1{ ’origin’: ’183.14.133.88’, ’headers’: { ’X-Request-Id’: ’ebe922b4-c463-4fe9-9faf-49748d682fd7’, ’Accept-Encoding’: ’gzip, deflate’, ’X-Forwarded-Port’: ’80’, ’Total-Route-Time’: ’0’, ’Connection’: ’close’, ’Connect-Time’: ’0’, ’Via’: ’1.1vegur’, ’X-Forwarded-For’: ’183.14.133.88’, ’Accept’: ’*/*’, ’User-Agent’: ’python-requests/2.18.1’, ’X-Request-Start’: ’1504755961007’, ’Host’: ’httpbin.org’, ’X-Forwarded-Proto’: ’http’ }, ’args’: { ’show_env’: ’1’ }, ’url’: ’http: //httpbin.org/get?show_env=1’}

3、帶header的get:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'get'url = ’’.join([host,endpoint])headers = {'User-Agent':'test request headers'}r = requests.get(url)r = requests.get(url,headers=headers)#response = r.json()print (eval(r.text))[’headers’][’User-Agent’]

輸出:

test request headers

4、同時(shí)帶參數(shù)和header:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'get'url = ’’.join([host,endpoint])headers = {'User-Agent':'test request headers'}params = {'show_env':'1'}r = requests.get(url)r = requests.get(url,headers=headers,params=params)#response = r.json()print (eval(r.text))[’headers’][’User-Agent’]print r.url

輸出:

test request headershttp://httpbin.org/get?show_env=1

(3)requests.post()

一、方法定義

二、post方法簡(jiǎn)單使用

1、帶數(shù)據(jù)的post

2、帶header的post

3、帶json的post

4、帶參數(shù)的post

5、普通文件上傳

6、定制化文件上傳

7、多文件上傳

一、方法定義:

1、到官方文檔去了下requests.post()方法的定義,如下:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

2、源碼:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

3、常用返回信息:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

二、post方法簡(jiǎn)單使用:

1、帶數(shù)據(jù)的post:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])data = {’key1’:’value1’,’key2’:’value2’}r = requests.post(url,data=data)#response = r.json()print (r.text)

輸出:

{ 'args': {}, 'data': '', 'files': {}, 'form': { 'key1': 'value1', 'key2': 'value2' }, 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '23', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.1' }, 'json': null, 'origin': '183.14.133.88', 'url': 'http://httpbin.org/post'}

2、帶header的post:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])headers = {'User-Agent':'test request headers'}# r = requests.post(url)r = requests.post(url,headers=headers)#response = r.json()

輸出:

{ 'args': {}, 'data': '', 'files': {}, 'form': {}, 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '0', 'Host': 'httpbin.org', 'User-Agent': 'test request headers' }, 'json': null, 'origin': '183.14.133.88', 'url': 'http://httpbin.org/post'}

3、帶json的post:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])data = { 'sites': [{ 'name':'test' , 'url':'www.test.com' },{ 'name':'google' , 'url':'www.google.com' },{ 'name':'weibo' , 'url':'www.weibo.com' } ]}r = requests.post(url,json=data)# r = requests.post(url,data=json.dumps(data))response = r.json()

輸出:

{ 'args': {}, 'data': '{'sites': [{'url': 'www.test.com', 'name': 'test'}, {'url': 'www.google.com', 'name': 'google'}, {'url': 'www.weibo.com', 'name': 'weibo'}]}', 'files': {}, 'form': {}, 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '140', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.1' }, 'json': { 'sites': [ { 'name': 'test', 'url': 'www.test.com' }, { 'name': 'google', 'url': 'www.google.com' }, { 'name': 'weibo', 'url': 'www.weibo.com' } ] }, 'origin': '183.14.133.88', 'url': 'http://httpbin.org/post'}

4、帶參數(shù)的post:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])params = {’key1’:’params1’,’key2’:’params2’}# r = requests.post(url)r = requests.post(url,params=params)#response = r.json()print (r.text)

輸出:

{'args': {'key1': 'params1','key2': 'params2'},'data': '','files': {},'form': {},'headers': {'Accept': '*/*','Accept-Encoding': 'gzip, deflate','Connection': 'close','Content-Length': '0','Host': 'httpbin.org','User-Agent': 'python-requests/2.18.1'},'json': null,'origin': '183.14.133.88','url': 'http://httpbin.org/post?key2=params2&key1=params1'}

5.普通文件上傳:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])#普通上傳files = { ’file’:open(’test.txt’,’rb’) }r = requests.post(url,files=files)print (r.text)

輸出:

{ 'args': {}, 'data': '', 'files': { 'file': 'hello world!n' }, 'form': {}, 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '157', 'Content-Type': 'multipart/form-data; boundary=392865f79bf6431f8a53c9d56c62571e', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.1' }, 'json': null, 'origin': '183.14.133.88', 'url': 'http://httpbin.org/post'}

6.定制化文件上傳:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])#自定義文件名,文件類型、請(qǐng)求頭files = { ’file’:(’test.png’,open(’test.png’,’rb’),’image/png’)}r = requests.post(url,files=files)print (r.text)heman793

7.多文件上傳:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])#多文件上傳files = [ (’file1’,(’test.txt’,open(’test.txt’, ’rb’))), (’file2’, (’test.png’, open(’test.png’, ’rb’))) ]r = requests.post(url,files=files)print (r.text)

8.流式上傳:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'post'url = ’’.join([host,endpoint])#流式上傳with open( ’test.txt’ ) as f: r = requests.post(url,data = f)print (r.text)

輸出:

{ 'args': {}, 'data': 'hello world!n', 'files': {}, 'form': {}, 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '13', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.1' }, 'json': null, 'origin': '183.14.133.88', 'url': 'http://httpbin.org/post'}

(4)Cookie&Session

掌握了前面幾節(jié)的的內(nèi)容,就可以做一些簡(jiǎn)單的http協(xié)議接口的請(qǐng)求發(fā)送了,但是這些還不夠。HTTP協(xié)議是一個(gè)無狀態(tài)的應(yīng)用層協(xié)議,也就是說前后兩次請(qǐng)求是沒有任何關(guān)系的,那如果我們測(cè)試的接口之前有相互依賴關(guān)系怎么辦呢(比如我要在博客園發(fā)文章,是需要先登錄的),這時(shí)我們就要用到cookie和session技術(shù)來保持客戶端與服務(wù)器端連接的狀態(tài),這也就是本節(jié)要介紹的內(nèi)容:

一、Cookie:

1、獲取cookie:

# -*- coding:utf-8 -*-#獲取cookieimport requestsimport jsonurl = 'https://www.baidu.com/'r = requests.get(url)#將RequestsCookieJar轉(zhuǎn)換成字典c = requests.utils.dict_from_cookiejar(r.cookies)print r.cookiesprint cfor a in r.cookies: print a.name,a.value

輸出:

<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>{’BDORZ’: ’27315’}BDORZ 27315

2、發(fā)送Cookie

# -*- coding:utf-8 -*-#發(fā)送cookie到服務(wù)器import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'cookies'url = ’’.join([host,endpoint])#方法一:簡(jiǎn)單發(fā)送# cookies = {'aaa':'bbb'}# r = requests.get(url,cookies=cookies)# print r.text#方法二:復(fù)雜發(fā)送s = requests.session()c = requests.cookies.RequestsCookieJar()c.set(’c-name’,’c-value’,path=’/xxx/uuu’,domain=’.test.com’)s.cookies.update(c)

二、Session

1、保持會(huì)話同步:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'cookies'url = ’’.join([host,endpoint])url1 = 'http://httpbin.org/cookies/set/sessioncookie/123456789'r = requests.get(url)print r.textprint '------'s = requests.session() #初始化一個(gè)session對(duì)象s.get(url1)#cookie的信息存在了session中r = s.get(url)print r.text

輸出:

{ 'cookies': {}}------{ 'cookies': { 'sessioncookie': '123456789' }}

2、保存繪畫信息:

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'headers'url = ’’.join([host,endpoint])header1 = {'testA':'AAA'}header2 = {'testB':'BBB'}s = requests.session() #初始化一個(gè)session對(duì)象s.headers.update(header1) #已經(jīng)存在于服務(wù)中的信息r = s.get(url,headers=header2) #發(fā)送新的信息print r.text

輸出:

{ 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'Testa': 'AAA', 'Testb': 'BBB', 'User-Agent': 'python-requests/2.18.1' }}

3.刪除已存在的會(huì)話信息,保存為None

# -*- coding:utf-8 -*-import requestsimport jsonhost = 'http://httpbin.org/'endpoint = 'headers'url = ’’.join([host,endpoint])header1 = {'testA':'AAA'}header2 = {'testB':'BBB'}s = requests.session() #初始化一個(gè)session對(duì)象s.headers.update(header1) #已經(jīng)存在于服務(wù)中的信息r = s.get(url,headers=header2) #發(fā)送新的信息print r.textprint ’--------’s.headers[’testA’] = None #刪除會(huì)話里的信息testAr1 = s.get(url,headers = header2)print r1.text

{ 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'Testa': 'AAA', 'Testb': 'BBB', 'User-Agent': 'python-requests/2.18.1' }}--------{ 'headers': { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'Testb': 'BBB', 'User-Agent': 'python-requests/2.18.1' }}

4、提供默認(rèn)數(shù)據(jù):

s = requests.Session()s.auth = (’user’, ’pass’)s.headers.update({’x-test’: ’true’})# both ’x-test’ and ’x-test2’ are sents.get(’http://httpbin.org/headers’, headers={’x-test2’: ’true’})

參考:

http://docs.python-requests.org/en/master/user/quickstart/#cookieshttp://docs.python-requests.org/en/master/user/advanced/#session-objects

(5)其他(認(rèn)證&代理&超時(shí)設(shè)置)

一、認(rèn)證

1、基本認(rèn)證:

# -*- coding:utf-8 -*-import requestsurl = 'http://httpbin.org/basic-auth/user/passwd'r1 = requests.get(url)print '未提供用戶名密碼:' + str(r1.status_code)#Basic Authenticationr2 = requests.get(url,auth=(’user’,’passwd’))print '已提供用戶名密碼:' + str(r2.status_code)

輸出:

未提供用戶名密碼:401已提供用戶名密碼:200

2、數(shù)字認(rèn)證:

>>> from requests.auth import HTTPDigestAuth>>> url = ’http://httpbin.org/digest-auth/auth/user/pass’>>> requests.get(url, auth=HTTPDigestAuth(’user’, ’pass’))<Response [200]>

3、OAuth認(rèn)證:

參考:http://docs.python-requests.org/en/master/user/authentication/

二、代理

1、方法一:proxy參數(shù):

import requestsproxies = { 'https': 'http://41.118.132.69:4433'}r = requests.post('http://httpbin.org/post', proxies=proxies)print r.text

2、方法二:設(shè)置環(huán)境變量:

$ export HTTP_PROXY='http://10.10.1.10:3128'$ export HTTPS_PROXY='http://10.10.1.10:1080'$ python>>> import requests>>> requests.get(’http://example.org’)

3、HTTP Basic Auth使用代理方法:http://user:password@host/

proxies = {’http’: ’http://user:pass@10.10.1.10:3128/’}

三、證書驗(yàn)證

1、SSL證書(HTTPS):

import requests#跳過12306 的證書驗(yàn)證,把 verify 設(shè)置為 False: r = requests.get(’https://kyfw.12306.cn/otn/’, verify=False)print r.text

2、客戶端證書:

>>> requests.get(’https://kennethreitz.org’, cert=(’/path/client.cert’, ’/path/client.key’))<Response [200]>

or

s = requests.Session()s.cert = ’/path/client.cert’

四、超時(shí)配置

1 、利用timeout參數(shù)來配置最大請(qǐng)求時(shí)間:

r = requests.get(’https://github.com’, timeout=5)

2、設(shè)置timeout=None,告訴請(qǐng)求永遠(yuǎn)等待響應(yīng),而不將請(qǐng)求作為超時(shí)值傳遞

r = requests.get(’https://github.com’, timeout=None)

五、錯(cuò)誤異常

1、所有Requests顯式拋出的異常都繼承自:requests.exctptions.RequestException

2、遇到網(wǎng)絡(luò)問題(如:DNS查詢失敗,拒絕連接等)時(shí),requests會(huì)拋出一個(gè)ConnectionError異常

3、遇到罕見的無效HTTP響應(yīng)時(shí),Request則會(huì)拋出一個(gè)HTTPError異常

4、若請(qǐng)求超時(shí),則拋出一個(gè)Timeout異常

5、若請(qǐng)求超過了最大的重寫向次數(shù),則會(huì)拋出一個(gè)TooManyRedirects異常

(6)unittest-單個(gè)用例管理:

上面主要介紹了環(huán)境搭建和requests庫(kù)的使用,可以使用這些進(jìn)行接口請(qǐng)求的發(fā)送。但是如何管理接口案例?返回結(jié)果如何自動(dòng)校驗(yàn)?這些內(nèi)容光靠上面五節(jié)是不行的,因此從本節(jié)開始我們引入python單元測(cè)試框架 unittest,用它來處理批量用例管理,校驗(yàn)返回結(jié)果,初始化工作以及測(cè)試完成后的環(huán)境復(fù)原工作等等。

一、單個(gè)用例管理起來比較簡(jiǎn)單,參考如下圖,單個(gè)用例一般多用在調(diào)試的時(shí)候:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

二、代碼如下:

# -*- coding:utf-8 -*-# 單個(gè)用例執(zhí)行# 1、導(dǎo)入模塊import unittest# 2、繼承自u(píng)nittest.TestCase類class TestOne(unittest.TestCase): # 3、配置環(huán)境:進(jìn)行測(cè)試前的初始化工作 def setUp(self): print ’ncases before’ pass # 4、定義測(cè)試用例,名字以“test”開頭 def test_add(self): ’’’test add method’’’ print ’add...’ a = 3 + 4 b = 7 # 5、定義assert斷言,判斷測(cè)試結(jié)果 self.assertEqual(a, b) def test_sub(self): ’’’test sub method’’’ print ’sub...’ a = 10 - 5 b = 4 self.assertEqual(a, b) # 6、清理環(huán)境 def tearDown(self): print ’case after’ pass# 7、該方法會(huì)搜索該模塊下所有以test開頭的測(cè)試用例方法,并自動(dòng)執(zhí)行它們if __name__ == ’__main__’: unittest.main()

輸出:

Ran 2 tests in 0.001sOKcases beforeadd...case aftercases beforesub...case afterProcess finished with exit code 0

(8)unittest-生成測(cè)試報(bào)告:

用例的管理問題解決了后,接下來要考慮的就是報(bào)告我問題了,這里生成測(cè)試報(bào)告主要用到HTMLTestRunner.py 這個(gè)模塊,下面簡(jiǎn)單介紹一下如何使用:

一、下載HTMLTestRunner下載:

這個(gè)模塊不能通過pip安裝,只能下載安裝,下載地址如下:

python2.x版本:http://tungwaiyip.info/software/HTMLTestRunner.html

python3.x版本:http://hzqldjb.blog.51cto.com/9587820/1590802

二、mac下配置

1、終端進(jìn)入python環(huán)境

2、輸入:

import sysprint sys.path

3、找到site-packages文件夾的路徑并將下載的HTMLTestRunner.py文件拷貝到此的文件夾下

4、在python環(huán)境下,輸入 import HTMLTestRunner 不報(bào)錯(cuò)即安裝成功

三、使用該模塊生成報(bào)告:

1、目錄結(jié)構(gòu)

case包下面有baidu,httpbin兩個(gè)包 每個(gè)包下面分別有兩個(gè)測(cè)試的py文件 每個(gè)test_00x.py文件里各有2個(gè)test case run_all_case.py文件:用來執(zhí)行所有的test case且生成測(cè)試報(bào)告

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

2、運(yùn)行后生成報(bào)告如下:

Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

3、run_all_case.py代碼如下:

# -*- coding:utf-8 -*-import unittestimport osimport timeimport HTMLTestRunner# 用例路徑case_path = os.path.join(os.getcwd())# 報(bào)告存放路徑report_path = os.path.join(os.getcwd(), ’report’)print report_pathdef all_case(): discover = unittest.defaultTestLoader.discover(case_path, pattern='test*.py', top_level_dir=None) print discover return discoverif __name__ == ’__main__’: # 1、獲取當(dāng)前時(shí)間,這樣便于下面的使用。 now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time())) # 2、html報(bào)告文件路徑 report_abspath = os.path.join(report_path, 'result_'+now+'.html') # 3、打開一個(gè)文件,將result寫入此file中 fp = open(report_abspath, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u’接口自動(dòng)化測(cè)試報(bào)告,測(cè)試結(jié)果如下:’, description=u’用例執(zhí)行情況:’) # 4、調(diào)用add_case函數(shù)返回值 runner.run(all_case()) fp.close()

到此這篇關(guān)于Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python接口自動(dòng)化測(cè)試內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: RFID电子标签厂家-上海尼太普电子有限公司| 数显恒温油浴-电砂浴-高温油浴振荡器-常州迈科诺仪器有限公司 | 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 成都租车_成都租车公司_成都租车网_众行宝 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 天津货架厂_穿梭车货架_重型仓储货架_阁楼货架定制-天津钢力仓储货架生产厂家_天津钢力智能仓储装备 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | 化工ERP软件_化工新材料ERP系统_化工新材料MES软件_MES系统-广东顺景软件科技有限公司 | 水质传感器_水质监测站_雨量监测站_水文监测站-山东水境传感科技有限公司 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 陶氏道康宁消泡剂_瓦克消泡剂_蓝星_海明斯德谦_广百进口消泡剂 | 西安展台设计搭建_西安活动策划公司_西安会议会场布置_西安展厅设计西安旭阳展览展示 | 定制/定做衬衫厂家/公司-衬衫订做/订制价格/费用-北京圣达信 | 北京网络营销推广_百度SEO搜索引擎优化公司_网站排名优化_谷歌SEO - 北京卓立海创信息技术有限公司 | 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 水厂污泥地磅|污泥处理地磅厂家|地磅无人值守称重系统升级改造|地磅自动称重系统维修-河南成辉电子科技有限公司 | 品牌广告服务平台,好排名,好流量,好生意。 | 哈尔滨京科脑康神经内科医院-哈尔滨治疗头痛医院-哈尔滨治疗癫痫康复医院 | 耐高温电缆厂家-远洋高温电缆| 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 自清洗过滤器-全自动自清洗过反冲洗过滤器 - 中乂(北京)科技有限公司 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 月嫂_保姆_育婴_催乳_母婴护理_产后康复_养老护理-吉祥到家家政 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 直齿驱动-新型回转驱动和回转支承解决方案提供商-不二传动 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | 电车线(用于供电给电车的输电线路)-百科 | 陕西自考报名_陕西自学考试网 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 |