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.
- # nose.tools support functions for writing tests
-
- assert_almost_equal(first, second, places=7, msg=None)
- assert_almost_equals(first, second, places=7, msg=None)
- assert_equal(first, second, msg=None)
- assert_equals(first, second, msg=None)
- assert_false(expr, msg=None)
- assert_not_almost_equal(first, second, places=7, msg=None)
- assert_not_almost_equals(first, second, places=7, msg=None)
- assert_not_equal(first, second, msg=None)
- assert_not_equals(first, second, msg=None)
- assert_true(expr, msg=None)
- eq_(a, b, msg=None)
- 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:
- # conveniences.py
- import math
-
- import py.test
- py.test.raises(OverflowError, math.log, 0)
- py.test.raises(ValueError, math.sqrt, -1)
- py.test.raises(ZeroDivisionError, "1 / 0")
-
- import nose.tools
- nose.tools.assert_raises(OverflowError, math.log, 0)
- nose.tools.assert_raises(ValueError, math.sqrt, -1)
- # 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.
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?