發表文章

目前顯示的是有「Python」標籤的文章

Python - 執行且客製HTMLTestRunner的report

圖片
$ wget -P /usr/lib/python2.7 http://tungwaiyip.info/software/HTMLTestRunner_0_8_2/HTMLTestRunner.py # 編輯執行unittest 的py檔 # 先宣告產生的html檔案存放路徑 tests = ['test_case_1', 'test_case_2' ] suite = unittest.TestSuite(map(BackUpTest,tests)) testfile = "/tmp/test.html" fp = file(testfile,"wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp) runner.run(suite) 我們用HTMLTestRunner(0.8.2)來產生unittest report 預設輸出的畫面如下   今天我們想在每個test case加上 自己的docstring該如何做呢? 最終畫面如下 # HTMLTestRunner.py # 第416行開始就是產生report的html語法 # 第一步 在header_row先加入一個"Description"的欄位 # 接著在"Total"那一列需加入一個空白欄位 # 如果是想顯示Class Unittest Module的docstring,需再產生class report template的腳本先新增一個%(docstring)s的變數欄位 # 這邊的 %( docstring )s 可以自行定義 # 如果是想顯示每個unitest function的docstring需在產生unittest report template的腳本新增一個 %(docstring)s的變數欄位 # 因為產生unittest report的有分有output及沒有output,所以兩個地方要改 # 一樣這邊 的 %( docstring )s 可以自行定義 # 我們這邊就要在_generate_...

Python - unittest

參考來源 : 使用PYTHON的UNITTEST做测试 python 的 unittest 单元测试使用详解 Python unittest 模組 Part 1 利用 Coverage 計算 Python 程式碼的涵蓋率 Python 單元測試(Unit Testing) [Python]如何使用HtmlTestRunner让自动化测试报告内容更丰富 04、生成 HTMLTestRunner 测试报告 unittest colored output

Python - bytes reversed ABCDEF -> EFCDAB

#!/usr/bin/python #-*- coding:utf-8 -*- ''' @author: Duncan ''' # option1 Byte = 'ABCDEF' print "".join(reversed([Byte[i:i+2] for i in range(0, len(Byte), 2)])) # option2 temp_list = [] for i in range(0, len(Byte), 2): temp_list.append(Byte[i:i+2]) print "".join(reversed(temp_list)) 參考來源 :  Reverse a string in Python two characters at a time (Network byte order)

Python - 計算特定目錄底下的檔案以及目錄數量

$ vim countFileandFolder.py import os import sys fileList = [] fileSize = 0 folderCount = 0 rootdir = '/usr/lib' for root, subFolders, files in os.walk(rootdir): folderCount += len(subFolders) for file in files: f = os.path.join(root,file) fileSize = fileSize + os.path.getsize(f) #print(f) fileList.append(f) print("Total Size is {0} bytes".format(fileSize)) print(“Total Files “, len(fileList)) print(“Total Folders “, folderCount) 參考來源 :   recursive list files in a dir using Python

Python - multithread

#!/usr/bin/python #-*- coding:utf-8 -*- ''' @author: Duncan ''' from Queue import Queue from threading import Thread class ThreadWorker(Thread): def __init__(self, name, tasks): Thread.__init__(self) self.tasks = tasks # tasks queue self.daemon = True # 需在start之前,具有和main thread一同終止的特性,預設是False self.start() self.name = name # 可以分別設定thread的name   def run(self): while True: func, args, kargs = self.tasks.get() # 從tasks queue取出task   try: func(*args, **kargs) # 執行function except Exception as e: print e self.tasks.task_done() class ThreadPoolManager: def __init__(self, number_threads):   self.tasks = Queue( ) # 建立沒有限制長度的queue for _ in range( number_threads ): ThreadWorker(_,self.tasks) # 啟動根據 num_threads數量的task任務 def add_task(self, func, *args, **ka...