python自动化框架pycharm_pycharm+python+selenium+pytest框架及问题总结

nvs\\pytest\\lib\\site-packages\\allure\\pytest_plugin.py'>

解决策略:https://www.cnblogs.com/lsdb/p/10430031.html

5、 如何执行pytest:

(1) 在pycharm中:点击terminal,然后输入pytest 测试用例.py (注意:测试用例的文件名一定要带:test_*.py或者*_test.py)

(2) 或者在dos命令行,cd到pytest安装目录,执行:pytest E:\pycharm\run\Netone\test_first.py

结果描述:

test_second.py .F

表示:这个文件中一个方法成功(.),一个方法执行失败(F)  (E)表示报错error

备注:上述方法是对于一个py类文件中的两个方法的测试,也可以对一个工程下的所有类进行测试,如:

一个Netone工程下,有两个py类文件,执行:pytest E:\\pycharm\\run\\Netone,根据pytest执行规则,会默认搜寻所有文件名为test_*.py或者*_test.py的文件。

6、 pytest执行规则https://blog.csdn.net/dawei_yang000000/article/details/93380552:

pytest搜索测试文件和测试用例的过程称为测试搜索,只要遵循如下几条原则便能够被它搜索到:

测试文件应命名为test_(something).py或者(something)_test.py

测试函数、测试类方法应命名为test_(something)

测试类应命名为Test(something)

7、 各种断言的处理方式:

https://blog.csdn.net/liuchunming033/article/details/46504187

8、如何生成测试用例报告:

(1)pytest自带的插件

pycharm:

https://blog.csdn.net/weixin_34362875/article/details/93794459

pytest.main(['-m smoke',

'--result-log=report/test_first.log',

'--junit-xml=report/test_first.xml',

'--html=report/test_first.html'])

命令行:

https://www.jianshu.com/p/8fa34a3c82bd

在dos命令行:

E:\pycharm\run\Netone\Data>pytest data.py --html=E:\pycharm\run\Netone\report\pc

s\pcs_report_html.html

(2) allure

a、安装步骤参考:http://www.51ste.com/share/det-862.html

b、在pycharm中打开terminal,执行:pytest E:\\pycharm\\run\\Netone\\test_second.py --alluredir E:\pycharm\run\Netone\report\test_allure

c、执行完毕后,会自动在路径下E:\pycharm\run\Netone\report\test_allure生成一个文件夹test_allure,文件夹中会有两个json文件(因为我的test_second.py中定义了两个方法且相互不调用,

因此测试test_second.py,就会生成两个json),这个json文件只是测试的结果而不是生成的报告。

d、dos命令行:cd到allure的安装目录bin目录

执行:

allure generate E:\pycharm\run\Netone\report\test_allure -o E:\pycharm\run\Netone\report\test_allure_report –clean

将test_allure文件夹中的json文件转成test_allure_report中可以用浏览器打开的html文件

e、在pycharm中打开html:右击 ---- open in Brower即可。

或者:cmd中,cd到工作目录:执行:allure open -h 127.0.0.1 -p 8083 E:\pycharm\run\Netone\report\pcs\pcs_html_report

9、 执行报错:AttributeError: module 'allure' has no attribute 'severity_level'

解决策略:

pip uninstall pytest-allure-adaptor

pip install allure-pytest

10、pytest的8个强大的插件:

https://www.sohu.com/a/239476958_100159565

11、如何获取到定位元素中的text文本内容:

直接通过:reponse = driver.find_elements_by_xpath(' //*[@id="iform"]/table/tbody/tr[2]/td/table/tbody/tr[67]/td/table/tbody/tr[2]/td[2]/div/text()')  是无法获取到text内容的

需要:先要获取到上一层数据,然后text获取:

步骤1:reponse = driver.find_elements_by_xpath(' //*[@id="iform"]/table/tbody/tr[2]/td/table/tbody/tr[67]/td/table/tbody/tr[2]/td[2]/div‘)

步骤2:text_element = reponse.text

12、 判断定位到的元素是否存在,不可用if来判断, 需要封装一个函数,来判断元素是否存在

# def isElementPresent(self, by, value): #     """ #     用来判断元素标签是否存在, #     """ #     try: #         element = self.driver.find_element(by=by, value="xpath") #     # 原文是except NoSuchElementException, e: #     except NoSuchElementException as e: #         # 发生了NoSuchElementException异常,说明页面中未找到该元素,返回False #         return False #     else: #         # 没有发生异常,表示在页面中找到了该元素,返回True #         return True

13、 py文件存在跨目录的import,执行pytest报错:

解决策略:

https://blog.csdn.net/fanzirong_/article/details/92577830

在执行文件中添加:

import sys, os

sys.path.append((os.path.abspath(os.path.join(os.path.dirname("E:\\pycharm\\run\\Netone\\page_object\\pcs"), '../'))))

sys.path.append((os.path.abspath(os.path.join(os.path.dirname("E:\\pycharm\\run\\Netone\\common"), '../'))))

# 路径最好写成绝对路径

转存失败重新上传取消

14、执行过程中,报错:

from Netone.testCase.test_pcs.test_pcs import test_open_Netone

E ModuleNotFoundError: No module named 'Netone'

解决策略:在执行文件最前端加入代码:

import sys, os

sys.path.append((os.path.abspath(os.path.join(os.path.dirname("E:\\pycharm\\run\\Netone\\page_object\\pcs"), '../'))))

sys.path.append((os.path.abspath(os.path.join(os.path.dirname("E:\\pycharm\\run\\Netone\\common"), '../'))))

sys.path.append((os.path.abspath(os.path.join(os.path.dirname("E:\\pycharm\\run\\Netone\\testCase\\test_pcs"), '../'))))

详解:https://www.cnblogs.com/liuyanhang/p/11018407.html

转存失败重新上传取消

15、数据分离:

方法1:函数传参的方式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值