zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【使用pytest重构项目】pytest allure生成测试报告

pytest项目重构 生成 测试报告 Allure 使用
2023-09-11 14:17:00 时间

前言

一直想学习自动化测试,但是都没行动,业余时间学习零零碎碎并记录20210420。

 

6、使用pytest重构项目

  • pytest框架介绍
  • pytest标记
  • pytest参数处理
  • pytest Fixtrue
  • pytest allure生成测试报告
  • pytest setup和teardown应用
  • 使用pytest重构项目

 

pytest allure生成测试报告

1、安装插件allure-pytest

  • (Mac):pip3 install allure-pytest
  • (Windos):pip install allure-pytest
ffdeMBP ~ % pip3 install allure-pytest
Defaulting to user installation because normal site-packages is not writeable
Collecting allure-pytest
  Downloading allure_pytest-2.8.40-py3-none-any.whl (9.6 kB)
Requirement already satisfied: six>=1.9.0 in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site-packages (from allure-pytest) (1.12.0)
Requirement already satisfied: pytest>=4.5.0 in /Library/Python/3.7/site-packages (from allure-pytest) (6.2.3)
Collecting allure-python-commons==2.8.40
  Downloading allure_python_commons-2.8.40-py3-none-any.whl (15 kB)
Requirement already satisfied: attrs>=16.0.0 in /Library/Python/3.7/site-packages (from allure-python-commons==2.8.40->allure-pytest) (20.3.0)
Requirement already satisfied: pluggy>=0.4.0 in /Library/Python/3.7/site-packages (from allure-python-commons==2.8.40->allure-pytest) (0.13.1)
Requirement already satisfied: importlib-metadata>=0.12 in /Library/Python/3.7/site-packages (from pluggy>=0.4.0->allure-python-commons==2.8.40->allure-pytest) (3.10.1)
Requirement already satisfied: typing-extensions>=3.6.4 in /Library/Python/3.7/site-packages (from importlib-metadata>=0.12->pluggy>=0.4.0->allure-python-commons==2.8.40->allure-pytest) (3.7.4.3)
Requirement already satisfied: zipp>=0.5 in /Library/Python/3.7/site-packages (from importlib-metadata>=0.12->pluggy>=0.4.0->allure-python-commons==2.8.40->allure-pytest) (3.4.1)
Requirement already satisfied: py>=1.8.2 in /Library/Python/3.7/site-packages (from pytest>=4.5.0->allure-pytest) (1.10.0)
Requirement already satisfied: toml in /Library/Python/3.7/site-packages (from pytest>=4.5.0->allure-pytest) (0.10.2)
Requirement already satisfied: packaging in /Library/Python/3.7/site-packages (from pytest>=4.5.0->allure-pytest) (20.9)
Requirement already satisfied: iniconfig in /Library/Python/3.7/site-packages (from pytest>=4.5.0->allure-pytest) (1.1.1)
Requirement already satisfied: pyparsing>=2.0.2 in /Library/Python/3.7/site-packages (from packaging->pytest>=4.5.0->allure-pytest) (2.4.7)
Installing collected packages: allure-python-commons, allure-pytest
Successfully installed allure-pytest-2.8.40 allure-python-commons-2.8.40

2、下载allure一个可执行文件

  • 地址:https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.7.0/

  • 设置环境变量:.bash_profile文件增加(PATH=$PATH:/usr/local/allure-2.7.0/bin)

 

 ~ % cd Downloads
 Downloads % sudo mv allure-2.7.0 /usr/local
Password:
 % cd /usr/local
 % ls
Caskroom     Frameworks   allure-2.7.0 etc          lib          sbin         solarized
Cellar       Homebrew     bin          include      opt          share        var
 local % cd allure-2.7.0/bin
 bin % pwd
/usr/local/allure-2.7.0/bin
 bin % cd ~
 ~ % vim .bash_profile
 ~ % source .bash_profile
 ~ % allure --version
2.7.0

allure用例描述

使用方法参数值参数说明
@allure.epic()epic描述敏捷里面的概念,定义史诗,往下是feature
@allure.feature()模块名称功能点的描述,往下是story
@allure.story()用户故事用户故事,往下是title
@allure.title(用例的标题)用例的标题重命名html报告名称
@allure.testcase()测试用例的链接地址对应功能测试用例系统里面的case
@allure.issue()缺陷对应缺陷管理系统里面的链接
@allure.description()用例描述测试用例的描述
@allure.step()操作步骤测试用例的步骤
@allure.severity()用例等级blocker,critical,normal,minor,trivial
@allure.link()链接定义一个链接,在测试报告展现
@allure.attachment()附件报告添加附件

测试案例

  • pytest结合allure测试用例

#pytest生成测试报告
import allure
import pytest

@pytest.fixture(scope="session")
def login():
    print("用例先登录")

@allure.step("步骤1:点xxx")
def step_1():
    print("111")

@allure.step("步骤2:点xxx")
def step_2():
    print("222")

@allure.feature("编辑页面")
class TestEditPage():
    """编辑页面"""

    @allure.story("这是一个xxx的用例")
    def test1(self,login):
        """用例描述 先登录再执行story的用例"""
        step_1()
        step_2()
        print("这是一个xxx的用例")

    @allure.story("打开a页面")
    def test2(self,login):
        """用例描述:先执行fixcure的登录然后再执行yyy"""
        print("yyy")

if __name__ == '__main__':
    # 注意生成测试报告必须在命令行执行
    # 所以执行时要切换到seelenium_projrct目录下去
    # pytest --alluredir ./reports testcases/pytest/test_07.py  # 生成测试报告存放到路径reports目录下
    # 所以执行时要切换到seelenium_projrct目录下去
    # allure serve ./reports  启动allure 查看报告

    pytest.main(['--alluredir' './reports' 'test_07.py'])
  • 查看报告(打开终端,切换到seelenium_projrct目录下去,用 allure serve ./reports命令启动allure 查看报告)

“永不放弃,总有希望在前面等待!”送给自己,也送给正在阅读文章的博友们~