Skip to main content

An easy-to-use testing framework based on pytest

Project description

Lamber

Lamber 收集 pytest 执行数据(models),提供消费接口(hooks),并允许用户集成自定义数据。

安装

pip install lamber

依赖

  • python >= 3.10
  • pytest >= 8.3.5

基于 Sqlite3 的测试报告

测试报告是一种典型的媒介,可以更加直观的展示 Lamber 的功能。Lamber 支持将测试执行数据保存到 Sqlite3 数据库文件中,通过 --lamber-sqlite-dir 命令行选项指定数据库文件的目录,数据库文件名为 lamber.db

lamber-report 是基于 Lamber 开发的测试报告,后续讨论将基于此展开。 lamber-report

对原生 pytest 脚本的支持

一个典型的 pytest 脚本如下:

import pytest


@pytest.fixture(scope="module")
def module_fixture():
    print("Setup module env")
    yield
    print("Clean module env")


@pytest.fixture(scope="function", autouse=True)
def function_fixture(module_fixture):
    print("Setup function env")
    yield
    print("Clean function env")


def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5
  • 包含 module 和 function 级别的 fixture。

生成的测试报告主体如下:

test_sample.png

  • 包含 Step Tree、Traceback、Sourcecode、Logs 等信息。
  • 其中 pytest fixture 的调用关系会在 Step Tree 中展示。可以通过 --lamber-ignore-fixture-step 选项关闭此功能。

对原生 pytest 脚本的改造

Lamber 引入 Step 的概念,将测试执行的过程进一步细分,支持上下文管理器with lamber.Step:)和装饰器@lamber.step)两种方式声明步骤。

改造上述脚本:

import pytest

import lamber


@pytest.fixture(scope="module")
def module_fixture():
    with lamber.Step("Setup module env"):
        print("Setup module env")

    yield

    with lamber.Step("Clean module env"):
        print("Clean module env")


@pytest.fixture(scope="function", autouse=True)
def function_fixture(module_fixture):
    with lamber.Step("Setup function env"):
        print("Setup function env")

    yield

    with lamber.Step("Clean function env"):
        print("Clean function env")


# Step decorator
@lamber.step("Plus one")
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

生成的测试报告主体如下:

test_sample_with_step.png

  • 在 Step Tree 上会有进一步细化。

支持收集自定义环境信息

Lamber 支持通过 pytest_lamber_report_environment 方法集成自定义的环境信息,例如:目标测试版本,备注信息等。

# conftest.py

def pytest_lamber_report_environment() -> dict:
    return {
        "target version": "version 1.0.0",
        "comment": "custom comment",
    }

在测试报告中的体现如下:

report_environment_items

LICENSE

MIT LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lamber-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file lamber-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lamber-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.17

File hashes

Hashes for lamber-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 427f0e81f26520e5d636f39623e8812e9ae09a69593ccf10f75f81844546a6f1
MD5 fdd684da95762ecd38090da7e139820f
BLAKE2b-256 9db5b32c10b11b87b9578fbbaa26dd6734d5a9bff48155241bcdab269d67f950

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page