python 自動化接口測試(6)


   迎接新的一波更新吧,這次是基於圖靈機器人的一個api接口的測試。

 這是api的接口:http://www.tuling123.com/openapi/api
 我們試着通過瀏覽器直接訪問看下

 這是反饋的結果,那么我們來看下圖靈機器人這邊給的接口文檔,http://www.tuling123.com/help/h_cent_webapi.jhtml?nav=doc這是文檔中心,這里的編寫很規范的,我們看到這個就很好知道我們想要用的接口,需要的東西,以及簡單的接口說明,我們可以從這里很快的得到我們想要的信息。

這里面詳細的給我們描述了需要的接口地址,包括請求格式和請求參數,那么我們接下來,來分析下,

可見這個文檔給我們了請求地址,請求參數格式,那么我們接下來需要想想我們的思路,

理清我們要測試的目的,測試的思路,怎么去來寫這個測試用例,怎么組織我們想要的測試結果,盡量讓我們的測試更加全面,這里呢,

我的想法呢就是主要有一下, 測試api的url   測試請求方式   請求的參數,返回結果。那么我們的斷言寫在哪里呢,我是把斷言用返回結果code加斷言,

這是我整個目錄

讓返回結果來看看api返回值是否正確,那么我來寫我的測試用例,在這里,我寫的測試用例是基於yaml的文件寫的,方便這里的讀寫,

post:
 post1:
   key: "aaaa"
   coneent: 'sasa'
   url: 'http://www.tuling123.com/openapi/api'
   fangshi: 'POST'
   code: "40001" #密碼錯誤
 post2:
   key: "dfeb1cc8125943d29764a2f2f5c33739"
   coneent: ''
   url: 'http://www.tuling123.com/openapi/api'
   fangshi: 'POST'
   code: "40002" #未輸入內容
 post3:
   key: "dfeb1cc8125943d29764a2f2f5c33739"
   coneent: ''
   fangshi: 'POST'
   url: 'http://www.tuling123.com/openapi/api'
   code: "40007" #格式異常
 post4:
   key: "dfeb1cc8125943d29764a2f2f5c33739"
   coneent: 'sdsad'
   fangshi: 'POST'
   url: 'http://www.tuling123.com/openapi/api'
   code: "40004" #次數用完

 我的斷言就是這些code,

那么我們寫好測試用例了,下面就是來組織我們的腳本了。我喜歡吧一些我們經常用的封裝起來,不管是框架也好,還是讓我用起來方便吧,我一般都是簡單的寫幾個函數,這樣呢,在我接下來用的時候就特別方便了。我這里面呢使用的是第三方庫,理由很簡單,就是我們的第三方庫提供給我們很多便利, 我使用的是requests來做的,我們大家可以看下,教程 。      這是一個中文的教程,大家可以來試試,這里因為我是文章,就不給大家講解。大家可以看下詳細的文檔。接下來看下我封裝的,其實就是簡單的總結

# -*- coding: utf-8 -*-
# @Author  : leizi
import requests,json
class reques():
    def __init__(self):
        self.headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:51.0) Gecko/20100101 Firefox/51.0"}
    def get(self, url):#get消息
        try:
            r = requests.get(url, headers=self.headers)
            r.encoding = 'UTF-8'
            json_response = json.loads(r.text)
            return json_response
        except Exception as e:
            print('get請求出錯,出錯原因:%s'%e)
            return {}
    def post(self, url, params):#post消息
        data = json.dumps(params)
        try:
            r =requests.post(url,params=params,headers=self.headers)
            json_response = json.loads(r.text)
            return json_response
        except Exception as e:
            print('post請求出錯,原因:%s'%e)
    def delfile(self,url,params):#刪除的請求
        try:
            del_word=requests.delete(url,params,headers=self.headers)
            json_response=json.loads(del_word.text)
            return json_response
        except Exception as e:
            return {}
            print('del請求出錯,原因:%s'%e)
    def putfile(self,url,params):#put請求
        try:
            data=json.dumps(params)
            me=requests.put(url,data)
            json_response=json.loads(me.text)
            return json_response
        except Exception as e:
            print('put請求出錯,原因:%s'%e)
            return json_response

其實沒有怎么封裝吧,但是呢 還是給我提供了便利。 我封裝了幾個請求方式,這樣在接下來的使用中我可以直接使用了,我自己固定好的格式,給定的函數。

接下來就是來寫我們的用例了。這里我利用了yaml和 unittest來組織用例。yaml使用方法以及剖析。http://www.cnblogs.com/c9com/archive/2013/01/05/2845539.html

unittest詳細講解:http://www.cnblogs.com/yufeihlf/p/5707929.html

# -*- coding: utf-8 -*-
# @Author  : leizi
from fengzhuang.feng import reques
import yaml,unittest

class Test_tuling(unittest.TestCase):
    def setUp(self):
        title=u'登陸測試'
        self.data_file = open(r"C:\\Users\\Administrator\\Desktop\\jiejko\\data\\data.yaml","r",encoding= "utf-8")
        self.data = yaml.load(self.data_file)
        self.post_data=self.data['post']
    def tearDown(self):
        pass
    def test_post1(self):
        try:
            self.url=self.post_data['post1']['url']
            self.key=self.post_data['post1']['key']
            self.coneent=self.post_data['post1']['coneent']
            self.fangshi=self.post_data['post1']['fangshi']
            self.code=int(self.post_data['post1']['code'])
            self.parem={'key':self.key,'info':self.coneent}
            if self.fangshi == 'POST':
                self.me=reques().post(url=self.url,params=self.parem)
                self.assertEqual(self.me['code'],self.code,msg='接口返回標識符有誤')
            else:
                print('不支持%s方式請求'%self.fangshi)
        except Exception as e:
            print('用例1測試失敗,原因:%s'%e)

    def test_post2(self):
        try:
            self.url=self.post_data['post2']['url']
            self.key=self.post_data['post2']['key']
            self.coneent=self.post_data['post2']['coneent']
            self.fangshi=self.post_data['post2']['fangshi']
            self.code=int(self.post_data['post2']['code'])
            self.parem={'key':self.key,'info':self.coneent}
            if self.fangshi == 'POST':
                self.me=reques().post(url=self.url,params=self.parem)
                self.assertEqual(self.me['code'],self.code,msg='接口返回標識符有誤')
            else:
                print('不支持%s方式請求'%self.fangshi)
        except Exception as e:
            print('用例2測試失敗,原因:%s'%e)
    def test_post3(self):
        try:
            self.url=self.post_data['post3']['url']
            self.key=self.post_data['post3']['key']
            self.coneent=self.post_data['post3']['coneent']
            self.fangshi=self.post_data['post3']['fangshi']
            self.code=int(self.post_data['post3']['code'])
            self.parem={'key':self.key,'info':self.coneent}
            if self.fangshi == 'POST':
                self.me=reques().post(url=self.url,params=self.parem)
                self.assertEqual(self.me['code'],self.code,msg='接口返回標識符有誤')
            else:
                print('不支持%s方式請求'%self.fangshi)
        except Exception as e:
            print('用例3測試失敗,原因:%s'%e)
    def test_post4(self):
        try:
            self.url=self.post_data['post4']['url']
            self.key=self.post_data['post4']['key']
            self.coneent=self.post_data['post4']['coneent']
            self.fangshi=self.post_data['post4']['fangshi']
            self.code=int(self.post_data['post4']['code'])
            self.parem={'key':self.key,'info':self.coneent}
            if self.fangshi == 'POST':
                self.me=reques().post(url=self.url,params=self.parem)
                self.assertEqual(self.me['code'],self.code,msg='接口返回標識符有誤')
            else:
                print('不支持%s方式請求'%self.fangshi)
        except Exception as e:
            print('用例4測試失敗,原因:%s'%e)
if __name__ == '__main__':
    project_path=''
    suite = unittest.TestSuite()
    suite.addTest(Test_tuling("test_post4"))
    suite.addTest(Test_tuling('test_post3'))
    suite.addTest(Test_tuling('test_post2'))
    suite.addTest(Test_tuling('test_post1s'))
    temp=str(time.time())
    filedir=project_path+"//report//"+temp
    os.makedirs(filedir)
    filename="//pyresult.html"
    filepath=filedir+filename
    fp=file(filepath,'wb')# 調用HTMLtestrunner來執行腳本並生成測試報告,html格式的
    runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='report',description='demo')
    runner.run(suite)

這是我寫的用例。那么大家可以看出來,我的寫法也是簡單的。就是一些簡單的使用。

這邊的測試報告我使用的是HTMLrunner。

詳細代碼:GitHub傳送門  

由於在后面在6的基礎上進行了 優化,git clone代碼后,使用下面命令

git checkout  5a9c6b041aa1b47e40df52d57727ae39f3e6319c

那么我們來看下最后的測試報告,

 

 最后我還寫了發送郵件的模塊,其中加的log模塊暫時還沒有用在代碼中。 后續的優化,這樣,我就運行一下,然后最后給我發送測試報告,我不用盯着電腦了。

其實在這里,大家還可以加入多線程來跑腳本,這樣比較快。

其實大家都是為了走的更遠,做的更好。路在腳下,相信自己。

有疑問可以加我qq:952943386或者我的qq群194704520

也可以資助我下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM