pythonweb测试_python的web自动化测试

python+web自动化

第一部分:

安装:python,PyCharm编辑器,selemium

官网下载python最新版本

https://www.python.org/downloads/release

https://www.jetbrains.com/pycharm/download/#section=windows

pip install -U selenium

查看安装版本号

python -V

pycharm设置:

https://www.jetbrains.com/help/pycharm/type-hinting-in-product.html

https://blog.csdn.net/xiao_xian_/article/details/88181845?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf

pycharm设置autopep8

pip install autopep8

File->Setting->Tools->External Tools->autopep8

Name: AutoPEP8

Program: D:\Python\Python38-32\Scripts\autopep8.exe

Arguments: --in-place --aggressive --aggressive $FilePath$

Working directory: $ProjectFileDir$

Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*

pycharm设置智能提示:“Power Save Mode”勾选取消

快捷键:

Ctrl + / 行注释/取消行注释

Ctrl + Alt + L 代码格式化

Ctrl + D 复制选定的区域或行

Ctrl + Y 删除选定的行

Ctrl + Numpad+/- 展开/折叠代码块(当前位置的:函数,注释等)

Ctrl + shift + Numpad+/- 展开/折叠所有代码块

代码参考

https://www.cnblogs.com/tepy/p/10786430.html

安装三大浏览器驱动driver

1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list

2.Firefox的驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases/

3.IE的驱动IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver/

注意:下载解压后,将chromedriver.exe , geckodriver.exe , Iedriver.exe发到Python的安装目录,例如 D:\python 。

然后再将Python的安装目录添加到系统环境变量的Path下面。

例子:

from selenium import webdriver

from selenium.webdriver.remote.webelement import WebElement

driver = webdriver.Firefox()

driver.get("http://www.baidu.com")

# 隐式等待,设定时间内等待元素出现

driver.implicitly_wait(30)

driver.find_element_by_id("kw").send_keys("pycharm")

driver.find_element_by_id("su").click()

第二部分:

pytest+selenium自动化测试框架

视频地址

https://edu.csdn.net/course/play/27202/357913

pytest和unittest框架对比

pip install pytest

特点:可集成ci环境jenkins,兼容性好,生成html测试报告

pytest可以按照一定的规则找到测试用例

特点:

文件名test_开头或者_test结尾的py文件

以Test开头的类

以test_开头的方法

所有的包必须有init.py文件

测试报告:

使用pytest插件:pytest--html

运行:测试用例路径>pytest -s

D:\Python\PycharmProjects\pythonProject\testCases>pytest

运行方法:

pytest会从我们当前运行目录开始查找所有目录,查找以test_开头或者_test结尾的py文件名且文件中所有以

test_开头的函数和以Test开头的类和类里面以test_开头的方法为测试用例

单独执行某个py文件中的所有测试用例

pytest test.py

执行目录下的所有用例

pytest testCase/

单独执行某个用例函数/类

pytest test_login.py::test_1

pytest test_login.py::TestClass::test_1

运行参数:

-s:显示详细日志信息

-v:更加详细的展示结果信息

--html=report.html:生成测试报告

-n NUM:多进程运行

-m:运行执行标记的测试用例

-k:关键字参数,执行带关键字的测试用例

-q:显示测试结果信息

fixture:

详细介绍可以参考路径https://www.cnblogs.com/huizaia/p/10331469.html

与python自带的unittest测试框架中的setup,teardown类似,pytest提供fixture函数

用以在测试执行前和执行后进行必要的准备和清理工作。但是fixture函数对setup和teardown进行了很大的改进

@pytest.fixture(scope='Function,class,module,session',auto=False)

@pytest.fixture()

scope参数:执行范围

Function,每个test都运行,默认是function的scope

class,每个class的所有test,只允许一次

module,每个module的所有test,只允许一次

session,每个session,只运行一次

自定义mark:

通过@pytest.mark控制需要执行哪些feature的test

在pytest当中,先给用例打标记,在运行时,通过标记名来过滤测试用例

注册标记:创建一个pytest.ini文件:写入[pytest]markers=me:yuo

使用标记:在测试用例的前面加上:@pytest.mark.已注册标签名

skip

跳过用例mark:

skip和skipif标记,测试会直接跳过,而不会被执行

@pytest.mark.skipif(condition)

使用标记:在测试用例的前面加上:@pytest.mark.已注册标签名

参数化

允许在测试函数或类中定义多组参数

传入单个参数:

@pytest.mark.parametrize('参数名',lists)

传入两个参数:

@pytest.mark.parametrize('参数1','参数2',.[(参数1_data[0],参数2_data[0]),(参数1_data[1],参数2_data[1])])

@pytest.mark.parametrize('user,pwd', data())

conftest.py

配置里可以实现数据共享,不需要import就能自动找到一些配置

conftest文件实际应用需要结果fxture来使用,fixture中参数scope也使用conftest中fixture的特性

conftest.py配置需要注意一下点:

conftest.py配置脚本名称是固定的,不能该名称

conftest.py与允许的用例要在同一个pakage下,名且有_init_.py文件

不需要import导入conftest.py,pytest用例会自动查找

案例

fixture的举例,module仅运行一次

import pytest

@pytest.fixture(scope='module')

def test_1():

a = 'test'

b = '1'

print("test_1被调用执行")

return a, b

def test_2(test_1):

print("\ntest2 :" + test_1[0] + "_" + test_1[1])

class Test3:

def test_3(self, test_1):

print("test3 :" + test_1[0] + "_" + test_1[1])

def test_4(self, test_1):

print("test4 :" + test_1[0] + "_" + test_1[1])

if __name__ == '__main__':

pytest.main(['-s', 'test_login.py'])

skip和参数化举例:

import pytest

def test_1():

print("test 1")

num1 = 1

num2 = 2

@pytest.mark.skipif(num1 < num2, reason='跳过')

def test_2():

print("test 2")

def test_3():

print("test 3")

def data():

return[('123', '123'), ('456', '456'), ('789', '789')]

@pytest.mark.parametrize('user,pwd', data())

def test_4(user, pwd):

print(user + pwd)

pagefactory:待更新...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值