In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the testing tools in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When we are writing a program, we need to use tests to verify whether there are errors or problems in the program, but it is cumbersome to write a large number of tests to make sure that every detail of the program is all right. In Python, we can use some standard modules to help us automate the testing process, such as:
Unittest: a general testing framework
Doctest: a simpler module, designed for checking documents, but also ideal for writing unit tests.
Below, the author will briefly introduce the application of these two modules in testing.
Doctest
The doctest module searches for code snippets that look like python interactive sessions, and then attempts to execute and validate the results. Let's take doctest.testmod as an example. The function doctest.testmod reads all the document strings in the module, looks for examples that appear to have been extracted from the interactive interpreter, and then checks to see if they reflect the actual situation.
Let's first create the sample code file test_string_lower.py. The complete code is as follows:
#-*-coding: utf-8-*-def string_lower (string):''returns the lowercase of a string: param string: type: str: return: the lower of input string > > string_lower (' AbC') 'abc' > string_lower (' ABC') 'abc' > string_lower (' abc') 'abc'' 'return string.lower () if _ _ name__ = ='_ _ main__': import doctest Test_string_lower doctest.testmod (test_string_lower)
First of all, the program is explained. The function string_lower is used to return the lowercase of the input string. The comments in the function contain a total of three test cases, which are expected to contain as many test cases as possible. Then import doctest and test_string_lower in the main function, and then run the testmod function in doctest to test.
Then we started testing. First, type python test_string_lower.py on the command line, and you'll find nothing output when you run it, but this is actually a good thing, indicating that all the tests in the program have passed! What if we want to get more output? You can add the parameter-v when you run the script, when the command becomes python test_string_lower.py-v, and the output is as follows:
Trying: string_lower ('AbC') Expecting:' abc' ok Trying: string_lower ('ABC') Expecting:' abc' ok Trying: string_lower ('abc') Expecting:' abc' ok 1 items had no tests: test_string_lower 1 items passed all tests: 3 tests in test_string_lower.string_lower 3 tests in 2 items. 3 passed and 0 failed. Test passed.
As you can see, there is still a lot going on behind the program testing. Then, we try the case of a program error, for example, we accidentally write the return of the function as:
Return string.upper ()
This actually returns the uppercase of the input string, while the example we tested returns the lowercase of the input string, and then runs the script (plus the parameter-v), and the output is as follows:
Failed example: string_lower ('abc') Expected:' abc' Got: 'ABC' 1 items had no tests: test_string_lower * 1 Items had failures: 3 of 3 in test_string_lower.string_lower 3 tests in 2 items. 0 passed and 3 failed. * Test Failed*** 3 failures.
At this point, the program test fails, and it not only captures the bug, but also clearly points out where the error is. It is not difficult for us to modify the program.
Unittest
Unittest is similar to the popular Java testing framework JUnit, it is more flexible and powerful than doctest, and can help you write large and detailed test sets in a structured way.
Let's start with a simple example. First, let's write a my_math.py script with the following code:
#-*-coding: utf-8-*-def product (x, y):'': param x: int, float: param y: int, float: return: X * y 'return x * y
The function of this function is to enter two numbers x, y and return the product of the two numbers. Then comes the test_my_math.py script, with the complete code as follows:
Import unittest, my_math class ProductTestcase (unittest.TestCase): def setUp (self): print ('begin test') def test_integers (self): for x in range (- 10,10): for y in range (- 10,10): P = my_math.product (x, y) self.assertEqual (p, x, y) 'integer multiplication failed') def test_floats (self): for x in range (- 10,10): for y in range (- 10,10): xx = x yy 10 yy = y my_math.product 10 p = my_math.product (x, y) self.assertEqual (p, x * y) 'integer multiplication failed') if _ _ name__ = =' _ main__': unittest.main ()
The function unittest.main is responsible for running the test for you: execute the setUp method before the test method, instantiate all TestCase subclasses, and run all methods whose names start with test. The assertEqual method checks the specified condition (equal here) to determine whether the specified test succeeded or failed.
Next, we run the previous test and the output is as follows:
Begin test .begin test. -Ran 2 tests in 0.001s OK
As you can see, the program runs two tests, and outputs' begin test',. before each test. Indicates that the test is successful, and if the test fails, F is returned.
Then simulate the test error and change the product method in the my_math function to return:
Return x + y
Then run the test script and the output is as follows:
Begin test Fbegin test F = FAIL: test_floats (_ _ main__.ProductTestcase)-Traceback (most recent call last): File "test_my_math.py", line 20, in test_floats self.assertEqual (p, x * y) 'integer multiplication failed') AssertionError:-2.0,1.0: integer multiplication failed = FAIL: test_integers (_ _ main__.ProductTestcase)-Traceback (most recent call last): File "test_my_math.py" Line 12, in test_integers self.assertEqual (p, Xeroy, 'integer multiplication failed') AssertionError:-20! = 100: integer multiplication failed-Ran 2 tests in 0.001s FAILED (failures=2)
Both tests failed, returned F, and helped you point out the error. Next, you should be able to fix the bug quickly.
This is the end of the content of "what are the testing tools in Python"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.