Python learns assertions in -41.python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.