Skip to main content

HAR2tavern

tavern 是一个pytest插件,用来进行接口自动化测试,通过YAML来描述和管理测试用例。 尽管YAML已经十分简洁,但是的接口数量很多时,创建YAML文件依然耗时。

本工具将通过抓包获取到的HAR文件,快速生成tavern所支持的YAML文件,实现快速进行接口自动化的目的。

当然,你也可以对生成的YAML进行修改,以实现复杂业务的测试。

安装

pip install har2tavern

使用

  1. 基本用法

    >har2tavern file.harhar2
    
    Output file: ./test_api_1.tavern.yaml
    Output file: ./test_api_2.tavern.yaml
    Output file: ./test_api_3.tavern.yaml
    Output file: ./test_api_4.tavern.yaml
    Output file: ./test_api_5.tavern.yaml
    Output file: ./test_api_6.tavern.yaml
    Output file: ./test_api_7.tavern.yaml
    Successfully generated 7 files
    

    本命令将生成多个yaml格式的测试用例

  2. 更多参数

    >har2pytest --help
    Usage: har2pytest [OPTIONS] HARFILE
    根据har文件,生成yaml格式的接口自动化测试用例
    
    Options:
      -H, --host TEXT            只为指定的host生成用例
      -S, --single-file BOOLEAN  将用例合并到单个文件
      --help                     Show this message and exit.
    
  3. 生成requests代码

    >har2requests file.harhar2
    
    import requests
    
    s = requests.Session()
    resp_1 = s.request(
        **{
            "method": "POST",
            "url": "http://127.0.0.1:8000/todo",
            "headers": {
                "Host": "127.0.0.1:8000",
                "Connection": "keep-alive",
                "Accept": "application/json, text/plain, */*",
                "Authorization": "Bearer 0OWgIous2pQjm2Z4aiT2X0HCt3BnuJnp6fVMVv0TpMpEfW8xL53UkzCFJcAlMEaB",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
                "Content-Type": "application/json;charset=UTF-8",
                "Origin": "http://127.0.0.1:8000",
                "Sec-Fetch-Site": "same-origin",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Dest": "empty",
                "Referer": "http://127.0.0.1:8000/static/index.html",
                "Accept-Encoding": "gzip, deflate, br",
            },
            "json": {"title": "2222", "is_done": False},
        }
    )
    

开发

  1. 获取源代码

    git clone https://github.com/dongfangtianyu/har2tavern.git
    cd har2tavern
    
  2. 创建venv,并安装依赖

    python -m venv venv
    source ~/venv/bin/activate
    pip install -r requirements.txt
    pre-commit install
    
  3. 执行测试、 执行检查

    pytest && pre-commit run --all-files
    

    在写代码之前,要确定你获取的代码是没有问题的,所有先执行一次测试

  4. 开发代码

    coding....
    

    现在,你可以尽情的编码了。

  5. 执行测试 、 执行检查

    特别提醒你在提交代码之前,再次进行测试和检查

    pytest && pre-commit run --all-files
    
  6. 打包

    python setup.py sdist
    

    执行该命令将在 ./dist目录下中生成安装包,可以分发、安装

贡献

如果你对本工具有更好的想法,欢迎发起PR https://github.com/dongfangtianyu/har2tavern

我们希望PR中有修改的描述、测试用例,并且所有用例全部测试通过。

Release files for har2tavern 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for har2tavern 0.0.2
File Size Uploaded
har2tavern-0.0.2.tar.gz 5.4 kB Details

Release files / har2tavern-0.0.2.tar.gz

Download URL har2tavern-0.0.2.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4f3a572f394a0c43702e2d39748f16be1c5d9ec46f1eaa1845673cc0e22e44c7
BLAKE2b-256 checksum
How to use checksums
8fa155f8de8b5cd2e3db355aff6c0d180be49aee5aa46b9677ed11d06f7128cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page