Python interface test

Source: Internet
Author: User

The previous article already has an interface configured with a mock server.

The Python code writes the interface test, mainly uses the UnitTest, the requests two frames

Import Jsonimport unittest, Requestsclass getcase (unittest. TestCase): #测试用例之前执行def setUp (self):p rint ("Testing start") Self.demian = "http://localhost:12306/" self.headers = {' Content-type ': ' application/json '}self.json_data = Json.dumps ({"New": "QQ", "old": "Taobao"}) #测试用例执行完之后执行def TearDown (self):p rint ("Testing engding") #测试get接口def the Test_get_case (self): results = Requests.get (Self.url (' gets ')). JSON () Self.assertequal (len (results), 2) self.assertequal (results[0][' title '), ' Java ') self.assertequal (results[0][' Version '], ' 1.8.0 ') self.assertequal (results[1][' title '), ' Python ') self.assertequal (results[1][' version '], ' 3.5 ') # Test Post Interface def test_post_case (self): results = Requests.post (Self.url (' posts '), data = self.json_data, headers = self.headers) re= Results.json () self.assertequal (Results.status_code, $) self.assertequal (Len (re), 2) Self.assertequal (re[0][' fruit '), ' Apple ') self.assertequal (re[0][' computer '), ' Lenvo ') self.assertequal (re[1][') Mobile '], ' iphone ') self.assertequal (re[1][' book '), ' testing ')#测试put接口def test_edit_case (self): results = Requests.put (Self.url (' edit '), data = self.json_data, headers = self.headers ) re= Results.json () self.assertequal (Results.status_code, $) self.assertequal (re[' success '], ' true ') # Test Delete Interface def test_delete_case (self): results = Requests.delete (self.url (' delete ')) re= Results.json () Self.assertequal (Results.status_code, $) self.assertequal (re[' success '), ' true ') def url (self, path): return Self.demian + pathif __name__ = = ' __main__ ': Unittest.main ()

Operation Result:

Python interface test

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.