Ubuntu18.04+Python3+allure环境搭建(适用于所有Linux系统搭建allure环境)

环境准备

首先确保当前系统下已安装了Python3和Java

my@mypc:~$ python3
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
my@mypc:~$ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~18.04-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
my@mypc:~$ javac -version
javac 1.8.0_265

安装配置allure

下载allure二进制文件压缩包

点我下载

将下载下来的压缩包解压缩到家(~)目录

# 进入家目录
cd ~
# 解压
unzip allure-2.13.6.zip

进入allure-2.13.6目录下的bin目录,为allure创建软连接即可

# 进入allure-2.13.6目录下的bin目录
cd /home/my/allure-2.13.6/bin
# 创建软连接,需要root权限
sudo ln -s /home/my/allure-2.13.6/bin/allure /usr/bin/allure
# 验证allure是否安装配置成功
allure --version

最后附上官方安装教程

使用allure生成测试报告

# 安装pytest
pip3 install -i https://pypi.douban.com/simple pytest
# 安装allure-pytest
pip3 install -i https://pypi.douban.com/simple allure-pytest

示例Demo

文件结构如下

# allure_demos是根目录,allure_files是空目录用来存放allure生成的数据文件
├── allure_demos
│   ├── allure_files
│   ├── __init__.py
│   └── test_cases_with_allure.py

test_cases_with_allure.py文件内容如下

# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time      : 2020/10/10 下午1:19
# @Author    : passerby223
# @FileName  : test_cases_with_allure.py
# @Description  : 

import pytest


def test_success():
    """this test succeeds"""
    assert True


def test_failure():
    """this test fails"""
    assert False


def test_skip():
    """this test is skipped"""
    pytest.skip('for a reason!')


def test_broken():
    raise Exception('oops')

要使Allure能够在测试执行期间收集测试结果,只需添加 --alluredir 选项,并提供指向应存储结果的文件夹的路径

my@mypc:~$ cd allure_demos/
# 生成测试报告所需数据文件并存放到allure_files文件夹下
my@mypc:~$ pytest  --alluredir=allure_files
# 生成测试报告
my@mypc:~$ allure serve allure_files
Generating report to temp directory...
Report successfully generated to /tmp/652755770337899811/allure-report
Starting web server...
2020-10-10 13:28:35.879:INFO::main: Logging initialized @1452ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://127.0.1.1:41397/>. Press <Ctrl+C> to exit
正在现有的浏览器会话中打开。
[1010/132836.342200:ERROR:nacl_helper_linux.cc(308)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly

执行完allure serve allure_files后会启动一个web服务自动在默认浏览器中打开测试报告。

展示效果如下
image.png

image.png