(Thirty-six) recognize Unittest unit test unit testing framework unittest- awareness

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install any additional information please search on their own here do not introduced, all examples are performed using python3.6 + selenium.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

For those unfamiliar with the test to become a novice in terms of unit testing is a topic on sound tall, seemingly only advanced test or developer to do the job. In fact, it is not imagine so advanced, we are slowly opened its mysterious veil by example.

Readers may there is a doubt, we are not in school Web automate it? Why go learning unit testing framework, in fact, unit testing framework not only for code-level testing, unit testing framework is concerned, I believe that it is mainly the following three things.

Organization provides use cases and implementation: When you only a few test cases, do not have to consider organizing use cases, however, when hundreds of thousands of test cases to reach a large number of test piled together, it creates scalability and maintenance problems, and the organization of this time specification needs to consider the use cases. Unit testing framework that is used to solve this problem.

The method provides a wealth of comparison: either functional testing or unit testing, the results need to be compared to the actual (asserted) after the completion of use cases performed with expected results so as to determine whether or not performed by use cases. Unit testing framework will provide a wealth of general assertions. For example, it is judged equal / not only comprising / not comprising, True / False assertion like.

Provides a wealth of logs: throw a clear reason for the failure when a test case fails, the execution is complete when all the use cases can provide a wealth of execution results. For example, the total execution time, the number of use cases of failure, the number of cases and the like used successfully.

General unit testing framework will provide these features, these features range from the test frame element of view, it is equally applicable to a Web development and execution Automation embodiment.

 

Understanding unittest

 

What is unit testing? Unit testing is responsible for minimal software design unit (module) for authentication, which uses software design described in the documentation for the module as a guide for important program branches tests to detect errors in the module. In Python There are many languages unit testing framework, such as the doctest , unittest , pytest , nose , etc., unittest frame (formerly PyUnit frame) as Python language comes unit testing framework, Python 2.1 and later versions have unittest as a standard module into Python development Kit.

 

Understanding of unit testing

 

Readers may ask, do not unit test unit test framework to write it? The answer is given pit, unit testing by itself is a piece of code to verify open another section, so do unit testing framework can also write unit tests, the following example demonstrates by not testing framework for unit testing.

 

First create a test-class calculator.py

# Calculator 
class the Count ():
     DEF  __init__ (Self, A, b):
        self.a = int(a)
        self.b = int(b)
    
    # Calculating adder 
    DEF the Add (Self):
         return self.a + self.b,

Very simple procedure, creating a Count calculated for two classes of integers, by __init __ () method to initialize two numbers, then creates add () method returns the result of the sum of two numbers.

According to the above features implemented without testing framework written test but as members test.py

from calculator import Count

# Test addition of two integers 
class TestCount ():
     DEF test_add (Self):
         the try :
            j = Count(2,3)
            add =j.add()
            assert(add == 5),'Integer addition result error!'
        except AssertionError as msg:
            print(msg)
        else:
            print('Test paa!')

# Performing Test Method class 
mytest = TestCount ()
mytest.test_add()

First, the introduction calculator file Conut class; then test_add () call method Count class and pass two parameters 2 and 3 ; the last call Count class add () method for two parameters wigs operation, and by assert () method for determining add () return value is equal to 5. If not equal, "custom Throws Integer addition result error! abnormality information", if equal print " the Test Pass! "

operation result:

 

Not difficult to find that there are many problems with this test. First of all, there is no written test procedures may follow certain norms, a programmer can write always completely different testing procedures, a code is not all maintenance is very troublesome. Secondly, the need to write a lot of code to the auxiliary unit testing, in test.py the code for testing in even more than the code being tested, but this is just a test, a unit of the module, the only written a test case is clearly not enough.

In order for the unit test code easier to maintain and write, the best way is to follow certain norms to write test cases, which is a unit testing framework born in mind. Then tell how unittest write unit test unit test framework.

 

from calculator import Count
import unittest

# Test addition of two integers 
class TestCount (of unittest.TestCase):
     DEF the setUp (Self):
         Print ( ' Test Start ' )
     DEF test_add (Self):
        j = Count(2,3)
        self.assertEqual(j.add(),5)
    def tearDown(self):
        print("test end")

# Performing Test Method class 
IF  the __name__ == ' __main__ ' :
    unittest.main()

Analysis of the above code, first introduced unittest module, create TestCount class that inherits unittest 's TestCase class, we can TestCase class is a collection called test for a particular class.

setUp () method is used to initialize work before the test case execution, here simply print " the Test Start " message. tearDown () method setUp () method of echoes, for rehabilitation work performed after the test (e.g., exit operation), print "herein Test End " information.

In test_add () in the first call Count class and incoming number to be calculated, by invoking add () to get the number of return values are added two methods. No longer uses the cumbersome handling exception, it calls unittest framework provided assertEqual () method add () Returns the value assertion, determines whether the two are equal, assertEqual () method of the TestCase derived class inheritance.

unittest provides global main () method which can easily test module into one unit test scripts can be run directly. main () method uses TestLoader class to search for all the module half drunk Test Method "test" the beginning of the name, and automatically execute them.

operation result:

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11505025.html