笔记 并发/多线程/分布式

# pytest  自动化测试- 并发/多线程/分布式

# pytest 自动化测试基石,为自动化测试脚本的编写提供了一系列的规范和功能
'''
编写:规范 test_*  *_test
执行工具  pytest 命令
功能  生成测试报告   pytest --html=pytest --html=report.html (在Terminal local中执行) 可用插件
(pip install pytest-html 生成脚本需要安装html库)
插件 allure
多线程并发(pytest -xdist库   pip install pytest-xdist==3.6.1)   执行操作  pytest -sv -n 4 --html=report-thread.html
最简单的操作 -n auto   计算机会自动计算
多线程并发问题   用例执行顺序会发生明显变化(用例之间有依赖的话必须要一定顺序执行,如何处理:
--dist load  默认顺序
--dist loadfile 同一个脚本文件中安顺序执行
给用例分组,在需要的用例前加 @pytest.mark.xdist_group(name='dep_001') 相同的name会按用力编号顺序执行
)
分布式  :用例执行过程,业务复杂度不同,自动化测试代码的复杂度也会有区别
有些用例需要查各种数据库表,处理各种文件
多线程并发执行能解决自动化测试中的大部分效率问题
怎么用?
环境搭建
pytest 分布式执行分为 两个角色
1.控制机 编写脚本为主  一般不参与运行,也可参与运行
192.168.1.42  

2.执行机  负责执行脚本
192.168.1.56

部署运行
1.每台电脑建议 python pytest 版本一致
2.创建虚拟环境,每台电脑下载pytest 各种库和依赖库,第三方库均保持一致(selenium,jsonpath等等)
3.执行机上运行socket服务(socketserver.py文件需要下载下来,在运行 python socketserver.py)
4.控制机和执行机之间要网络互通,建议内网并关闭防火墙  
5.执行测试   (控制机和执行机的脚本文件需要一致,控制机选择一下命令执行测试用例脚本,--rsyncdir 后需要贴自己脚本的地址)
(pytest -sv --html=report-dist.html 普通执行语句)
(pytest -sv -d --tx socket=192.168.1.56:8888 --rsyncdir D:\pythoncharm\pythonProject2\test\test_scripts.py --html=report-dist.html 普通执行语句)






pip install pytest
1
验证版本
pytest --version
1
使用
def f(a):
   return a + 1

def tests_f():
    # 断言
   assert func(1) == 2

执行
执行shell命令

pytest
1
django 使用
安装
pip install pytest-django
1
使用
在Django项目的根目录,也就是和manage.py同一级,创建配置文件pytest.ini。

[pytest]
DJANGO_SETTINGS_MODULE=settings.local
python_files=pytest_*.py
python_paths=app
filterwarnings =ignore::Warning

DJANGO_SETTINGS_MODULE:根据自己项目实际配置文件填充。

python_files:所有以pytest_开头的文件,在单独运行pytest之时都会被执行。

python_paths:此项配置python路径,若app在当前目录,可忽略,若app均在指定的目录之下,这里指定app文件目录。

filterwarnings:配置忽略的预警级别。

测试代码

import json
import mock
import pytest
from django.test import Client

@pytest.mark.django_db
@pytest.fixture(scope='function')
def create_user():
    user = User.objects.create(name="测试姓名")

@pytest.mark.django_db
def test_user_api():
    response = Client().get(path='/user', data=json.dumps(data), HTTP_AUTHORIZATION="token 123456")
    content = response.json()
    assert content['name'] == "测试姓名"

@pytest.mark.django_db:启用数据库的访问。

@pytest.fixture(scope='function'):fixture修饰器来标记固定的一个函数,其他函数和模块之类的方法调用被修饰器标记的函数时会被激活并优先执行,通常会被用于完成预置处理和重复操作。例如上面test_user_api方法执行之前会自动调用加了fixture修饰器的create_user()方法。

fixture修饰器
修饰器使用方式:@pytest.fixture(scope='session')
fixture中的scope参数用来控制fixture的作用范围:session > module > class > function。
function:每一个函数或方法都会调用。
class:每一个类调用一次,一个类可以有多个方法。
module:每一个.py文件调用一次。
session:整个会话调用一次。
————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/weixin_44649870/article/details/122606204
'''

from time import sleep

import jsonpath
import pytest
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service as ChormeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


@pytest.fixture
def driver(): # 夹具  --固定装置 --封装一个通用的浏览器函数,把浏览器驱动初始化,其他用例一旦调用driver就激活再用力开始执行前使用
    driver = webdriver.Chrome(service=ChormeService())   #ChormeService() 可以加入 r'浏览器驱动地址'
    yield driver
    driver.quit()
def test_ui_001(driver):  #pytest用例规范,必须以test开头或者结尾
    ''' UI自动化测试登录功能 '''
    driver.get('http://192.168.239.128/opencart/')
    el = driver.find_element(By.XPATH, '//*[@id="top-links"]/ul/li[2]/a/span[1]')
    ActionChains(driver).move_to_element(el).perform()
    driver.find_element(By.LINK_TEXT, '会员登录').click()
    sleep(3)
    driver.find_element(By.ID, 'input-telephone').send_keys('12233335555')
    driver.find_element(By.ID, 'input-password').send_keys('123456')
    driver.find_element(By.XPATH, '//*[@id="content"]/div/form/input').click()
    assert EC.url_to_be("http://192.168.239.128/opencart/index.php?route=account/account")(driver), '检查是否成功登录跳转到首页'

def test_ui_002(driver):...

@pytest.mark.xdist_group(name='dep_001')
def test_api_003():...

@pytest.mark.xdist_group(name='dep_001')
def test_api_004(driver):...

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值