iis7.5 没有默认网站,wordpress wpml,seo群发软件,主机托管服务这两天一直在找直接用python做接口自动化的方法#xff0c;在网上也搜了一些博客参考#xff0c;今天自己动手试了一下。
一、整体结构 上图是项目的目录结构#xff0c;下面主要介绍下每个目录的作用。
Common:公共方法:主要放置公共的操作的类#xff0c;比如数据库sql… 这两天一直在找直接用python做接口自动化的方法在网上也搜了一些博客参考今天自己动手试了一下。
一、整体结构 上图是项目的目录结构下面主要介绍下每个目录的作用。
Common:公共方法:主要放置公共的操作的类比如数据库sqlhelper、文件操作类等
Config:公共变量:主要放置公共变量,比如ST、UAT、生产环境的url地址、用户名密码、数据库连接
Data:数据层,有点类似三层架构中的DAL,它是数据的来源根据数据存放的格式再细分json、xml、表单和数据库
Log:日志层:存放日志便于跟踪调试
Page:页面层:先把整个系统划分若干子系统每个子系统包含若干页面。这个把用户操作的页面抽象成了page对象页面的操作抽象成方法这样测试人员可以传递不同的测试案例进行测试如果是面向服务的纯接口性质的没有页面那就没必要再这样划分,这样就把接口测试转换成了python的单元测试。
Result:存放单元测试的执行结果也可以把每次执行的结果存到数据库打点然后做测试结果趋势分析,如果后续把项目集成到Jenkins中的话相当于Jenkins集成python单元测试这样的话这层也可以不需要。
Case:测试案例层针对上面Page对应的单个方法利用测试数据和期望数据进行assert判断这里用到的测试数据和期望数据后续可以放在Excel中测试人员只需填充测试数据。
Run:这里用来组装成suite然后进行运行案例。
二、测试
1.安装HTMLTestRunner
把它下载下来放到python安装目录的lib目录下
2.业务逻辑层
这里模拟一些业务处理这里做接口自动化时会使用requests库进行请求。
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding(utf-8)
import requestsdef Add(name,pwd):sessionrequests.session()responsesession.get(http://www.baidu.com)print(response.status_code)return response.status_code200def Edit(name,pwd):return {name:name,pwd:pwd}def Delete(name,pwd):return {name:name,pwd:pwd}def Search(name,pwd):return {name:name,pwd:pwd}3.案例层
原本计划增加一个套件suite层如果是单个接口的不加也可以如果是多个接口进行流程测试使用suite时案例的顺序就不会改变。如果是流程的也可以写成case只是里面需要多次调用业务逻辑层。
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding(utf-8)
import unittest
from Root.Page import Login
from Root.Page.UserManager import Index
import HTMLTestRunner
import time
class index(unittest.TestCase):def setUp(self):print(setUp)def tearDown(self):print(tearDown)def test_add(self):arr Login.Login(admin, 123456)flag Index.Add(arr[0], arr[1])self.assertTrue(flag)flag Index.Add(arr[0], arr[1])self.assertTrue(flagFalse)def test_edit(self):response Login.Login(admin, 123456)dic Index.Edit(response[0], response[1])self.assertNotEqual(dic,{name:123})def test_delete(self):response Login.Login(admin, 123456)dic Index.Delete(response[0], response[1])self.assertNotEqual(dic,{name:123})4.运行
这里主要考虑可能整个系统会分成不同的模块进行运行这样也能维护上也必将方便可以多执行机执行。这里使用的HTMLTestRunner来生成报告.
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding(utf-8)
import os
import unittest
from HTMLTestRunner import HTMLTestRunner
from Root.Test.Case.UserManager import Index
import HTMLTestRunner
import time
if __name__ __main__:# 1、构造用例集suite unittest.TestSuite()# 2、执行顺序是安加载顺序先执行test_sub再执行test_addsuite.addTest(Index.index(test_add))suite.addTest(Index.index(test_edit))suite.addTest(Index.index(test_delete))suite.addTest(Index.index(test_edit))suite.addTest(Index.index(test_edit))filename ../../../Result/{0}Report.html.format(time.strftime(%Y%m%d%H%M%S, time.localtime()) ) # 定义个报告存放路径支持相对路径f file(filename, wb) # 结果写入HTML 文件runner HTMLTestRunner.HTMLTestRunner(streamf, title测试报告, descriptionXXX系统接口自动化测试测试报告,verbosity2) # 使用HTMLTestRunner配置参数输出报告路径、报告标题、描述runner.run(suite)三、测试案例参数化
上面的每个单元测试只能运行一个测试案例的数据就是如何实现参数化这样配置一下案例数据就能运行多次单元测试这样就会方便很多。找了下python自带的单元测试框架不支持这里使用了nose和parameterized 。
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding(utf-8)
from nose.tools import assert_equal
from parameterized import parameterized
import HTMLTestRunner
import time
import unittest
import mathparameterized([(2, 2, 4),(2, 3, 8),(1, 9, 1),(0, 9, 0),
])
def test_pow(base, exponent, expected):assert_equal(math.pow(base, exponent), expected)class TestMathUnitTest(unittest.TestCase):parameterized.expand([(negative, -1.5, -2.0),(integer, 1, 1.0),(large fraction, 1.6, 1),])def test_floor(self, name, input, expected):assert_equal(math.floor(input), expected) 然后cmd跳转到该python文件的目录下输入命令,它会把该文件中test开头的案例都跑了,然后就可以看到有一个案例运行输出结果的html文件.
nosetests testRuncase.py --with-html --html-reportnose_report2_test.html 最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你
文章转载自: http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.blxor.com.gov.cn.blxor.com http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.kdnrc.cn.gov.cn.kdnrc.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.sjwiki.com.gov.cn.sjwiki.com http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn