Full Analysis of Python testing problems

Source: Internet
Author: User

Before giving you a detailed introduction to the Python test, let's first take a look at the Python test and then give a comprehensive introduction to the Python test. The Python test is still quite common. So I studied the Python test, I would like to share it with you here and hope it will be useful to you.

As you can see in the first article, both py. test and nose support the compilation of simple functions, which greatly simplifies the compilation of Python testing:

 
 
  1. Traceback (most recent call last):  
  2.   File "test_partial_pickle.py", line 12, in <module> 
  3.     s = pickle.dumps(partial(add, 10))  
  4.   File "/Library/Frameworks/Python.framework/Versions/  
  5.      2.5/lib/python2.5/pickle.py", line 1366, in dumps  
  6.      Pickler(file, protocol).dump(obj)  
  7.   File "/Library/Frameworks/Python.framework/Versions/  
  8.      2.5/lib/python2.5/pickle.py", line 224, in dump  
  9.      self.save(obj)  
  10.   File "/Library/Frameworks/Python.framework/Versions/  
  11.      2.5/lib/python2.5/pickle.py", line 306, in save  
  12.      rv = reduce(self.proto)  
  13.   File "/Library/Frameworks/Python.framework/Versions/  
  14.      2.5/lib/python2.5/copy_reg.py", line 69, in _reduce_ex  
  15.      raise TypeError, "can't pickle %s objects" % base.__name__  
  16. TypeError: can't pickle partial objects 


If you only need to check component behavior in a specific environment, both the test function and the more traditional test class can meet the needs. However, if you want to perform a series of tests, they are basically the same, and only some parameters are different, what should we do?

To facilitate this test, do not copy and paste the test function multiple times and set a unique name. Both py. test and nose support derivative test ). The idea here is that you provide a test function, which is actually an iterator, then use its yield statement and provide call parameters to return a series of functions. For example, if you want to run a test on a series of Web browsers, you can write the following code:

For derivative Python testing, py. test is more convenient. Therefore, you can easily identify ongoing tests and understand the test report when one or more tests fail. The first item in each tuples can be a name, it is output as part of the test Name:

 
 
  1. Traceback (most recent call last):  
  2.   File "test_partial_pickle.py", line 12, in <module> 
  3.     s = pickle.dumps(partial(add, 10))  
  4.   File "/Library/Frameworks/Python.framework/Versions/  
  5.      2.5/lib/python2.5/pickle.py", line 1366, in dumps  
  6.      Pickler(file, protocol).dump(obj)  
  7.   File "/Library/Frameworks/Python.framework/Versions/  
  8.      2.5/lib/python2.5/pickle.py", line 224, in dump  
  9.      self.save(obj)  
  10.   File "/Library/Frameworks/Python.framework/Versions/  
  11.      2.5/lib/python2.5/pickle.py", line 306, in save  
  12.      rv = reduce(self.proto)  
  13.   File "/Library/Frameworks/Python.framework/Versions/  
  14.      2.5/lib/python2.5/copy_reg.py", line 69, in _reduce_ex  
  15.      raise TypeError, "can't pickle %s objects" % base.__name__  
  16. TypeError: can't pickle partial objects 

A major problem when designing and Writing Test suites is how to deal with the common setup and teardown code. Many real tests are not as simple as the example functions provided in this article; they must perform complex operations, such as opening a web page in the Python test, clicking the "Continue" button, and then checking the results. Before you start a test, such as opening the page and clicking the button, you must complete the test.

Now, consider what if the test is performed in all the one hundred functional tests. Both of them need to run Firefox by calling the common setup routine before they can execute their own tests. In response, teardown may be performed to cancel setup. A major problem when designing and Writing Test suites is how to deal with the common setup and teardown code. Many real tests are not as simple as the example functions provided in this Article;

They must perform complex operations, such as opening a webpage in Firefox, clicking the "Continue" button, and then checking the results. Before you start a test, such as opening the page and clicking the button, you must complete the test.

  1. Python source code compilation skills
  2. Simple and Easy-to-use Python tools
  3. Introduction to the Python application field
  4. Python Android Object-Oriented Programming-Python applications
  5. How to Use the Python module to parse the configuration file?

Related Article

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.