python+selenium自动化测试

web自动化测试

1 automatic_test目录下目录结构:

  • cases
    • login_page.py
    • report.py
    • result.html(HTMLTestRunner测试报告)
  • pages
    • login_page.py
  • locators
    • login_page.py
  • conftest.py
  • pyreport.html(pytest测试报告)

2 使用HTMLTestRunner生成测试报告

HTMLTestRunner模块无法通过pip安装。
想要安装首先在终端中查找路径,命令如下:

python
>>>import sys
>>>sys.path

安装到mac对应目录下(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages),安装时将HTMLTestRunner文件夹放在以上目录下,文件夹中放两个文件,一个是 HTMLTestRunner.py(注意python2与python3版本不同),另一个是__init__.py 空文件,这样python就会将HTMLTestRunner作为一个模块导入。

附上report.py代码:

#coding=utf-8
import unittest
from login_page import LoginCRM   #导入测试用例
from HTMLTestRunner import HTMLTestRunner

testunit = unittest.TestSuite()
testunit.addTest(LoginCRM('test_login_crm'))   #添加测试用例
fp = open('./result.html','wb')
runner = HTMLTestRunner.HTMLTestRunner(stream = fp, title = u'测试报告', description = u'用例执行情况:')
runner.run(testunit)

如果HTMLTestRunner没有导入正确会报错,python2、python3都有对应的HTMLTestRunner.py 版本,版本不对也会报错,小问题注意一下。

然后运行report.py文件,结束后打开result.html就可以看到网页版的测试报告拉~


3 使用pytest生成测试报告

首先在iterm2中安装pytest-html,pip install pytest-html
编辑好conftest.py文件后,输入命令

py.test ../cases/login_page.py --html=../pyreport.html

生成的报告保存在pyreport.html 文件中(pytest的报告看起来比HTMLTestRunner的友好-_-)

运行中遇到的一些问题:
编码报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 4: ordinal not in range(128)
解决办法是,在文件开头导入sys模块:

import sys
reload(sys)
sys.setdefaultencoding('utf8')

解决~


tips:
在.py文件开始加入以下代码定位到上一级目录:

import sys
sys.path.append('../')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值