assert assert usage

Use assert assertion is a very good habit to learn python, python assert assert sentence format and language usage is very simple. Before a program is not perfect, we do not know where the program will go wrong, rather than let it run most crashes, it is better to crash when an error condition occurs, this time on the need to assert asserted help. This article is about the basics assert assertion.

python assert assert the role

python assert its assertion is a statement of a Boolean value that must be true determination, if an exception occurs it means that the expression is false. Assert assert statement can be understood as a raise-if-not, to test the expression, it returns false, it will trigger an exception.

assert assertion syntax statement

assert python how to use?
expression assert expression

Do some assert usage for reference the following statement:

==. 1. 1 assert
assert == 2 * 2 + 2 2
assert len ([ 'My Boy', 12 is]) <10
assert Range (. 4) == [0,1,2,3]
How to add the assertion assert abnormal parameters

Abnormal parameters assert, in fact, add the string information in the assertion expression used to explain and assert better know what went wrong. Format is as follows:

expression The Assert [, arguments]
Assert expression [parameters]

assert len (lists)> = 5 , ' less than the number of elements in the list. 5'
Assert 2 == 1, '2 does not equal 1'
Why not satisfactory Python Assert

Python assertions very simple to use, you can keep up with any of the conditions behind the judge assert, if the assertion fails will throw an exception.

>>> assert 1 + 1 == 2
>>> assert isinstance('Hello', str)
>>> assert isinstance('Hello', int)

Traceback (MOST recent Results Last Call):
File "<the INPUT>", Line 1, in <Module>
AssertionError
actually assert looks good, but is not cool with it. For example, some people will tell you the program wrong, but do not tell where wrong. Many times this assert might as well not write, I wanted to write a BS-ing. Direct throw an exception to some of the more happy.

Improvement program # 1

A slightly modified embodiment of a Diudiu necessary information is to be put behind assert statements, such as this.

>>> s = "nothin is impossible."
>>> key = "nothing"
>>> assert key in s, "Key: '{}' is not in Target: '{}'".format(key, s)

Traceback (MOST recent Results Last Call):
File "<the INPUT>", Line 1, in <Module>
AssertionError: Key: 'Nothing' IS not in Target: '. IS Impossible nothin'
looks okay, but in fact written very boring. If you are a test Wang, there are thousands of test cases need to do to make the assertion verification, I believe you are faced with the above approach, there must be ten million heart kind of horses galloping by.

. \ Is continued on the next line of symbols

Guess you like

Origin www.cnblogs.com/joelwang/p/11123036.html