python 项目目录结构

MobileAutomation 项目结构
MobileAutomation
├─ Basefunctest
│  │  __init__.py
│  │
│  ├─ iOS
│  │  │  conftest.py
│  │  │  main.py
│  │  │  test_create_blank_pdf.py
│  │  │  test_file_to_pdf.py
│  │  │  test_merge_pdf.py
│  │  │  test_scan.py
│  │  │  test_web_to_pdf.py
│  │  │  __init__.py
│  │  │
│  │  ├─ common
│  │  │      data_operator.py
│  │  │      func_operator.py
│  │  │      web_operator.py
│  │  │      __init__.py
│  │  │
│  │  └─ page
│  │  │      base_page.py
│  │  │      blank_pdf_page.py
│  │  │      file_content_page.py
│  │  │      locator.py
│  │  │      main_page.py
│  │  │      page_operator.py
│  │  │      scan_page.py
│  │  │      web_to_pdf_page.py
│  │  │      __init__.py
│  │  │
│  │  └─ report
│  │  │      allure_report
│  │  │      allure_result_file
│  │  │
│  └─ tools
│      └─ allure-2.13.9
│          ├─ bin
│          │      allure
│          │      allure.bat
│          │
│          ├─ config
│          │      allure-cucumber.yml
│          │      allure-junit.yml
│          │      allure.yml
│          │
测试入口的 main.py
# coding=utf-8
# !/usr/bin/env python
# description: execute tests entrance
import os
import shutil
from common import data_operator
from common import func_operator

if __name__ == '__main__':
    report_path = os.path.join(data_operator.WORK_SPACE, 'Basefunctest', 'iOS', 'report')
    print(f"report_path:{report_path}")
    # clear report directory
    if os.path.exists(report_path):
        shutil.rmtree(report_path)
        os.mkdir(report_path)
    allure_result_file = './report/allure_result_file'
    allure_report = './report/allure_report'
    execute_str = f"python3 -m pytest --alluredir {allure_result_file}"

    # check jenkins environ
    environ_dict = os.environ
    build_url = environ_dict.get('BUILD_URL')
    print(f"jenkins build url:{build_url}")

    # run main.py on jenkins
    if build_url:
        allure_url = ''.join([build_url, 'allure'])
        print(f"jenkins allure url:{allure_url}")
        os.system(execute_str)

    # run main.py local
    else:
        ip = func_operator.get_host_ip()
        port = func_operator.get_free_port(8000, 9000)
        print(f"allure server port:{port}")
        allure_path = '../tools/allure-2.13.9/bin/allure'
        os.system(execute_str)
        os.system(f"{allure_path} generate {allure_result_file} -o {allure_report} --clean")
        os.system(f"{allure_path} open -h {ip} -p {port} {allure_report}")

jenkins 配置

新建一个节点

当前测试的 iOS 工程需要在 mac 电脑上跑,所以需要在 mac 上搭建一个节点。
在这里插入图片描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-44rSUFFy-1625570438320)(mdpic\jenkins+pytest+allure2.png)]
在这里插入图片描述

启动节点

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-htBLzLGp-1625570438325)(mdpic\jenkins+pytest+allure5.png)]


新建一个项目
配置要运行的节点标签

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uBlwdCKC-1625570438326)(mdpic\jenkins+pytest+allure7.png)]

配置git

添加密码,创建子目录(名字为git的项目名)。其中有一些配置是需要安装jenkins插件的。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uzV4CctW-1625570438326)(mdpic\jenkins+pytest+allure8.png)]在这里插入图片描述

配置build

选择了用shell脚本的方式触发测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zl9JmQSL-1625570438327)(mdpic\jenkins+pytest+allure10.png)]

配置allure插件

和项目中的 main.py 文件中的代码相匹配:测试产生的 allure 文件存在路径、生成报告的路径
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OeNCdpgr-1625570438328)(mdpic\jenkins+pytest+allure11.png)]在这里插入图片描述

展示

在这里插入图片描述在这里插入图片描述在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐