Skip to main content

Pytest plugin for generating test cases with YAML. In test cases, you can use markers, fixtures, variables, and even call Python functions.

Project description

pytest-yaml-sanmu

Pytest plugin for generating test cases with YAML. In test cases, you can use markers, fixtures, variables, and even call Python functions.

Require

python~=3.12
pytest~=8.2
pyyaml~=6.0
pyyaml-include~=1.3.1

Install

pip install pytest-yaml-sanmu

Config

# pytest.ini

[pytest]
# To execute YAML files starting with "test_"
yaml_run_case = true

Usage

1. Write tests

# test_api.yaml

test: fetch baidu # Each test case has a name
steps:                 # Each test case has multiple steps

  - request:           # steps 1: send request
      method: get
      url: https://www.baidu.com

  - response:          # steps 2: assert response
      status_code: 200
      text: "*baidu*"

2. Write Hook

# conftest.py

import requests
import responses_validator


def pytest_yaml_run_step(item):
    step = item.current_step
    request = step.get('request')
    response = step.get('response')

    if request:
        print(f'url={request["url"]}')
        item.resp = requests.request(**request)

    if response:
        responses_validator.validator(item.resp, **response)

    return True

3. Run pytest

(.venv) ~/pytest-yaml-demo>pytest
================== test session starts ==================
platform win32 -- Python 3.12.2, pytest-8.2.2, pluggy-1.5.0
rootdir: ~/pytest-yaml-demo
configfile: pytest.ini
plugins: allure-pytest-2.13.5, yaml-0.3.0.dev3
collected 1 item                                                                                 

test_api.yaml .                            [100%]

================== 1 passed in 0.22s ================== 

4. More tests

# tests/test_mark_parametrize.yaml

test: parametrize
mark:
  - parametrize:
      - n
      - [1,2,3]
steps:
  - request:
      method: get
      url: https://baidu.com?o=${n}
# tests/test_ddt.yaml
test: ddt
mark:
  - parametrize:
      - n
      - !include ddt_n.yaml
steps:
  - request:
      method: get
      url: https://baidu.com?o=${n}
# tests/test_mark_skip.yaml

test: skip
mark:
  - skip
steps:
  - request:
      method: get
      url: https://www.baidu.com
# tests/test_mark_usefixtures_values.yaml
test: usefixtures
mark:
  - usefixtures:
      - base_url
steps:
  - request:
      method: get
      url: ${base_url}/abc.html

FeedBack

WeChat: python_sanmu

Article

Changelog

dev

v1.0.2(2024-08-16)

  • feat: global_variable_paths_ignore_if_non_existent=TrueIgnore class without exception
  • fix: Remove steps_can_use_vars of configuration
  • chore: Display the default values of ini configuration in help
  • deprecated: Renamed run_yaml_case as yaml_run_case of configuration(will be break in the v1.2.0)
  • docs: add article url

v1.0.0 (2024-07-20)

  • feat: Added a configuration to ignore errors when the variable file does not exist
  • chore: Updated GitHub Actions

v0.5.0 (2024-07-07)

  • feat: Support for defining local variables in YAML
  • feat: Support for defining global variables in YAML and Python
  • feat: Added four configuration items to specify global variable files
  • feat: Support for using Jinja2 in mark
  • fix: Provided try_call function to output undefined when a function is not defined, suppressing errors
  • fix: Provided str filter to convert any variable to a string format compatible with YAML

v0.4.0 (2024-05-04)

  • break: Renamed test_name -> name in yaml file.
  • feat: Allowed saving local variables for items and automatically injecting them into item.current_step. Local variables are cleared before each item execution
  • feat: Used Jinja2 for variable and function loading
  • fix: Fixed self.usefixtures not working
  • chore: Cleaned files before and after compilation
  • docs: Added usage examples (pytest.ini, conftest.py, test_abc.yaml)

v0.3.0 (2024-04-20)

  • feat: Implemented mark.parametrize and mark.usefixture natively, supporting fixture parameterization
  • feat: Compiled to binary files
  • fix: Fixed JSONSchema errors
  • fix: Fixed type errors
  • chore: Updated YAML case schema
  • chore: Locked pytest dependency to ~ =8.1.1
  • chore: Set requires-python = ">=3.12"

v0.2.6 (2024-04-07)

  • feat: Made schema importable as a property
  • feat: Validated YAML against case file requirements using jsonschema
  • chore: Considering removal of Pydantic in the future and providing more detailed error messages
  • fix: Fixed error in schema.yaml
  • chore: Locked pyyaml-include to ~ =1.3.1
  • chore: Used OIDC for publishing to PyPI
  • chore: Updated GitHub Actions version

v0.2.4 (2023-11-29)

  • feat: YAML files support !include, searching from the current file directory instead of cwd #1
  • feat: Import yaml_funcs only upon instantiation to allow reuse by other modules

v0.2.3 (2023-07-03)

  • feat: Plugin is disabled by default; must be enabled via ini configuration
  • feat: Used YAML as the plugin name
  • feat: Display YAML in pytest_report_header instead of yaml-sanmu
  • fix: yaml.dump should not alter content order
  • deps: pydantic>=2.0

v0.2.0 (2023-03-10)

  • feat: Support for user-defined marks
  • feat: Support for pytest marks without parameters
  • feat: Support for pytest marks with parameters (positional only)
  • feat: Support for mark.parametrize
  • feat: Support for mark.usefixtures
  • feat: Built-in templates, supporting function calls
  • feat: Explicitly display YAML content when a test case fails
  • feat: Ability to mark steps with Allure
  • chore: Published to PyPI via GitHub Actions

v0.1.0 (2023-02-08)

  • feat: Recognized YAML files as pytest test cases

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 Distributions

pytest_yaml_sanmu-1.0.2-cp312-cp312-win_amd64.whl (272.6 kB view details)

Uploaded CPython 3.12 Windows x86-64

pytest_yaml_sanmu-1.0.2-cp312-cp312-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

pytest_yaml_sanmu-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pytest_yaml_sanmu-1.0.2-cp312-cp312-macosx_11_0_arm64.whl (280.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pytest_yaml_sanmu-1.0.2-cp312-cp312-macosx_10_9_x86_64.whl (290.0 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

File details

Details for the file pytest_yaml_sanmu-1.0.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pytest_yaml_sanmu-1.0.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 460b7aeb22a959ab41e9d3334f6308e4d34ec72b612d8723a6e56e2dcc509d9d
MD5 2074a4253d98915d5576ebaec3ae8d50
BLAKE2b-256 13343e558141f514c8faaed838bed3b79c7f3828a9566da6bc6a13f100ac80d3

See more details on using hashes here.

File details

Details for the file pytest_yaml_sanmu-1.0.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pytest_yaml_sanmu-1.0.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f1952be70988efb0abd4b35093e2870edd733f7e5022d3e72db6c9fe56985db5
MD5 e035d91e6d32d9ab26fe5de3078e89f7
BLAKE2b-256 d40624b09ad2ec1bf9bba28b62f52953af087c8a3fc36fbd31a80b33c156df46

See more details on using hashes here.

File details

Details for the file pytest_yaml_sanmu-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytest_yaml_sanmu-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ddf666a98f75b296973bc93d208fbfe544267bc211600a403eac6b6066c3d7b1
MD5 62157afe1ad496434f5841051851ee55
BLAKE2b-256 f9ab3ba865b085060e5a43efc7e5e9270455b347549f268bb9245c268143e117

See more details on using hashes here.

File details

Details for the file pytest_yaml_sanmu-1.0.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pytest_yaml_sanmu-1.0.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7998c227cc9594a169fbf171151a1b7c913c799a35b8d31b6d25ba0811d6def5
MD5 80b1b53f1299f5114d46bc3e25ee1929
BLAKE2b-256 2917e4a2ffaac2f44058da06461e9bba2cab7f8e994298d524f98d24b885a2d1

See more details on using hashes here.

File details

Details for the file pytest_yaml_sanmu-1.0.2-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pytest_yaml_sanmu-1.0.2-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 eaf21d3843c38c3cbe5542804a888955e830814b3044707326a5cfa320f70cb9
MD5 30afdc9aa37b27b197ef09249ed7e492
BLAKE2b-256 9f7ef8c81b209227f8b7422861b962e2764765c53abd61e9e83e885dc3db8b2e

See more details on using hashes here.

Supported by

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