Pytest + Playwright + Allure + BDD 框架与GitHub Action集成并以GitHub Pages发布Allure测试报告

本文介绍了如何使用Pytest、Playwright、Allure和BDD框架构建一个GitHubAction示例,包括项目与GitHubAction的集成、Allure报告生成及GitHubPages展示测试报告的详细步骤。
摘要由CSDN通过智能技术生成

本文主要是想通过以Pytest + Playwright + Allure + BDD框架为demo的项目,介绍项目怎样与 Github的持续集成工具GitHub Action进行集成并展示allure report,如果大家对框架搭建感兴趣,可以参考另一篇文章 自动化框架设计及落地的那些事

1 GitHub 基础知识

1.1 GitHub Action是什么

首先我们先来了解一下GitHub Action基础知识。GitHub Action是GitHub 推出的持续集成的工具,持续集成由很多操作组成,比如获取代码、安装依赖、运行测试、上传测试结果等等。GitHub 把这些操作就称为 actions。很多的actions在不同的项目里面是相同的,因此GitHub是允许开发者把每个操作写成独立的脚本文件,存放到代码仓库,使得其他开发者可以引用的。

到目前当我们要集成一个项目到GitHub Action时,基本就只需要引用他人已经写好的多个action即可,整个持续集成过程,就变成了一个 actions 的组合。我们可以到官方网站搜索更多的actions。

引用action的语法 userName/repoName@版本号。 如actions/setup-python@v5,就表示github.com/actions/setup-python这个仓库,它代表一个 action,作用是安装 Python,@后面指出的是安装的版本号。后面集成到GitHub Action时还会详讲用法。

1.2 怎么集成到GitHub Action

知道了action之后,我们就来聊聊怎么将项目集成到Github Action :就是在我们仓库的根目录下,创建一个 workflow文件,放在.github/workflows目录下,workflow文件是一个 *.yml 文件——这个 Yaml 文件就是 Github Action 的配置文件。

一些术语介绍:

  • workflow (工作流程):持续集成一次运行的过程,就是一个 workflow。
  • job (任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。
  • step(步骤):每个 job 由多个 step 构成,一步步完成。
  • action (动作):每个 step 可以依次执行一个或多个命令(action)。

Workflow 是由一个或多个 job 组成的可配置的自动化过程。workflow的配置文件字段比较多,可以参考官网介绍Workflow syntax for GitHub Actions。下面是一些基本字段。

(1)name

Workflow 的名称,Github 在存储库的 Action 页面上显示 Workflow 的名称,如:

name: playwright pytest bdd demo CI

(2)on

触发 Workflow 执行的 event 名称,比如:每当我提交代码到 Github 上的时候执行workflow可以写为:

on:push

(3)on.<push|pull_request>.<tags|branches>

指定触发事件时,可以限定分支或标签

(4)jobs.<job_id>.name

一个 Workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务

每个 job 必须具有一个 id 与之关联。上面的 my_first_job 和 my_second_job 就是 job_id。

(5)jobs.<job_id>.needs

needs 可以标识 job 是否依赖于别的 job——如果 job 失败,则会跳过所有需要该 job 的 job

(6)jobs.<jobs_id>.outputs

用于和 need 打配合,outputs 输出=》need 输入

(7) jobs.<job_id>.runs-on

指定运行 job 的运行环境,Github 上可用的运行器为:

(8)jobs.<job_id>.steps

steps字段指定每个 Job 的运行步骤,可以包含一个或多个步骤。step 可以运行:

commands:命令行命令 setup tasks:环境配置命令(比如安装个 Node 环境、安装个 Python 环境) action(in your repository, in public repository, in Docker registry):一段 action, 每个 step 都在自己的运行器环境中运行,并且可以访问工作空间和文件系统。

(9)jobs.<job_id>.steps.uses

上面已经讲到action,那么action在workflow怎么引用的?以一个安装python的例子说明:

2 实例:Pytest + Playwright + Allure + BDD Demo项目集成到GitHub Action

2.1 集成GitHub Action

搭建一个Pytest + Playwright + Allure + BDD框架的demo项目并提交到github,添加yml文件到项目.github/workflows目录下,主要想实现的是能下载代码自动搭建环境跑测试、出报告,具体步骤细节:

获取代码库 ->安装python->安装pipenv来管理环境->安装项目依赖->安装浏览器->跑自动化测试用例->自动化测试报告作为附件上传

那么结合上述对workflow的了解以及查找一些已有的action,具体我们的workflow yml文件的内容:

# This is a basic workflow to help you get started with Actions

name: playwright pytest bdd demo CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  schedule:
    - cron: '0 0,4,12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  test:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.8'
          allow-prereleases: true
          cache: 'pipenv'

      - name: Install pipenv
        run: pip install pipenv

      - name: Install dependencies
        run: |
          pipenv install wheel
          pipenv install

      - name: Ensure browsers are installed
        run: pipenv run python -m playwright install

      - name: Run test cases
        run: pipenv run python run.py

      - uses: actions/upload-artifact@v3
        if: always()
        with:
          name: playwright-traces_and_results
          #跑完的allure测试结果是放在项目report目录下的,打包该目录上传
          path: |
            report/
            *trace.zip

有了这个yml文件之后,每当我们push代码就会在action tab下触发一下CI,并会运行我们的测试

运行完之后,我们点进run完的workflow进入可以看到运行后上传的测试报告的附件文件

下载测试报告附件到本地解压后的文件内容如下,需要特定工具或运行allure命令打开查看报告。

点击进入job我们还可以看到详细的运行步骤

至此,我们的测试流水线算是完成了,跑完了测试且生成了测试报告附件。但是当我们组内的其他人员想要直观的看到测试报告,需要下载附件并用一定的工具打开或运行一些allure命令,比较麻烦,这个是否可以优化以其他便捷的方式打开呢?带着这个问题,我们继续探索,发现了GitHub另一个强大的功能GitHub Pages, 可用于在Web 上实时发布网站代码。

2.2 集成GitHub Pages

GitHub Pages 是 GitHub 提供给用户用来展示个人或者项目主页的静态网页系统。也就是说我们可以把项目代码写好后上传 GitHub,然后利用 GitHub Pages 为这个项目生成一个静态页面,别人通过网址可以直接访问我们的页面。怎么用它来展示我们Allure测试报告的html文件呢?

(1)进入到 上文提到的Pytest + Playwright + Allure + BDD框架的demo项目GitHub地址,创建一个空分支gh-pages,用此分支来展示测试报告静态文件。项目下就有两个分支了:

(2)settings下对pages进行设定Branch分支为gh-pages,可以看到上方GitHub Page的访问站点更新显示如下:

(3)到Settings-Action-General下设置运行workflow时授予GITHUB_TOKEN权限

(4)设定好一切之后,我们对workflow进行一些调整,通过gh-pages分支来发布测试报告,yml文件内容如下:

# This is a basic workflow to help you get started with Actions
name: playwright pytest bdd demo CI
# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  schedule:
    - cron: '0 0,4,12 * * *'
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  test:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.8'
          allow-prereleases: true
          cache: 'pipenv'

      - name: Install pipenv
        run: pip install pipenv

      - name: Install dependencies
        run: |
          pipenv install wheel
          pipenv install

      - name: Ensure browsers are installed
        run: pipenv run python -m playwright install

      - name: Run test cases
        run: pipenv run python run.py

      - uses: actions/upload-artifact@master
        with:
          name: Test-allure-report
          path: report #  path to store the allure report
          retention-days: 20

      - name: Get Allure history # Step to retrieve Allure history
        uses: actions/checkout@v4
        if: always() # Execute even if previous steps fail
        continue-on-error: true # Continue execution even if the step fails
        with:
          ref: gh-pages # Specify the branch to retrieve Allure history from
          path: gh-pages # Set the destination path for Allure history

      - name: Generate allure report action # Step to generate Allure report
        uses: simple-elf/allure-report-action@master
        if: always() # Execute even if previous steps fail
        id: allure-report
        with:
          allure_results: temp # path to store the allure results
          allure_history: allure-history # Specify the directory to store Allure history
          allure_report: allure-report
          keep_reports: 20 # Specify the number of previous reports to keep

      - name: Deploy Allure report to GitHub Pages # Step to deploy Allure report to GitHub Pages
        if: always() # Execute even if previous steps fail
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }} # Provide the GitHub token for authentication
          publish_branch: gh-pages # Specify the branch to publish the report to
          publish_dir: allure-history # Specify the directory containing the report
            



(5)push代码,会优先trigger 项目playwright pytest bdd demo CI运行测试,再自动产生一个名为pages-build-deployment的新CI,发布测试结果报告到pages

点击运行完的发布测试报告的CI,通过点击如下链接就可以打开报告了(速度有点慢)

测试报告:

参考资料:

https://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html 

  • 25
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值