I04_HttpRunner上传图片:upload 方式

I04_HttpRunner上传图片:upload 方式

 

如果进行文件上传,在 HttpRunner 中可以使用两种方式来实现:

  • 通过 requests 模块约定方法上传文件
  • 通过 requests_toolbelt 方式上传文件

 

在 HttpRunner 中,集成了requests_toolbelt 的文件上传功能,可以通过 upload 关键字简化文件的上传。

 

源码如下:httprunner-2.5.5\httprunner\ext\uploader\__init__.py

""" upload test extension.

If you want to use this extension, you should install the following dependencies first.

- requests_toolbelt
- filetype

Then you can write upload test script as below:

    - test:
        name: upload file
        request:
            url: http://httpbin.org/upload
            method: POST
            headers:
                Cookie: session=AAA-BBB-CCC
            upload:
                file: "data/file_to_upload"
                field1: "value1"
                field2: "value2"
        validate:
            - eq: ["status_code", 200]

 

 

使用格式:

  upload:
        : <文件路径及名称>

 

 

编写用例:

- config:
    name: testcase description

- test:
    name: upload
    request:
      url: https://graph.baidu.com/upload
      method: POST
      
      # 实现文件的上传
      upload:
        image: dog.jpg
        
    validate:
      - eq: [status_code, 200]
      - eq: [content.status, 0]
      - eq: [content.msg, Success]

 

 

执行测试用例:

I04_HttpRunner上传图片:upload 方式_第1张图片

 

 

 

 

你可能感兴趣的:(接口自动化测试之,-,HttpRunner框架)