• Python 学习之路: 常用断言汇总


             

            最近学习Python,在代码测试阶段用学到断言功能,断言方法用于检查你认为应该满足的条件是否确实满足。如果该条件确实满足,即可确信程序行为没有错误,否则,条件并不满足,将引发异常错误。

    unittest.TestCase 中常用的6个断言:

    assertEqual(a,b)     核实 a==b
    assertNotEqual(a,b)   核实 a!=b
    assertTrue(x)         核实 x=True 
    assertFalse(x)        核实 x=False
    assertIn(item,list)   核实 item in list 
    assertNotIn(item,list)  核实 item not in list

    1.状态 断言

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

    2.json断言

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

    3.list结构里的json断言

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

    4.jsonpath断言

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

    5.assert_that断言

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

    6.post_xml断言

    1. def test_post_xml(self):
    2. xml = """6"""
    3. headers = {"Content-Type": "application/xml"}
    4. r = requests.post('https://httpbin.testing-studio.com/post', data=xml,headers=headers).text
    5. print(r.text)

    7.files断言

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

    8.header断言

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

    9.cookie断言

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

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    location rewrite
    Parallel 与 ConcurrentBag<T> 这对儿黄金搭档(C#)【并发编程系列_2】
    Linux中Java图片生成中文乱码问题解决
    【SpringBootStarter】自定义全局加解密组件
    【M365运维】很抱歉、无法打开“https://..../.xlsx”
    C Primer Plus(6) 中文版 第10章 数组和指针 10.8 变长数组(VLA)
    Python学习:获取对象信息
    PHP代码审计系列(一)
    【服务器】Java连接redis及使用Java操作redis、使用场景
    【C++】STL简介 | STL六大组件 | string类 | string类对象操作
  • 原文地址:https://blog.csdn.net/UniMagic/article/details/126857193