Detailed description of the Python operator Style

Source: Internet
Author: User

Calling a method affects readability. Although the assertEqual () method name can indicate whether the two values are equal, the Code still does not seem like a comparison. for developers familiar with Python operators, it is not as clear as the Python operators.

Second, as we will see in the third article in this series, the new test framework now knows how to check the assert statements to find out the conditions that cause the test to fail. This means that simple assert statements can now generate meaningful test failure messages.

It provides information similar to the results of calling old methods such as assertEqual. Finally, even if assertEqual () is still necessary, it is simpler to import this function from the test module instead of making the function available through class inheritance. It is more in line with the Python operator style.

In fact, as shown below, when py. to provide more test and nose routines that support testing, they only need to define these routines as functions, and then users can import these functions into their own code. Of course, if the author does need to cache it through a routine.

 
 
  1. # nose.tools support functions for writing tests  
  2.  
  3. assert_almost_equal(first, second, places=7, msg=None)  
  4. assert_almost_equals(first, second, places=7, msg=None)  
  5. assert_equal(first, second, msg=None)  
  6. assert_equals(first, second, msg=None)  
  7. assert_false(expr, msg=None)  
  8. assert_not_almost_equal(first, second, places=7, msg=None)  
  9. assert_not_almost_equals(first, second, places=7, msg=None)  
  10. assert_not_equal(first, second, msg=None)  
  11. assert_not_equals(first, second, msg=None)  
  12. assert_true(expr, msg=None)  
  13. eq_(a, b, msg=None)  
  14. ok_(expr, msg=None) 

However, if programmers want to write simple test code and do not want to consider the mess involved in doctest, it is a good way to test functions. In short, the test function greatly enhances the simplicity of writing a test. Programmers do not need to remember, override, or copy the previously written test code. The new Convention allows Python programmers to write a test like a general Python OPERATOR: simply open an empty file and enter it!

Both the py. test and nose frameworks provide special routines that simplify the writing of tests. It can be considered that they provide a convenient test "dialect", which can be used to compile the test. This simplifies the compilation of the test and reduces errors, and makes the test shorter and more readable.

However, using these routines can also lead to an important consequence: your test is bundled with the framework that provides the function, thus losing compatibility. Therefore, consider convenience and compatibility. If you only use the clumsy standard Python operator unittest module to write all tests from scratch, they can run in any testing framework.

Furthermore, if you use a simple method to compile the test function as described above), the test can be run at least in py. test and nose. However. If you start to use the features specific to a testing framework, if another framework develops new important features in the future, you decide to migrate the framework, and you must rewrite the test.

Both py. test and nose provide replacements for TestCase's assertRaises () method. The version provided by py. test is novel. It can also accept the string to be executed, which is more powerful because it can test the expression that causes an exception, not just the function call:

 
 
  1. # conveniences.py  
  2. import math  
  3.  
  4. import py.test  
  5. py.test.raises(OverflowError, math.log, 0)  
  6. py.test.raises(ValueError, math.sqrt, -1)  
  7. py.test.raises(ZeroDivisionError, "1 / 0")  
  8.  
  9. import nose.tools  
  10. nose.tools.assert_raises(OverflowError, math.log, 0)  
  11. nose.tools.assert_raises(ValueError, math.sqrt, -1)  
  12. # No equivalent for third example! 

When dealing with floating point numbers, if you want the test to be able to flexibly implement the Python operator and allow small errors in the processing of floating point numbers, it is particularly meaningful to check the approximate values above.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.