栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

appium 06 pytest的应用(管理用例,生成报告等) 2022学习记录

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

appium 06 pytest的应用(管理用例,生成报告等) 2022学习记录

配合pytest,整个自动化项目会变得更加优雅

基本使用:

下载:

pip  install pytest     

                                    

使用要求:

- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头,并且不能带有 init 方法
- 测试函数以test_开头

如:

class TestLogin:
    def setup_class(self):
        # 获取配置信息
        desired_caps = copy.deepcopy(ServerAppium.caps)
        desired_caps['noReset'] = True
        server_ip = ServerAppium.ip
        self.account_num = Account.num
        self.account_password = Account.password
        # 连接appium server
        self.driver = webdriver.Remote(server_ip, desired_caps)

    def test_login(self):
        print('登录测试')
        # 隐式等待,每个元素找10s
        self.driver.implicitly_wait(10)
        # 调用login封装的登录方法
        login_page = LoginPage(self.driver)
        login_page.login(self.account_num, self.account_password)

启动:

if __name__ == '__main__':
    pytest.main()

pytest 的setup/teardown方法(用于用例初始化和用例结束的清理):

模块级别(setup_module/teardown_module)、
函数级别(setup_function/teardown_function)、
类级别(setup_class/ teardown_class)、
方法级别(setup_method/teardown_methond或者setup/teardown)。

1、setup_module和teardown_module在整个测试用例所在的文件中所有的方法运行前和运行后运行,只会运行一次;
2、setup_class和teardown_class则在整个文件中的一次class中所有用例的前后运行,只运行一次;
3、setup_method和teardown_method在class内的每个方法运行前后运行,运行n条用例次;
4、setup_function和teardown_function则是在非class下属的每个测试方法的前后运行;

参考文档:https://blog.csdn.net/Michaelyq1229/article/details/115740760

装饰器(跳过某个用例):

@pytest.mark.skip(reason = "暂时不执行")

def test_case1(self):
    print("开始执行测试用例1")
    assert 1 + 1 == 2

@pytest.mark.skip(reason="暂时不执行")
def test_case2(self):
    print("开始执行测试用例2")
    assert 2 + 8 == 10

def test_case3(self):
    print("开始执行测试用例3")
    assert 99 + 1 == 100


通过pytest生成测试报告

有多个pytest的三方库可以使用,如pytest-html。这里我使用的是pytest-testrepor

下载:

pip install pytest-testreport

发现报错:

补充需求的三方库

pip install Jinja2

在使用pytest运行测试时,如果要使用pytest-testreport生成测试报告, 运行测试时加上参数--report 指定报告文件名,即可实现。

其他运行参数:
--title :指定报告标题
--tester :指定报告中的测试者
--desc :指定报告中的项目描述
--template :指定报告模板样式(1 or 2)

- 命令行执行:

pytest --report=musen.html --title=测试报告 --tester=wuyutao --desc=项目描述  --template=2

- 代码中使用pytest.main执行

import pytest

pytest.main(['--report=musen.html',
             '--title=报告',
             '--tester=wuyutao',
             '--desc=报告描述信息',
             '--template=2'])

发现main()中的参数未生效:

因为Pycharm识别出是appium并且使用了Pytest框架后,它默认没有使用python来运行,而是使用的pytest

这两处都是pytest

进入run configration修改配置

 

使用python启动Pytest,即可自动生成报告

 

模板中的“用例描述”通过方下第一行(如下的 ’‘’退出登录模式‘‘’ )多引号注释实现:

def test_logout(self):
    '''退出登录模块测试'''
    
    # 隐式等待,每个元素找10s
    self.driver.implicitly_wait(10)
    # 调用logout_page封装的退出
    LogoutPage(self.driver).logout(self.width, self.height)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号