美文网首页
python断言

python断言

作者: 吱吱菌啦啦 | 来源:发表于2022-04-11 16:00 被阅读0次

Q:什么是断言?
A:我理解的断言就是一个命题,在程序执行中,命题为真则通过,命题为假则不通过。

关于assert用法,参考菜鸟教程:https://www.runoob.com/python3/python3-assert.html

总结以下9种python断言方式

1.状态

def test_get(self):
    r = requests.get('https://httpbin.testing-studio.com/get')
    print(r.next)
    print(r.json())
    print(r.status_code)
    assert r.status_code == 200

2.json

def test_post_json(self):
    payload = {
        "level": 1,
        "name": "zizi"
    }
    r = requests.post('https://httpbin.testing-studio.com/post', json=payload)
    print(r.text)
    assert r.json()['json']['level'] == 1

3.list结构里的json

def test_hogwarts_json(self):
    r = requests.get('https://home.testing-studio.com/categories.json')
    print(r.text)
    assert r.status_code == 200
    print(jsonpath.jsonpath(r.json(), '$..name')) #打印出所有的name
    assert r.json()['category_list']['categories'][0]['name'] == "开源项目" #json断言

4.jsonpath

def test_hogwarts_json(self):
    r = requests.get('https://home.testing-studio.com/categories.json')
    print(r.text)
    assert r.status_code == 200
    print(jsonpath.jsonpath(r.json(), '$..name')) #打印出所有的name
    assert jsonpath.jsonpath(r.json(),'$..name')[0] == "开源项目" #jsonpath断言

5.assert_that

def test_hamcrest(self):
    r = requests.get('https://home.testing-studio.com/categories.json')
    assert_that(r.json()['category_list']['categories'][0]['name'], equal_to("开源项目"))   #assert_that

6.post_xml

def test_post_xml(self):
    xml = """<xml version='1.0' encoding='utf-8'><a>6</a>"""
    headers = {"Content-Type": "application/xml"}

    r = requests.post('https://httpbin.testing-studio.com/post', data=xml,headers=headers).text
    print(r.text)

7.files

def test_files(self):
    url = 'https://httpbin.testing-studio.com/post'
    files = {'file': open('report.xls', 'rb')}
    r = requests.post(url, files=files)
    print(r.text)

8.header

def test_header(self):
    headers = {'user-agent': 'my-app/0.0.1'}
    r = requests.get('https://httpbin.testing-studio.com/get', headers=headers)
    print(r.text)
    assert r.json()['headers']["User-Agent"] == "my-app/0.0.1"

9.cookie

def test_cookie(self):
    cookies = dict(cookies_are='working')
    r = requests.get('https://httpbin.testing-studio.com/get', cookies=cookies)
    print(r.text)
    assert r.json()['headers']["Cookie"] == "cookies_are=working"

相关文章

  • Assert断言

    Assert断言 环境 Python3.7.0 macOS High Sierra 10.13.6 python ...

  • Python断言

    assent 断言当该关键字后面的条件为假,程序自动崩溃并抛出AssertionError异常 举例:

  • 「Python」断言

    断言用于判断一个表达式,在表达式的结果为False的时候触发异常。 语法 assert 条件判断表达式[, 异常抛...

  • python断言

    Q:什么是断言?A:我理解的断言就是一个命题,在程序执行中,命题为真则通过,命题为假则不通过。 关于assert用...

  • Python 中的 assert() 使用说明

    通常每一门编程语言都有自己断言语句,以方便出错调试,Python 自然也不例外。 Python 中的断言是 ass...

  • Python3 unittest断言详解

    Python3 unittest断言详解 unittest中断言主要有三种类型: 1.基本的布尔断言,即:要么正确...

  • Pytest系列15 -多重校验插件之pytest-assume

    一、前言 pytest中可以用python的assert断言,也可以写多个断言,但一个失败,后面的断言将不再执行 ...

  • python中的断言如何使用

    python中的断言使用python的童鞋一定知道,但是什么时候使用,可能不是很清楚,我们就这个如何合适的使用断言...

  • Python Debug

    1. 异常: 1.1 抛出异常 使用raise关键字抛出异常 1.2 捕获异常 2. 断言 禁用断言python ...

  • Python中不尽如人意的断言Assertion

    原文出处: cicaday Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言...

网友评论

      本文标题:python断言

      本文链接:https://www.haomeiwen.com/subject/ozevsrtx.html