pytest-yaml-sanmu 1.1.3
Pytest plugin for generating test cases with YAML. In test cases, you can use markers, fixtures, variables, and even call Python functions.
Requires Python: >=3.12

# pytest-yaml-sanmu

pytest-yaml-sanmu is a plugin for `pytest`_ that  considers YAML files as test case.

In this way, we can use markers, fixtures, variables, and even call Python functions in YAML.



For more information, please [click here](https://pytest-yaml-doc.pages.dev/) .





## Require

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






## Install
```
pip install pytest-yaml-sanmu
```



## Config

```ini
# pytest.ini

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



## Usage

### 1. Write tests

```yaml
# test_api.yaml

name: 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

```python
# 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 ================== 
```





## FeedBack

WeChat: `python_sanmu`





## Article

-   [pytest-yaml-sanmu(一):把yaml作为测试文件的pytest插件](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484510&idx=1&sn=0b4ef14731fab2ff887b131e8e6700fa)
-   [pytest-yaml-sanmu(二):使用hook自定义yaml用例的执行方式](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484511&idx=1&sn=120ba045f41a75f463ca800afb5fd488)
-   [pytest-yaml-sanmu(三):使用yaml进行API测试和Web测试](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484512&idx=1&sn=37648e7e5e61898fecf9f5f59710392d)
-   [pytest-yaml-sanmu(四):标记和筛选YAML用例](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484600&idx=1&sn=3123430b2cfbb3595a597251a5eb36d5)
-   [pytest-yaml-sanmu(五):跳过执行和预期失败](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484601&idx=1&sn=3e1448d3881ea2f93d9a58419dba4101)
-   [pytest-yaml-sanmu(六):YAML数据驱动测试](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484645&idx=1&sn=849132be19c411fb3ac6626f4e5aee74)
-   [pytest-yaml-sanmu(七):使用fixture返回值](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484646&idx=1&sn=a81548aeb463f4c1e2ed2195994defc6)
-   [pytest-yaml-sanmu(八)：定义和使用变量](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484697&idx=1&sn=1ae1a56e99d1144f3cbc6e9ad15b5d8a)
-   [pytest-yaml-sanmu(九)：注册和调用Python函数](https://mp.weixin.qq.com/s?__biz=MzkxMDIyODUwOA==&mid=2247484704&idx=1&sn=ba3fd21bc21bed6296a716d7a2954299)