5.2 Unit Test why test?
The Web program development process generally consists of the following phases: [Requirements analysis, design phase, implementation phase, testing phase]. One of the testing phases is the ability to run tests on a system manually or automatically. The aim is to test whether it satisfies the requirements and to derive specific results to achieve the ultimate purpose of figuring out the difference between the expected and actual results.
Categories of tests:
Testing from the software development process can be divided into: unit testing, integration testing, system testing and so on. In many tests, unit testing is most closely related to program developers, because unit testing is done by developers, and other tests are performed by professional testers. So we mainly study unit tests.
What is unit testing?
In the process of program development, code is written in order to achieve the requirements. When our code is compiled, it simply means that it is syntactically correct and that the ability to implement it is not guaranteed. Therefore, when some of our function codes are completed, to verify that they meet the needs of the program. You can verify that the function code meets your expectations by writing the test code to simulate the procedure that the program runs.
Unit testing is where the developer writes a small piece of code to verify that the target code is functioning as expected. Typically, unit testing is primarily intended for some single-function modules.
For example: A mobile phone has many components, in the formal assembly of a mobile phone, the internal components of the mobile phone, CPU, memory, batteries, cameras, etc., should be tested, this is unit testing.
In the Web development process, unit tests are actually some "assert" (assert) code.
An assertion is a method of judging a function or object that produces a result that matches the result you expect. An assert assertion in Python is a decision that declares a Boolean value to be true, and an exception occurs if the expression is false. In unit tests, assert is generally used to assert the result.
Use of assertion methods:
The assertion statement resembles the following:
if not expression: Raise Assertionerror
Common assertion methods:
Assertequal If two values are equal, then passassertnotequal if the two values are not equal, Passasserttrue determines if the bool value is true, Passassertfalse If the bool value is False, then Passassertisnone does not exist, then Passassertisnotnone exists, the pass
How to test?
Simple test Cases: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,
def Fibo (x): if x = = 0 := 0elif x = = 1: = 1 else: c14/>return Fibo (x-1) + Fibo (x-2) return respassert Fibo (5) = = 5
Basic notation for unit tests:
First, define a class that inherits from UnitTest. TestCase
Import UnitTest class TestClass (unitest. TestCase): Pass
Second, in a test class, define two test methods
Import UnitTest class TestClass (unittest. TestCase): # The method is executed first, and the method is named fixed def setUp (self) :Pass # This method executes after the test code executes, and the method is named fixed def TearDown (self): Pass
Finally, in the test class, write the test code
Import UnitTest class TestClass (unittest. TestCase): # The method will be executed first, equivalent to the preparation before testing def setUp (self): Pass # This method executes after the test code is executed, which is equivalent to doing the finishing work after the test def TearDown (self): Pass # Test Code def test_app_exists (self): Pass
To send a message test:
#Coding=utf-8ImportUnitTest fromFlask_day04ImportappclassTestCase (unittest. TestCase):#Create a test environment that executes before the test code executes defsetUp (self): Self.app=app#Activating test Flagsapp.config['Testing'] =True self.client=self.app.test_client ()#executes after the test code execution completes defTearDown (self):Pass #Test Code defTest_email (self): Resp= Self.client.get ('/') Printresp.data self.assertequal (Resp.data,'Sent succeed')
Database testing:
#Coding=utf-8ImportUnitTest fromAuthor_bookImport*#Custom test classes, the Setup method and the Teardown method are executed before and after the test, respectively. A function that begins with Test_ is the specific test code. classdatabasetest (unittest. TestCase):defsetUp (self): app.config['Testing'] =True app.config['Sqlalchemy_database_uri'] ='Mysql://root:[email protected]/test0'Self.app=app Db.create_all ()defTearDown (self): Db.session.remove () Db.drop_all ()#Test Code defTest_append_data (self): au= Author (name='Itcast') BK= Book (info='python') Db.session.add_all ([AU,BK]) Db.session.commit () author= Author.query.filter_by (name='Itcast'). First () book= Book.query.filter_by (info='python'). First ()#Assertion Data existsSelf.assertisnotnone (author) self.assertisnotnone (book)
Flask Unit Tests