3.1 增加多进程执行playwright

增加了多进程的方式执行测试代码,对代码改动比较大

1、case

case目录依然是自动生成

3.1 增加多进程执行playwright_第1张图片

 2、config

dir_collection.py新增了配置

mkdir_collections = [
    'case',
    'log',
    'img',
]
del_collections = [
    'results',
    'report'
]
del_regex = 'temp'

3、data/img/log/resource/video

data/img/log/resource目录没做修改,删除了video目录

4、results/report及以temp开头的目录是自动生成,且会每次运行前都会自动删除

5、utils目录

删除了delete.py

修改了dir_check.py

import os
from config.dir_collection import mkdir_collections, del_collections, del_regex
import shutil


def check_dir():
    mk_dir()
    del_files()


def mk_dir():
    li = os.listdir()
    for i in mkdir_collections:
        if i not in li:
            os.mkdir(i)


def del_files():
    li = os.listdir()
    for i in li:
        if i.startswith('temp'):
            shutil.rmtree(i)
        elif i in del_collections:
            shutil.rmtree(i)


 修改了video.py

import os
import time


def generate_video(source_path: str):
    p = f"{source_path}/{int(time.time())}.webm"
    while True:
        if os.listdir(source_path):
            for i in os.listdir(source_path):
                os.renames(f'{source_path}/{i}', p)
            break
    return p

 新增了process.py

import os
import time

import pytest
import multiprocessing


def pytest_run(a):
    f = ['-s', '--alluredir=results']
    for i in a:
        f.append(i)
    print(f)
    pytest.main(f)


def split_list(lst, n):
    """将列表 lst 按 n 等份分割"""
    if n > len(lst):
        raise Exception('The number of processes cannot be greater than the number of test files')
    k, m = divmod(len(lst), n)
    return [lst[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n)]


def process(size: int, d: str):
    file_list = os.listdir(d)
    temp = []
    for i in file_list:
        file_path = d + '/' + i
        if os.path.isfile(file_path) and i.startswith('test'):
            temp.append(file_path)
    li = split_list(temp, size)
    pro = [multiprocessing.Process(target=pytest_run, args=(li[i],)) for i in range(size)]
    for p in pro:
        p.start()
    for p in pro:
        p.join()

6、conftest.py

修改了conftest.py

import time

import pytest
from playwright.sync_api import sync_playwright
from config.setting import config
from playwright.sync_api import Page
from utils.operate import operate
from utils.baseurl import get_baseUrl
import os
import allure
from utils.video import generate_video


def getName():
    pid = os.getpid()
    temp_file_name = f'temp{pid}'
    return temp_file_name


@pytest.fixture(scope='session')
def browser():
    browser = sync_playwright().start().chromium.launch(headless=False, slow_mo=500)
    return browser


@pytest.fixture(scope='session')
def page(browser):
    page = browser.new_page(ignore_https_errors=True, record_video_dir=getName())
    page.goto(get_baseUrl(config))
    operate(config['username'], page)
    operate(config['password'], page)
    operate(config['submit'], page)
    return page


def log(request):
    with open('log/http.txt', 'a', encoding='utf-8') as w:
        w.write(f'{request}.url' + '\n')


@pytest.fixture(scope='function', autouse=True)
def after(page: Page):
    yield
    page.on("request", lambda request: log(request))


@pytest.fixture(scope='session', autouse=True)
def clear(page: Page, browser):
    yield
    page.close()
    browser.close()
    p = generate_video(getName())
    allure.attach.file(p, f'{os.path.basename(p)}', attachment_type=allure.attachment_type.WEBM, extension='WEBM')

7、修改了main.py 

import os
import shutil
import time

from playwright.sync_api import sync_playwright
from config.setting import config
from utils.template import Template
from utils.md5 import Md5
import pytest
from utils.dir_check import check_dir
from utils.baseurl import get_baseUrl
from utils.process import process


def run():
    data = os.listdir('data')
    m = Md5('case', 'log', 'case_md5.json')
    n = Md5('utils', 'log', 'template_md5.json')
    filter_list = m.filter()
    utils_list = n.filter()
    if 'template.py' not in utils_list:
        filter_list = []
        n.write_md5()
    for i in data:
        file_path = 'data' + '/' + i
        if os.path.isfile(file_path):
            temp = 'test_' + i
            if temp not in filter_list:
                Template.create_test_file(file_path, 'case')
    m.write_md5()


if __name__ == "__main__":
    check_dir()
    get_baseUrl(config)
    run()
    process(2, 'case')
    os.system('allure generate  results -o ./report')
    for file_name in os.listdir('resource'):
        src_file = os.path.join('resource', file_name)
        dst_file = os.path.join('report', file_name)
        if os.path.exists(dst_file):
            os.remove(dst_file)
        shutil.copy(src_file, 'report')
    os.system('allure open report')

8、总结

最终修改后的效果,可以使用多进程的方式,开启多个浏览器实例,去对case目录下的测试文件分片执行,在测试文件特别多的情况下,是可以提高执行效率的。

其次,每个进程都有自己的视频及截图,进程之间互不干扰 

你可能感兴趣的:(python,python,开发语言)