python3 + request Interface Automation Framework

For the first time writing blog, written record automation interface framework, the framework is relatively simple, ha ha ha, the course of the next record to be! ~ ~ ~

A, this frame written by python3.6

  1, to prepare code environment, download python3.6 Download: https://www.python.org/ Downloads

  2, download pycharm software.

Second, begin to create python Interface Automation Framework:

  1, this I frame is created each file in the folder, the folder configuration config respectively, fengzhuang interface will get post encapsulation of two transmission modes and automatically performed to distinguish.

  2, logfile used to store the test case execution, save log log.

  3, public_common used to store some of the common methods, such as asserted log for use in the embodiment excel table.

  4, result after running the test is used to store generated html reports.

  5, test_case test used to store the code.

  6, test_case_data used to store test excel test cases.

  7, the most important script entry.

Third, the folder code starts filling:

  1, config folder is created Config_file.py file. I Config_file file should be written only in a failure to limit the number of retries, not shot shows, ha ha like to write their own

  2, create the logs folder logtest folder (logtest means has written a log file is not a good feeling, and from a new written test to be named experiment has been also did not change, ha ha)

    2.1, ah create a log code to log acquisition and storage. When there is no log file storage folder is automatically created a logfile

   3, after the log has been created successfully, change the parameters of the interface GET POST begin transmission with a code that will be written.

    3.1:get与post 分别创建了两个函数因为post与get的代码差不多  就不对post进行截图了,此代码是对接口进行请求并将返回的json数据进行转换并对代码进行容错(容错位置可能有问题,如果没成功,可以下方评论联系我!)

  4、请求参数代码创建完成后,对数据可以进行断言来判断返回的数据是否准确喽:

    4.1、我在上方先导入了一个log文件,一个图没截下来,再次说明下哈,

因为我写的是PHP的接口返回,我的接口返回的数据主要判断下接口里data list  中是否有数据  ,现在判断的比较简单,断言的name主要是用在case运行时带一个用例名称好进行纪录。

  5、现在数据传参、断言下面 对case中返回的接口传输方式get、post  进行一个封装,使其自动进行判断 啦啦啦 !

    5.1、上代码

两个图截的有点low了 ,再次说一下哈,getJson这个函数是一个,只是将返回的进行一个转换后在返回给调用的,也可以自己在进行优化的,这段代码主要时对excel中的用例里面,在excel中单独放了一个字段  fangshi   对这字段来进行一个检查,如果时post则会进行判断自动调用post方法  ge't相同方式。

  6、好了到这里一些,来进行一项重要的,读取excel中用例。

    6.1、上代码

先导入xlrd 模块,先创建一个空list,因为我将case的数据都写在了sheet1页面中了,所以直接就写死了,这里需要的可以自己写一下,这段代码里进行按照行进行了循环查找并将每一行传唤成一个dict形式,并添加带list中最后返回list。

  7、获取excel文件的方式也写完了,开始写case的框架来进行运行了

    7.1、上代码 

这段代码中有一个注释掉的,时进行测试的修改错误的,可以忽略掉。这里主要我将excel文件夹的路径直接写到了cese中,因为在excel中导出的数据时list形式,我将其进行一个循环来拿出数据进行,毕竟excel中会有很多的case来进行测试。最后对代码进行断言,在断言代码中的name字段在这里用到,用来获取excel中case中的名称,在生成log时会有一个标识,好进行跟踪。当然在这里我进行了错误错误代码,运行失败后,限制重试的次数,用到的就是Config文件中限制的次数。好了到这里接口自动化测试框架已经完成了很大的一部分了,下面就开始出现运行入口。(我这里用的UNITEST 框架,因为HTML报告没有自己写,用来一个省事的哈哈。以后会修改的。)

  8、框架运行的入口:

    8.1、上代码:

这里为框架运行的入口代码,里面有一个发邮件的功能,只是我还没有写成功,能发邮件 但是附件没带成功,sendmail文件我就没在这次书写上 ,后面的文章中,我会加上的。

四、以上为我写的接口自动化框架的基本代码,书写的比较简陋,可能规范也欠缺,哈哈  不过纪录下历程和新手学习下还算过的去

五、html测试报告样式

  5.1样式比较简单,哈哈  后续会优化的。

六、欢迎各位进行查看并指出错误。哈哈   代码借鉴了  雷子的博客。

Guess you like

Origin www.cnblogs.com/Jaredhan/p/10967058.html
Recommended