Python Learning note-unit test, UnitTest

Source: Internet
Author: User
The so-called unit test, is a module, a function, or a class for the correctness of testing a kind of work.


The test-driven development approach is called Test-driven development (testing drived development). This development model is not just testing, but a means of decomposing the requirements and ensuring the quality of the code.


Unit testing does not only ensure that functions, modules, or classes function in accordance with our requirements when testing. At the same time, it is also helpful for us to change the class or function in the future, we just need to run the unit test again to ensure that our modification has no effect on the function.


Of course, unit tests can also make the code much more significant.


Writing unit test code requires the introduction of Python's UnitTest package. We can create a new unit test class, but this class needs to inherit Unitest. TestCase class. The test method in the test class must be at the beginning of tests, otherwise it will not be executed. So we can use Unitest. The method in TestCase. The common methods are:


Assertequal (self, First, second) # to determine if it is an expected value
Asserttrue (BOOL) # to determine if it is true
Assertraises (Exception) #判断是非抛出特定异常.

Let's give an example:


We first define a class custom class and then unit test it.


dict.py

Class Dict (Dict):d ef __init__ (Self, **kwargs): Super (Dict, self). __init__ (**kwargs) def __getattr__ (self, item): Try: return self[item]except keyerror:raise attributeerrordef __setattr__ (self, Key, value): self[key] = Valueimport Unittestfrom Dict Import Dictclass dicttest (unittest. TestCase):d EF test_init (self):d = Dict (a=100, b=200) self.assertequal (D.A,) self.assertequal (D.B, 200) Self.asserttrue (Isinstance (d, Dict)) def test_key (self):d = dict () d[' key '] = "+" self.assertequal (d["key"], "+") def Test_attar (self):d = Dict () D.attar = "Attar" self.asserttrue ("Attar" in D) self.assertequal (D.attar, "Attar") def Test_ Keyerror (self):d = Dict () with Self.assertraises (keyerror): value = d[' notexisted ']def test_attarerror (self):d = Dict () With Self.assertraises (attributeerror): value = d.not_existedif __name__ = = ' __main__ ': Unittest.main ()

Since we have defined a subclass of dict, we have to test its key and value, and it is a class, so we also need to test its attribute.


We define the unittest. TestCase sub-class, need to write two sentences:

if __name__ = = ' __main__ ': Unittest.main ()

We can execute it just like a normal python file.


In addition, we can execute multiple test in the following ways


Python-m UnitTest Dicttest
SetUp and teardown functions.

The setup and Teardown functions are two special functions. Setup runs before all unit tests start; the teardown function runs after all unit tests are run. The scenarios for these two functions are: for example, if you need to connect to a database, you can start the connection in the Setup function and close the connection in the Teardown function.

The above is the Python Learning Note-unit test, unittest content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.