Test Flask Application _ Learning Notes

Source: Internet
Author: User

Source code on my github: Https://github.com/521xueweihan

Welcome everyone to Exchange study

"""A new test client is created in the SetUp () method and a new database is initialized. This method is called before each independent test function is run. The function of the TearDown () method is to close the file at the end of the test and delete the database file from the file system. In addition, the testing flag is turned on in the settings, which means that error snapping is turned off on request, so that better error reporting can be obtained when the test request is executed. The test client will provide us with a simple application interface. We can send a test request to the app through this interface. The client can also track cookies. Because SQLite3 is file system-based, we can easily use the temporary file module to create a temporary database and initialize it. The Mkstemp () function returns two things: a low-level file handle and a random filename. This file name will be followed by our database name. We must save the handle to DB_FD so that we can use the Os.close () function to close the file later. """    classflaskrtestcase (unittest. TestCase):defsetUp (self): SELF.DB_FB, flaskr.app.config['DATABASE'] =tempfile.mkstemp () flaskr.app.config['Testing'] =True Self.app=flaskr.app.test_client () flaskr.init_db ( )defTearDown (self): Os.close (SELF.DB_FB) os.unlink (flaskr.app.config['DATABASE'])        #when we visit the app's root URL (/) It should show ' No entries here's far '#Note: The Debug function starts with test so that UnitTest automatically recognizes the functions of these tests#and run them.     deftest_empty_db (self): RV= Self.app.get ('/')        assert 'No Entries this far' inchRv.data#Test your app's login and registration here. The test is sent to the app using the specified username and password#the logon and logoff requests. Because you are redirected to another page after logging in and logging off, you need to follow_redirects#Trace Redirection    deflogin (self, username, password):returnSelf.app.post ('/login', Data=Dict (username=username, password=password), follow_redirects= True)#follow_redirects Tracing Redirection        deflogout (self):returnSelf.app.get ('/logout', follow_redirects =True)deftest_login_logout (self): RV= Self.login ('Admin','default')        assert 'You were logged in' inchRv.data RV=self.logout ()assert 'You were logged out' inchRv.data RV= Self.login ('Adminx','default')        assert 'Invalid username' inchRv.data RV= Self.login ('Admin','Defaultx')        assert 'Invalid Password' inchRv.data#Test Add entry function    deftest_messages (self): Self.login ('Admin','default') RV= Self.app.post ('/add', Data=Dict (Title='<Hello>', Text='<strong>HTML</strong> allowed here'), Follow_redirects=True)assert 'No Entries this far'  not inchRv.dataassert '&lt; Hello&gt;' inchRv.dataassert '<strong>HTML</strong> allowed here' inchRv.dataif __name__=='__main__': Unittest.main ()#Notes#falsification of resources and environment--my understanding is through#With App.test_client () as C:#c.get ('/users/me ')--Incoming test value#assert_equal ()--compare two data to get a verdict

Test Flask Application _ Learning Notes

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.