#!/usr/bin/env python
#coding:utf-8
class calculator:
def __init__(self, a, b):
self.a=int(a)
self.b=int(b)
def myadd(self):
return self.a+self.b
def mysubs(self):
return self.a-self.b
def mymultiply(self):
return self.a*self.b
def mydivide(self):
try:
return self.a/self.b
except ZeroDivisionError:
print ("除數不能爲零")
return 9999999999999999
|
CalculatorTest.py
#!/usr/bin/env python
#coding:utf-8
import unittest
from Calculator import calculator
class calculatortest(unittest.TestCase):
def setUp(self):
print ("Test start!")
def test_base(self):
j=calculator(4,2)
self.assertEqual(j.myadd(),6)
self.assertEqual(j.mysubs(),2)
self.assertEqual(j.mymultiply(),8)
self.assertEqual(j.mydivide(),2)
def test_divide(self):
j=calculator(4,0)
self.assertEqual(j.mydivide(),9999999999999999)
def tearDown(self):
print ("Test end!")
if __name__=='__main__':
#構造測試集
suite=unittest.TestSuite()
suite.addTest(calculatortest("test_base"))
suite.addTest(calculatortest("test_divide"))
#運行測試集合
runner=unittest.TextTestRunner()
runner.run(suite)
|
94行
import StringIO
改成
import io
539行
self.outputBuffer = StringIO.StringIO()
改成
self.outputBuffer = io.StringIO()
631行
print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)
改成
print (sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
642行
if not rmap.has_key(cls):
改成
if not cls in rmap:
766行
uo = o.decode('latin-1')
改成
uo = o
772行
ue = e.decode('latin-1')改成
ue = e
|
runtest.py
#!/usr/bin/env python
#coding:utf-8
import unittest
from HTMLTestRunner import HTMLTestRunner
test_dir='./'
discover=unittest.defaultTestLoader.discover(test_dir,pattern="*Test.py")
if __name__=='__main__':
runner=unittest.TextTestRunner()
#如下用於生成測試報告
fp=open("result.html","wb")
runner =HTMLTestRunner(stream=fp,title='測試報告',description='測試用例執行報告')
runner.run(discover)
fp.close()
|
testLogin.py
import requests
#正確的用戶名,錯誤的密碼
url=「http://127.0.0.1:8000/login_action/
payload={{"username":"jerry","password":「000000"}}
data = requests.post(url,data=payload)
if (str(data.status_code)==‘200’) and (「用戶名或者密碼錯誤」 in str(data.text))
print(「pass」)
else:
print(「Fail」)
#錯誤的用戶名,正確的密碼
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「tom","password":「123456"}}
data = requests.post(url,data=payload)
if (str(data.status_code)==‘200’) and (「用戶名或者密碼錯誤」 in str(data.text))
print(「pass」)
else:
print(「Fail」)
#錯誤的用戶名,錯誤的密碼
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「tom","password":「000000"}}
data = requests.post(url,data=payload)
if (str(data.status_code)==‘200’) and (「用戶名或者密碼錯誤」 in str(data.text))
print(「pass」)
else:
print(「Fail」)
#正確的用戶名,正確的密碼
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「jerry","password":「123456"}}
data = requests.post(url,data=payload)
if (str(data.status_code)==‘200’) and (「查看購物車」 in str(data.text))
print(「pass」)
else:
print(「Fail」)
|
testLogin.py
import unittest,requests
class mylogin(unittest.TestCase):
def setUp(self):
print("--------測試開始--------")
def test_login_1:
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「tom","password":「000000"}}
data = requests.post(url,data=payload)
self.assertEqual(‘200’,str(data.status_code))
self.assertIn((「用戶名或者密碼錯誤」,str(data.text))
def test_login_2:
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「jerry","password":「123456"}}
data = requests.post(url,data=payload)
self.assertEqual(‘200’,str(data.status_code))
self.assertIn((「用戶名或者密碼錯誤」,str(data.text))
def test_login_3:
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「tom","password":「000000"}}
data = requests.post(url,data=payload)
self.assertEqual(‘200’,str(data.status_code))
self.assertIn((「用戶名或者密碼錯誤」,str(data.text))
def test_login_4:
url=「http://127.0.0.1:8000/login_action/
payload={{"username":「jerry","password":「000000"}}
data = requests.post(url,data=payload)
self.assertEqual(‘200’,str(data.status_code))
self.assertIn((「查看購物車」,str(data.text))
def tearDown(self):
print("--------測試結束--------")
if __name__=='__main__':
#構造測試集
suite=unittest.TestSuite()
suite.addTest(mylogin(" test_login_1 "))
suite.addTest(mylogin(" test_login_2 "))
suite.addTest(mylogin(" test_login_3 "))
suite.addTest(mylogin(" test_login_4 "))
#運行測試集合
runner=unittest.TextTestRunner()
runner.run(suite)
|
loginConfig.xml
<node>
<case>
<TestId>testcase001</TestId>
<Title>用戶登陸</Title>
<Method>post</Method>
<Desc>正確用戶名,錯誤密碼</Desc>
<Url>http://127.0.0.1:8000/login_action/</Url>
<InptArg>{"username":"jerry","password":"12345"}</InptArg>
<Result>200</Result>
<CheckWord>用戶名或者密碼錯誤</CheckWord>
</case>
<case>
<TestId>testcase002</TestId>
<Title>用戶登陸</Title>
<Method>post</Method>
<Desc>錯誤用戶名,正確密碼</Desc>
<Url>http://127.0.0.1:8000/login_action/</Url>
<InptArg>{"username":"smith","password":"knyzh158"}</InptArg>
<Result>200</Result>
<CheckWord>用戶名或者密碼錯誤</CheckWord>
</case>
<case>
<TestId>testcase003</TestId>
<Title>用戶登陸</Title>
<Method>post</Method>
<Desc>錯誤用戶名,錯誤密碼</Desc>
<Url>http://127.0.0.1:8000/login_action/</Url>
<InptArg>{"username":"smith","password":"12345"}</InptArg>
<Result>200</Result>
<CheckWord>用戶名或者密碼錯誤</CheckWord>
</case>
<case>
<TestId>testcase004</TestId>
<Title>用戶登陸</Title>
<Method>post</Method>
<Desc>正確用戶名,正確密碼</Desc>
<Url>http://127.0.0.1:8000/login_action/</Url>
<InptArg>{"username":"jerry","password":"knyzh158"}</InptArg>
<Result>200</Result>
<CheckWord>查看購物車</CheckWord>
</case>
</node>
|
<Title> 、<Method>…</Method> …,因此咱們能夠這樣來得到。
aaa = root.getElementsByTagName('AAA')
bbb = root.getElementsByTagName('BBB')
ccc = root.getElementsByTagName('CCC')
i = 0
for keyin AAA:
aaaValue = aaa[i].firstChild.data
bbbValue = bbb[i].firstChild.data
cccValue = ccc[i].firstChild.data
print(aaaValue)
print(bbbValue)
print(cccValue)
i =i+1
|
loginConfig.xml
#!/usr/bin/env python
#coding:utf-8
import unittest,requests
from xml.dom import minidom
class mylogin(unittest.TestCase):
def setUp(self):
print("--------測試結束--------")
#從XML中讀取數據
dom = minidom.parse('loginConfig.xml')
root = dom.documentElement
TestIds = root.getElementsByTagName('TestId')
Titles = root.getElementsByTagName('Title')
Methods = root.getElementsByTagName('Method')
Descs = root.getElementsByTagName('Desc')
Urls = root.getElementsByTagName('Url')
InptArgs = root.getElementsByTagName('InptArg')
Results = root.getElementsByTagName('Result')
CheckWords =root.getElementsByTagName('CheckWord')
i = 0
mylists=[]
for TestId in TestIds:
mydicts={}
#獲取每個數據,造成字典
mydicts["TestId"] = TestIds[i].firstChild.data
mydicts["Title"] = Titles[i].firstChild.data
mydicts["Method"] = Methods[i].firstChild.data
mydicts["Desc"] = Descs[i].firstChild.data
mydicts["Url"] = Urls[i].firstChild.data
mydicts["InptArg"] = InptArgs[i].firstChild.data
mydicts["Result"] = Results[i].firstChild.data
mydicts["CheckWord"] =CheckWords[i].firstChild.data
mylists.append(mydicts)
i = i+1
self.mylists = mylists
def test_login(self):
for mylist in self.mylists:
payload = eval(mylist["InptArg"])
url=mylist["Url"]
#發送請求
try:
if mylist["Method"] == "post":
data = requests.post(url,data=payload)
elif mylist["Method"] == "get":
data = requests.get(url,params=payload)
else:
print ("Method 參數獲取錯誤")
except Exception as e:
self.assertEqual(mylist["Result"],"404")
else:
self.assertEqual(mylist["Result"],str(data.status_code))
self.assertIn(mylist["CheckWord"],str(data.text))
def tearDown(self):
print("--------測試結束--------")
if __name__=='__main__':
#構造測試集
suite=unittest.TestSuite()
suite.addTest(mylogin("test_login"))
#運行測試集合
runner=unittest.TextTestRunner()
runner.run(suite)
|
getXML.py
#!/usr/bin/env python
#coding:utf-8
from xml.dom import minidom
class GetXML():
def getxmldata(xmlfile):
#從XML中讀取數據
dom = minidom.parse(xmlfile)
root = dom.documentElement
TestIds = root.getElementsByTagName('TestId')
Titles = root.getElementsByTagName('Title')
Methods = root.getElementsByTagName('Method')
Descs = root.getElementsByTagName('Desc')
Urls = root.getElementsByTagName('Url')
InptArgs = root.getElementsByTagName('InptArg')
Results = root.getElementsByTagName('Result')
CheckWords =root.getElementsByTagName('CheckWord')
i = 0
mylists=[]
for TestId in TestIds:
mydicts={}
#獲取每個數據,造成字典
mydicts["TestId"] = TestIds[i].firstChild.data
mydicts["Title"] = Titles[i].firstChild.data
mydicts["Method"] = Methods[i].firstChild.data
mydicts["Desc"] = Descs[i].firstChild.data
mydicts["Url"] = Urls[i].firstChild.data
mydicts["InptArg"] = InptArgs[i].firstChild.data
mydicts["Result"] = Results[i].firstChild.data
mydicts["CheckWord"] =CheckWords[i].firstChild.data
mylists.append(mydicts)
i = i+1
return mylists
|
loginConfig.xml
…
from getXML import GetXML #引入剛纔創建的類
…
class mylogin(unittest.TestCase):
def setUp(self):
print("--------測試開始--------")
self.mylists = GetXML.getxmldata("loginConfig.xml")#調用類中的函數
…
|