Python中的asrt用法

更新时间:2023-07-14 12:53:35 阅读: 评论:0

Python中的asrt⽤法
Python中的asrt⽤法
本⽂转载⾃,如有侵权,⽴马删除!
中日词典使⽤asrt断⾔是学习Python⼀个⾮常好的习惯,Python asrt 断⾔ 语句格式及⽤法很简单;在没完善⼀个程序之前,我们不知道程序在哪⾥会出错,与其让它在运⾏时崩溃,不如在出现错误条件时就崩溃,这时候就需要asrt断⾔的帮助;本⽂主要是将asrt断⾔的基础知识。
Python asrt 断⾔的作⽤
tconPython asrt 断⾔是声明其布尔值必须为真的判定,如果发⽣异常就说明表达式为假。可以理解 asrt断⾔语句为rai-if-not,⽤来测试表达式,其返回值为假,就会触发异常。
asrt 断⾔语句的语法格式
asrt Python怎么⽤?
expression asrt 表达式
下⾯做⼀些asrt⽤法的语句供参考:
asrt1==1
asrt2+2==2*2
asrt len(['my boy',12])<10
asrt range(4)==[0,1,2,3]
如何为asrt断⾔语句添加异常参数
asrt的异常参数,其实就是在断⾔表达式后添加字符串信息,⽤来解释断⾔并更好的知道是哪⾥出了问题。格式如下:
asrt expression[,arguments]
asrt表达式[,参数]
picket fences
asrt len(lists)>=5,'列表元素个数⼩于5'
asrt2==1,'2不等于1'
Python asrt 为何不尽如⼈意
Python中的断⾔⽤起来⾮常简单,你可以在asrt后⾯跟上⼈意判断条件,如果断⾔失败则会抛出异常。
asrt 1+1 == 2
asrt isinstance('Hello', str)
asrt isinstance('Hello', int)
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
AsrtionError
其实asrt看上去不错,然⽽⽤起来并不友好;就⽐如有⼈告诉你程序错了,但是不告诉哪⾥错了;很多时候这样的asrt还不如不写;直接抛⼀个异常会更好⼀些。
改进⽅案 #1
s ='nothin is impossible.'
key ='nothing'
asrt key in s, "key: '{}' is not in Target: '{}'".format(key, s)
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
AsrtionError: key: 'nothing' is not int Target: 'nothin is impossible.'scalar
看上去还⾏,但是其实很是有点不友好;假如你是⼀名测试员,有成千上万的测试案例需要做断⾔做验证,这样的做法就不适⽤了。
改进⽅案 #2
不管你是做测试还是开发的,想必听过不少测试框架;
import pytest
def test_ca():
expected ='Hello'
actual ='hello'
asrt expected == actual
if __name__ =='__main__':
幼儿英语教学视频pytest.main()
执⾏结果如下:
=============================test ssion starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
rootdir: E:\Python5\newmallpro\TestAPI
however
collected 0 items / 1 errors
=================================== ERRORS ====================================
________________________ ERROR collecting test_one.py _________________________
ImportError while importing test module 'E:\Python5\newmallpro\TestAPI\test_one.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_one.py:10: in<module>
import requests
E  ModuleNotFoundError: No module named 'requests'
Interrupted: 1 errors during collection
=========================== 1 error in 0.14 conds ===========================
unittest(单元测试)
Python⾃带的unittest单元测试框架就有了⾃⼰的断⾔⽅法lf.asrtXXX(),⽽且不推荐使⽤asrt XXX语句。
import unittest
class TestStringMethods(unittest.TestCa):
def test_upper(lf):
lf.asrtEqual('foo'.upper(),'FoO')
if __name__ =='__main__':
unittest.main()
执⾏结果如下:
FoO != FOO
Expected :FOO
Actual  :FoO
<Click to e difference>
Traceback (most recent call last):pasrby是什么意思啊
File "D:\appinstallation\pycharminstall\PyCharm 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals
old(lf, first, cond, msg)
File "D:\appinstallation\pythonInstall\lib\unittest\ca.py", line 839, in asrtEqual
asrtion_func(first, cond, msg=msg)
File "D:\appinstallation\pythonInstall\lib\unittest\ca.py", line 1220, in asrtMultiLineEqual
lf.fail(lf._formatMessage(msg, standardMsg))
File "D:\appinstallation\pythonInstall\lib\unittest\ca.py", line 680, in fail
rai lf.failureException(msg)
AsrtionError: 'FOO'!='FoO'
ptest
ptest中的断⾔可读性很好,⽽且智能提⽰也很⽅便你通过IDE轻松完成各种断⾔语句。感谢Karl⼤神写了这个⼀个测试框架。
from ptest.decorator import*
from ptest.asrtion import*
@TestClass()
class TestCas:
暗黄皮肤如何美白
@Test()
新文道def test1(lf):
actual ='foo'
expected ='bar'
asrt_that(expected).is_equal_to(actual)
if __name__ =='__main__':
TestCas().test1()
执⾏结果如下:
AsrtionError: Unexpectedly that the str <bar> is not equal to str <foo>.
改进⽅案 #3
很多⼈对Python中的断⾔表⽰不满⾜,所以⼤家都争相发明⾃⼰的asrt包。在这⾥我强烈推荐asrtpy这个包,
它异常强⼤⽽且好评如潮。
pip install asrtpy
代码⽰例:
from asrtpy import asrt_that
def test_something():
asrt_that(1+2).is_equal_to(3)
asrt_that('foobar').is_length(6).starts_with('foo').ends_with('bar')
asrt_that(['a','b','c']).contains('a').does_not_contain('x')
asrt_that('a').is_length(4)
if __name__ =='__main__':
test_something()
执⾏结果如下:
AsrtionError: Expected <a> to be of length <4>, but was <1>.
从他的⽂档上你会发现它⽀持了⼏乎你能想到的所有所有测试场景,包括但不限于⼀下列表:
Strings
Numbers
Lists
Tuples
Dicts
Sets在线汉英互译
Booleans
Dates
Fiels
Objects
⽽且它的断⾔信息简洁明了,不多不少。
Expected <foo> to be of length <4>, but was <3>.
Expected <foo> to be empty string, but was not.
Expected <Fal>, but was not.
Expected <foo> to contain only digits, but did not.
Expected <123> to contain only alphabetic chars, but did not.
Expected <foo> to contain only upperca chars, but did not.
Expected <FOO> to contain only lowerca chars, but did not.
Expected <foo> to be equal to <bar>, but was not.
Expected <foo> to be not equal to <foo>, but was.
Expected <foo> to be ca-innsitive equal to <BAR>, but was not.
总结
断⾔在软件中有⾮常重要的作⽤,写的好可以让你的系统更稳定,也可以让你有更多真正⾯对对象的时间,⽽不是在调试代码。
Python中默认的断⾔语句其实还有⼀个作⽤,如果你写了⼀个类型相关的断⾔,IDE会把这个对象当成这种类型,这时候智能提⽰就有如神助。
要不要把内置的断⾔语句换成可读性更好功能更强⼤的第三⽅断⾔,完全取决于实际情况。⽐如你真的需要验证某个东西并且很关⼼验证结果,那么必须不能⽤简单的asrt;如果你只是担⼼某个点可能有坑或者让IDE认识某个对象,⽤内置的asrt既简单⼜⽅便。
本⽂转载⾃,如有侵权,⽴马删除!

本文发布于:2023-07-14 12:53:35,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/177159.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:语句   测试   表达式   框架   对象   需要   程序
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图