導航:首頁 > 編程語言 > pythontestrunner

pythontestrunner

發布時間:2024-01-15 00:05:19

A. 如何使用python編寫測試腳本

1)doctest
使用doctest是一種類似於命令行嘗試的方式,用法很簡單,如下

復制代碼代碼如下:

def f(n):
"""
>>> f(1)
1
>>> f(2)
2
"""
print(n)

if __name__ == '__main__':
import doctest
doctest.testmod()

應該來說是足夠簡單了,另外還有一種方式doctest.testfile(filename),就是把命令行的方式放在文件里進行測試。

2)unittest
unittest歷史悠久,最早可以追溯到上世紀七八十年代了,C++,Java里也都有類似的實現,Python里的實現很簡單。
unittest在python里主要的實現方式是TestCase,TestSuite。用法還是例子起步。

復制代碼代碼如下:

from widget import Widget
import unittest
# 執行測試的類
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget()
def tearDown(self):
self.widget.dispose()
self.widget = None
def testSize(self):
self.assertEqual(self.widget.getSize(), (40, 40))
def testResize(self):
self.widget.resize(100, 100)
self.assertEqual(self.widget.getSize(), (100, 100))
# 測試
if __name__ == "__main__":
# 構造測試集
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase("testSize"))
suite.addTest(WidgetTestCase("testResize"))

# 執行測試
runner = unittest.TextTestRunner()
runner.run(suite)

簡單的說,1>構造TestCase(測試用例),其中的setup和teardown負責預處理和善後工作。2>構造測試集,添加用例3>執行測試需要說明的是測試方法,在Python中有N多測試函數,主要的有:
TestCase.assert_(expr[, msg])
TestCase.failUnless(expr[, msg])
TestCase.assertTrue(expr[, msg])
TestCase.assertEqual(first, second[, msg])
TestCase.failUnlessEqual(first, second[, msg])
TestCase.assertNotEqual(first, second[, msg])
TestCase.failIfEqual(first, second[, msg])
TestCase.assertAlmostEqual(first, second[, places[, msg]])
TestCase.failUnlessAlmostEqual(first, second[, places[, msg]])
TestCase.assertNotAlmostEqual(first, second[, places[, msg]])
TestCase.failIfAlmostEqual(first, second[, places[, msg]])
TestCase.assertRaises(exception, callable, ...)
TestCase.failUnlessRaises(exception, callable, ...)
TestCase.failIf(expr[, msg])
TestCase.assertFalse(expr[, msg])
TestCase.fail([msg])

B. python用例並發怎麼解決

python-selenium並發執行測試用例(方法一 各模塊每一條並發執行)

總執行代碼:
# coding=utf-8
import unittest,os,time
import HTMLTestRunner
import threading
import sys
sys.path.append('C:/Users/Dell/Desktop/CARE/program')#使用編輯器,要指定當前目錄,不然無法執行第20行代碼

def creatsuite():
casedir = []
list = os.listdir(os.path.dirname(os.getcwd()))#獲取當前路徑的上一級目錄的所有文件夾,這里可以改成絕對路徑(要搜索的文件路徑)
for xx in list:
if "" in xx:
casedir.append(xx)
suite =[]
for n in casedir:
testunit = unittest.TestSuite()
unittest.defaultTestLoader._top_level_dir = None
#(unittest.defaultTestLoader(): defaultTestLoader()類,通過該類下面的discover()方法可自動更具測試目錄start_dir匹配查找測試用例文件(test*.py),
並將查找到的測試用例組裝到測試套件,因此可以直接通過run()方法執行discover)
discover = unittest.defaultTestLoader.discover(str(n),pattern='tet_*.py',top_level_dir=None)
for test_suite in discover:
for test_case in test_suite:
testunit.addTests(test_case)
suite.append(testunit)
return suite, casedir
def runcase(suite,casedir):
lastPath = os.path.dirname(os.getcwd())#獲取當前路徑的上一級
resultDir = lastPath+"\\run\\report\\" #報告存放路徑
now = time.strftime("%Y-%m-%d %H.%M.%S",time.localtime())
filename = resultDir + now +" result.html"
fp = file(filename, 'wb')
proclist=[]
s=0
for i in suite:
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=str(casedir[s])+u'測試報告',description=u'用例執行情況:')
proc = threading.Thread(target=runner.run,args=(i,))
proclist.append(proc)
s=s+1
for proc in proclist:
proc.start()
for proc in proclist:
proc.join()
fp.close()
if __name__ == "__main__":
runtmp=creatsuite()
runcase(runtmp[0],runtmp[1])

閱讀全文

與pythontestrunner相關的資料

熱點內容
怎麼查伺服器假死原因日誌在哪看 瀏覽:277
掃描pdf文件 瀏覽:926
解壓密碼百度雲在線解壓 瀏覽:767
傳播學演算法推薦 瀏覽:749
我的世界網路游戲如何查找伺服器 瀏覽:257
安卓和蘋果通訊錄怎麼互傳 瀏覽:203
怎麼打開隱私與應用加密的菜單 瀏覽:416
我的世界伺服器小游戲的地址大全 瀏覽:578
在網路安全中加密安全機制提供了數據的 瀏覽:249
南京前端程序員私活怎麼收費 瀏覽:981
拓撲pdf 瀏覽:440
如何在工行app查我的訂單 瀏覽:214
車壓縮機改電動 瀏覽:83
如何尋找音樂app 瀏覽:831
一加加密的照片 瀏覽:200
阿里雲虛擬主機php 瀏覽:639
不卡點的解壓視頻 瀏覽:391
hex文件下載單片機 瀏覽:873
實現編譯器的自展技術 瀏覽:655
app開發者怎麼突破 瀏覽:418