移动端自动化测试报告的生成之pytest

首先我们需要在python里面安装

移动端自动化测试报告的生成之pytest

因为我使用的是python3版本,所以pip3加个3,这个时候enter运行会下载安装,就要看网速了

运行完之后我们pip list

移动端自动化测试报告的生成之pytest

这时我们会看到有pytest,说明已经安装成功

我们需要创建二个包和一个pytest.ini配置文件移动端自动化测试报告的生成之pytest

一个用来存放我们生成的测试报告,一个里面我们写测试用例

pytest.ini里面配置文件:移动端自动化测试报告的生成之pytest

 各个命令行参数 addopts = -s —reruns n —html=路径/xx.html # 命令之间空格分隔
        2.搜索路径:testpaths = 脚本存放的路径
        3.搜索文件名:python_files = test_*.py
        4.搜索测试类名:python_classes = Test_*
        5.搜索方法名:python_functions = test_*

下面看一下代码,随便写了个通讯录的

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
class Test_app():
    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.1'
        desired_caps['deviceName'] = '192.168.56.101:5554'

        desired_caps['appPackage'] = 'com.android.settings'
        desired_caps['appActivity'] = '.Settings '

        desired_caps['noReset'] = 'True'
        desired_caps['unicodeKeyboard'] = True
        desired_caps['resetKeyboard'] = False


        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    def teardown(self):
        pass

    def waitXpath(self,xpath):
        return WebDriverWait(self.driver,5,0.5).until(lambda x:x.find_element_by_xpath(xpath))
    def waitId(self,id):
        return WebDriverWait(self.driver,5,0.5).until(lambda x:x.find_element_by_id(id))


    def test_001(self):
        self.driver.close_app()
        self.waitXpath("//*[contains(@text,'通讯录')]").click()
        self.waitId("com.android.contacts:id/floating_action_button").click()
        self.waitXpath("//*[contains(@text,'姓名')]").send_keys("秀儿")
        self.waitXpath("//*[contains(@text,'电话')]").send_keys("123456")

这时我们不能像平常那样单击右键运行

我们需要移动端自动化测试报告的生成之pytest

会出来这个路径

移动端自动化测试报告的生成之pytest

我们需要输入   使用:跟在命令行里,pytest 测试文件 —html=路径/xx.html

运行完成后

会看到移动端自动化测试报告的生成之pytest

我创建的是html文件夹下面会生成一个html文件

我们会用默认浏览器打开

移动端自动化测试报告的生成之pytest

会生成这样一种测试报告,整体流程就这样

前面我们所说的配置pytest.ini文件

如果配置了可以直接输入pytest运行就可以生成测试报告

下面是我对上述pytest的总结

Pytest:

    1.安装 pip3 install pytest
    2.初始化和结束函数:
        1.setup teardown : 在一个类内部每个测试方法的开始和结束运行一次
        2.setup_class,teardown_class:在一个类内部只运行一次,不关心有多少测试方法

    3.pytest插件
        1.pytest-html: 生成测试报告
            安装:pip3 install pytest-html
            使用:跟在命令行里,pytest 测试文件 —html=路径/xx.html
        2.pytest-ordering: 控制测试函数运行顺序
            使用:@pytest.mark.run(order=x)
            x:
                1.全为正数 或者 全为负数 值越小 优先级越高,意味着最先执行
                2.正数和负数同时存在,正数优先级高
                3.值为负数时,优先级低于没被标记的测试方法
                4.值为正数时,优先级高于没被标记的测试方法
        3.pytest-rerunfailures : 失败测试函数重试机制

            使用:在命令行参数中配置:—reruns n 
            n:重试次数
    4.配置文件:
        1.命令行参数 addopts = -s —reruns n —html=路径/xx.html # 命令之间空格分隔
        2.搜索路径:testpaths = 脚本存放的路径
        3.搜索文件名:python_files = test_*.py
        4.搜索测试类名:python_classes = Test_*
        5.搜索方法名:python_functions = test_*


基本流程大概就这样,后续会加上allure框架生成测试报告