zoukankan      html  css  js  c++  java
  • run测试用例函数写法.py

    import pytest
    import requests

    #定义预期结果:
    expect = {"title":"V2EX"}

    def test_case_01():
    response = requests.get(url='https://www.v2ex.com/api/site/info.json')
    #拿到json数据:
    print(response.json())
    if response.json()["title"] == expect["title"]:
    print("断言成功")
    assert 1
    else:
    print("断言失败")
    assert 0

    def test_case_02():
    response = requests.get(url='https://www.v2ex.com/api/site/info.json')
    #拿到json数据:
    print(response.json())
    if response.json()["title"] != expect["title"]:
    print("断言成功")
    assert 1
    else:
    print("断言失败")
    assert 0

    if __name__ == '__main__':
    #pytest框架自动收集这个脚本里面所有以test开头当成测试用例执行里面的代码
    pytest.main(["-s","run测试用例函数写法.py"])

    """
    结果:
    ============================= test session starts =============================
    platform win32(当前平台信息) -- Python 3.6.6(解释器版本), pytest-5.3.2(pytest版本), py-1.8.1, pluggy-0.13.1(以及其他版本)
    rootdir(根目录): D:s27day68
    collected 1 item(收集了一个用例)

    run.py {'title': 'V2EX', 'slogan': 'way to explore', 'description': '创意工作者们的社区', 'domain': 'www.v2ex.com'}
    断言成功
    .
    ================================== FAILURES(错误信息) ===================================
    ________________________________ test_case_02 _________________________________

    def test_case_02():
    response = requests.get(url='https://www.v2ex.com/api/site/info.json')
    #拿到json数据:
    print(response.json())
    if response.json()["title"] != expect["title"]:
    print("断言成功")
    assert 1
    else:
    print("断言失败")
    > assert 0
    E assert 0
    ============================== 1 passed in 1.01s(用例通过并耗时1秒) ==============================
    """
  • 相关阅读:
    JIRA 6.3.6安装
    Mac安装Protobuf
    Linux 磁盘测速
    rsync快速删除海量文件
    Linux 修改主机名
    查看java进程中哪个线程在消耗系统资源
    redis安装
    springmvc返回中文乱码问题
    java.lang.NumberFormatException: multiple points问题
    谈谈java多线程(一)
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12217536.html
Copyright © 2011-2022 走看看