标签:SM eth getx .text 一段 set 情况 正则 提示
#!/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")#调用类中的函数
…
|
基于Python Requests的数据驱动的HTTP接口测试
标签:SM eth getx .text 一段 set 情况 正则 提示
原文地址:https://www.cnblogs.com/peter200-OK/p/9070032.html