python自动化(三)web自动化:7.测试框架实战三之并发执行用例

一.多进程并发执行测试用例

  • 当我们的自动化进行到一定程度时,用例数量将十分庞大。也许执行一遍用例就会花费几个小时。这时使用分布式来并行执行用例将十分重要。
  • pytest框架可以使用pytest-xdist来十分方便的进行分布式测试。

(1)pytest-xdist讲解

  • 安装
pip install pytest-xdist
  • 使用:多cpu并行执行用例,直接加个-n参数即可,后面num参数就是并行数量,比如num设置为3
pytest -n 3
pytest -n auto # 自动识别cpu个数,启动对应进程

(2)多进程执行用例

多进程要看出效果,需要用例数量较多才行,所以我们可以在yaml数据文件中增加更多数据。
在这里插入图片描述

  • 单进程执行用例
#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import allure
import pytest
from etc.config import LOGGER
from pages.index_page import IndexPage
from tools.get_data import get_data_from_yaml
from etc.config import BASEDIR
from tools.get_strtime import get_now_time

class Test_case_search:
    """
    搜索功能测试用例
    """
    datas = get_data_from_yaml(os.path.join(BASEDIR,'datas/search_datas.yaml'))
    def setup(self):
        LOGGER.info('START TO EXECUTE TEST CASE')
        self.index = IndexPage()
        self.index._get_url('https://www.baidu.com/')


    def teardown(self):
        self.index._quit_driver()

    @pytest.mark.parametrize("text",datas) # 数据参数化,实现数据驱动
    def test_search(self,text):
        try:
            self.index.search_contact(text=text)
            time.sleep(3)
            assert text[0] in self.index._get_title() # 添加断言
            LOGGER.info('EXECUTE TEST CASE SUCCESS')
        except Exception as e:
            LOGGER.error(f'EXECUTE TEST CASE failed, the reason is {e}')
            img_path = os.path.join(BASEDIR,f'reports/images/{get_now_time()}.png')
            self.index._get_screenshot_as_file(img_path) # 失败截图
            allure.attach.file(source=img_path,name='失败截图',attachment_type=allure.attachment_type.PNG) # 将失败截图上传到测试报告中
            raise e

if __name__ == "__main__":
    json_path = os.path.join(BASEDIR, f'reports/allure_result/{get_now_time()}')  # 指定生成的json报告文件的路径
    html_path = os.path.join(BASEDIR, f'reports/allure_report/{get_now_time()}')  # 指定生成的html报告文件的路径
    pytest.main(['-vs', 'test_search_case.py', f'--alluredir={json_path}']) # 生成json报告
    os.system(f'allure generate {json_path} -o {html_path}') # 使用allure生成HTML报告

在这里插入图片描述

  • 多进程执行用例
#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import allure
import pytest
from etc.config import LOGGER
from pages.index_page import IndexPage
from tools.get_data import get_data_from_yaml
from etc.config import BASEDIR
from tools.get_strtime import get_now_time

class Test_case_search:
    """
    搜索功能测试用例
    """
    datas = get_data_from_yaml(os.path.join(BASEDIR,'datas/search_datas.yaml'))
    def setup(self):
        LOGGER.info('START TO EXECUTE TEST CASE')
        self.index = IndexPage()
        self.index._get_url('https://www.baidu.com/')


    def teardown(self):
        self.index._quit_driver()

    @pytest.mark.parametrize("text",datas) # 数据参数化,实现数据驱动
    def test_search(self,text):
        try:
            self.index.search_contact(text=text)
            time.sleep(3)
            assert text[0] in self.index._get_title() # 添加断言
            LOGGER.info('EXECUTE TEST CASE SUCCESS')
        except Exception as e:
            LOGGER.error(f'EXECUTE TEST CASE failed, the reason is {e}')
            img_path = os.path.join(BASEDIR,f'reports/images/{get_now_time()}.png')
            self.index._get_screenshot_as_file(img_path) # 失败截图
            allure.attach.file(source=img_path,name='失败截图',attachment_type=allure.attachment_type.PNG) # 将失败截图上传到测试报告中
            raise e

if __name__ == "__main__":
    json_path = os.path.join(BASEDIR, f'reports/allure_result/{get_now_time()}')  # 指定生成的json报告文件的路径
    html_path = os.path.join(BASEDIR, f'reports/allure_report/{get_now_time()}')  # 指定生成的html报告文件的路径
    pytest.main(['-vs','-n 3', 'test_search_case.py', f'--alluredir={json_path}']) # 生成json报告,使用`-n 3`指定三个进程执行用例
    os.system(f'allure generate {json_path} -o {html_path}') # 使用allure生成HTML报告

运行结果如下:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值