First come humorous digression:
In modern programming development, TDD (test-driven development) is becoming more and more popular (PS:DDD (Domain Driven development) is also, but the two do not conflict, just like the process-oriented and object-oriented). As the fundamental of TDD-unit testing is becoming more and more important, unit tests can test the correctness of the code, such as the following C # code:
1 Public class Helper 2 {3 Public string Removehtmlmarkup (string str)4 {5 return str; 6 }7 }
The requirement is to remove the HTML markup from the string. Of course now this is wrong and will only output as is.
Next, write the test code. (Use of NUnit)
1 [Testfixture]2 Public classhelpertest3 {4 [Test]5 Public voidRemovehtmlmarkuptest ([Values ("")]stringstr)6 {7 varOutput =NewHelper (). Removehtmlmarkup (str);8Assert.false (output. Contains (""));9 }Ten}
It must not have passed now. Modify the original code, to remove the "
Of course This example may not be very good, just take a look.
All right, let's get back to the question and return to Python.
First of all, what I can't think of is that Python has assertions and rises to the level of a first-class citizen-the keyword. The keyword used is assert. The syntax is simple, unlike the Assert in C # (either Msunit or NUnit).
1 var = input ('please input 1:')2assert' 1 ','youmust input 1'
The Assert keyword is followed by two or one parameter. The first parameter is a Boolean value, and if the Boolean value is false, the assertion does not pass, throws a Assertionerror error, the second argument is the wrong description, or it can be no code, but generally it is the code.
Well, it's just a few lines, it's gone. Python is so concise.
Python learns assertions in -41.python