前期准备:
安装python3.10.4 appium 1.22.3 模拟器7.0.2.1 JDK1.8
如果appium链接夜神模拟器出现错误:Failed to create session. An unknown server-side error occurred while processing the command. Original error: Unable to find an active device or emulator with OS 7.0.2.8. The following are available: 127.0.0.1:62001 (7.1.2)
获取platform版本:adb shell getprop ro.build.version.release,修改appium中platform版本
pytest
命名规则:类名以Test开头,不能有_init_方法;方法以test_开头;模块名以test_开头或者以_test结尾
pytest用例的运行方式:
1.主函数模式
- 运行所有:pyest.main()
- 运行指定的:pyest.main(['-vs','xxx.py'])
- 运行目录:pyest.main(['-vs','./目录名'])
- 通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成。pyest.main(['-vs','./目录名/xxx.py::类名::函数名']) 或 pyest.main(['-vs','./目录名/xxx.py::函数名'])
2.命令行模式
- 运行所有:pytest
- 运行指定的:pytest -vs xxx.py
- 运行包含XXX的测试名称:pytest -k xxx -vs
- 运行具有xxx标记名称的测试:pytest -m xxx -vs
- -s:表示输出调试信息,包括print打印的信息
- -v:表示输出详细的信息 【-vs】
- -n:支持多线程分布运行: pyest -vs ./目录/xxx.py -n 2
- --reeruns num:失败用例重跑。pyest.main(['-vs','./目录名/xxx.py::类名::函数名','--reeruns=2'])
- -x:表示只要有一个用例报错,那么测试停止。
- --maxfail=2:出现两个用例失败就停止。
- -k “ ao”:根据用例部分字符串指定测试用例。pyest -vs ./目录/xxx.py -k "ao"
- 生成报告--html:--html ./repote/report.html 。 addopts = -vs --html ./report/repote.html
- 用户可以创建自己的标记名称:@pytest.mark.<markername>
3.通过pytest.ini配置文件运行
[pytest]
addopts = -vs
# 测试用例文件夹,可自己配置,../pytestobject为上一层的pytestobject文件夹
testpaths = ./testcase
#配置测试搜索的模块文件名称
python_files = test_*.py
#配置测试搜索的测试类名
python_classes = Test*
#配置测试搜索的测试函数名
python_functions = test
markers =
smoke:冒烟用例
usermanage:用户管理模块
productmanage:商品管理模块
- 位置:一般放在项目的根目录。
- 编码:必须是ANSI,可以使用notpad++修改编码格式。
- 作用:改变pytest默认的行为。
- 运行规则:不管是主函数的模式运行,命令行模式运行,都会去读取这个配置文件。
4.pytest测试用例的顺序
- 默认从上到下。添加标记改变顺序:@pytest.mark.run(order = num)
5.如何分组执行(冒烟,分模块执行,分接口和web执行)
- smoke:冒烟用例,分布在各个模块里面@pytest.mark.smoke。运行时通过pytest -vs -m 'smoke'
- 执行smoke:冒烟用例和usermanage:用户管理模块:pytest -vs -m 'smoke or usermanage'
6.pytest跳过测试用例
(1)无条件:@pytest.mark.skip(reason="微微")
(2)有条件:@pytest.mark.skipif(age<=18,reason="未成年")
pytest框架实现前后置
1.setup/teardown,steup_class/teardown_class
2.使用@pytest.fixture()装饰器来实现部分用例的前后置。@pytest.fixture(scope="",params="",autouse="",ids="",name="")
- scope="":表示作用域。function(默认),class,module,package/session
@pytest.fixture(scope ='function' ,params=['成龙','甄子丹'])
def my_fixture(request):
print('\n测试1---前')
yield
print('\n测试1----后')
return request.param
class TestMa:
def test_01_bili(self):
print('\n测试')
def test_02_ce(self):
print('\n测试1')
print('\n----------------'+str(my_fixture)
- params="":参数化(支持list,元组,字典列表,字典元组)
- autouse=true:自动执行,默认false
- ids="":当时用params参数化时,给每个值设置一个变量名。意义不大。
- name="":表示的是@pytest.fixture标记的方法取一个别名。
- yield:表示分割
注意:return和yield都表示返回的意思,但是return的后面不能有代码,yield返回后后面可以接代码。
当取了别名之后,那么原来的名称就用不了了。
3.通过conftest.py和@ytest.fixture()结合使用实现全局的前置应用(比如:项目的全局登录,模块的全局处理)
- conftest.py文件是单独存放的一个夹具配置文件,名称是不能更改。
- 用处可以在不同的py文件中使用同一个fixture函数。
- 原则上conftest.py文件需要和运行的用例放到同一层,并且不需要做任何的import导入的操作。不同的模块加一个conftest
Xfail/Skip测试
- 由于某些原因,一个测试在一段时间内没有意义。
- 一个新功能正在实现,我们已经为该功能添加了一个测试。
在这些情况下,我们可以选择xfail测试或跳过测试。Pytest将执行xfailed测试,但它不会被认为是失败或通过测试的一部分。即使测试失败,这些测试的细节也不会被打印出来。跳过一个测试意味着该测试将不被执行。
编辑 test_compare.py ,我们已经包括了xfail和skip标记 —
import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
num = 100
assert num > 100
@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
num = 100
assert num >= 100
@pytest.mark.skip
@pytest.mark.others
def test_less():
num = 100
assert num < 200
pytest test_compare.py -v
执行上述命令后,将生成以下结果 −
test_compare.py::test_greater xfail
test_compare.py::test_greater_equal XPASS
test_compare.py::test_less SKIPPED
============================ 1 skipped, 1 xfailed, 1 xpassed in 0.06 seconds
============================
N次测试失败后停止测试套件
如果我们想在N个测试失败后立即停止测试套件的执行,可以在pytest中使用maxfail来实现。
在n次测试失败后立即停止测试套件的执行,其语法如下
pytest --maxfail = <num>
在执行这个测试文件时,将在一次失败后停止测试的执行,方法是 —
pytest test_failure.py -v --maxfail 1
XML格式的测试执行结果
pytest test_multiplication.py -v --junitxml="result.xml"
总结:
setup/teardown,steup_class/teardown_class他是作用于所有用例或者所有的类。
@pytest.fixture()作用既可以实现部分用例的前后置也可以全部前后置。
conftest.py和@ytest.fixture()结合使用,作用于全局的前后置。
4.断言 assert
5.pytest结合allure-pytest插件生成allure测试报告
pytest - allure报告
pytest插件安装 命令:pip install pytest-allure-adaptor
pytest生成allure测试报告的命令参数 命令:--alluredir=/XXX/my_allure_results
查看allure的测试报告命令allure serve allure报告目录
示例:allure serve D:\Outputs\allure_reports
1、pytest命令基础上加--alluredir,生成xml报告。
pytest -s -q --alluredir [xml_report_path]
2、需要使用 Command Tool 来生成我们需要的美观报告。
allure generate [xml_report_path] -o [html_report_path]
注:直接用chrome浏览器打开报告,报告可能会是空白页面。
解决办法:1、在pycharm中右击index.html
选择打开方式Open in Browser
就可以了。2、使用Firefox直接打开index.html
。
- 验证allure allure --version
- 问题:dos可以验证但是pycharm验证失败。
- 生成json格式的临时报告:--alluredir ./temp
- 生成allure报告:os.system('allure generate ./temp -o ./report --clean')
- allure generate:命令,固定的
- ./temp:临时的json格式报告的路径
- -o :输出output
- ./report:生成allure报告的路径
- --clean:清空./report路径原来的报告
定制报告
- Feature:标注主要功能模块
@allure.feature('test_module_01')
@allure.feature('test_module_02')
- Story: 标注Features功能模块下的分支功能
@allure.feature('test_module_01')
@allure.story('test_story_01')
- Severity:标注测试用例的重要级别
Allure中对严重级别的定义:
--- Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)@allure.severity('blocker')
--- Critical级别:临界缺陷( 功能点缺失)
--- Normal级别:普通缺陷(数值计算错误)
--- Minor级别:次要缺陷(界面错误与UI需求不符)
--- Trivial级别:轻微缺陷(必输项无提示,或者提示不规范) - Step: 标注测试用例的重要步骤
@allure.step("字符串相加:{0},{1}")
- Issue和TestCase: 标注Issue、Case,可加入URL
@allure.issue("http://www.baidu.com")
@allure.testcase("http://www.testlink.com")
参考文章[1]:scoop——强大的Windows命令行包管理工具 - 简书
参考文章[2]:https://www.cnblogs.com/xiaoxi-3-/p/9492534.html
6.python的反射
正常:先初始化对象,在调方法
反射:通过对象得到类对象,然后通过类对象调用方法
7.jenkins的持续继承和allure报告集成
一.python+appium
- 设置终端参数项
- appium server进行启动
- 发送指令给到appium server
- appium server进行启动
- 模拟器/真机必须能够被电脑识别
二.appium的日志查看
1.自动化脚本发送http请求,请求参数:终端设置参数
2.创建会话
3.确认终端设备是否连接并且确认安卓版本,确认设置参数是否与终端一致
4.appium会推送一个包‘appiumBootstrap.jar模拟器上的.jar包:appium server指令进行接受,操控手机端