In the past two days, I have been looking for a way to directly use python to automate the interface. I also searched for some blog references on the Internet. I tried it myself today.
1. Overall structure
The above figure is the directory structure of the project. The following mainly introduces the role of each directory.
Common: public methods: classes that mainly place public operations, such as database sqlhelper, file operation classes, etc.
Config: public variables: mainly place public variables, such as ST, UAT, url address of production environment, username and password, database connection
Data: data layer, somewhat similar to DAL in the three-tier architecture, it is the source of data, and subdivides json, xml, form and database according to the format of data storage
Log: log layer: store logs for easy tracking and debugging
Page: Page layer: First divide the whole system into several subsystems, each subsystem contains several pages. This abstracts the page operated by the user into a page object, and abstracts the operation of the page into a method, so that the tester can pass different test cases for testing. If it is a service-oriented pure interface, there is no need to divide it in this way. , which converts interface tests into python unit tests.
Result: Store the execution result of the unit test. You can also store the result of each execution in the database for management, and then do the trend analysis of the test result. If the project is integrated into Jenkins later, it is equivalent to Jenkins integrating python unit testing. In this case, this Layers may also not be required.
Case: The test case layer uses the test data and expected data to make assert judgments for the single method corresponding to the above Page. The test data and expected data used here can be placed in Excel later, and the tester only needs to fill in the test data.
Run: This is used to assemble a suite and then run the case.
2. Test
1. Install HTMLTestRunner
Download it and put it in the lib directory of the python installation directory
2. Business logic layer
Some business processing is simulated here, and the requests library will be used to make requests when doing interface automation.
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import requests def Add(name,pwd): session=requests.session() response=session.get('http://www.baidu.com') print(response.status_code) return response.status_code==200 def Edit(name,pwd): return {'name':name,'pwd':pwd} def Delete(name,pwd): return {'name':name,'pwd':pwd} def Search(name,pwd): return {'name':name,'pwd':pwd}
3. Case layer
It was originally planned to add a suite layer. If it is a single interface, it is ok. If multiple interfaces are used for process testing, the order of cases will not change when using the suite. If it is a process, it can also be written as a case, but the business logic layer needs to be called multiple times.
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import unittest from Root.Page import Login from Root.Page.UserManager import Index import HTMLTestRunner import time class index(unittest.TestCase): def setUp(self): print('setUp') def tearDown(self): print('tearDown') def test_add(self): arr= Login.Login('admin', '123456') flag= Index.Add(arr[0], arr[1]) self.assertTrue(flag) flag= Index.Add(arr[0], arr[1]) self.assertTrue(flag==False) def test_edit(self): response= Login.Login('admin', '123456') dic= Index.Edit(response[0], response[1]) self.assertNotEqual(dic,{'name':'123'}) def test_delete(self): response= Login.Login('admin', '123456') dic= Index.Delete(response[0], response[1]) self.assertNotEqual(dic,{'name':'123'})
4. Run
The main consideration here is that the entire system may be divided into different modules for operation, which will also be convenient for maintenance, and can be executed by multiple executive machines. HTMLTestRunner is used here to generate reports.
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding( ' utf-8 ' ) import os import unittest from HTMLTestRunner import HTMLTestRunner from Root.Test.Case.UserManager import Index import HTMLTestRunner import time if __name__ == ' __main__ ' : # 1. Construct the use case set suite = unittest. TestSuite() # 2. The execution order is the order of loading: first execute test_sub, then execute test_add suite.addTest(Index.index( " test_add " )) suite.addTest(Index.index("test_edit")) suite.addTest(Index.index("test_delete")) suite.addTest(Index.index("test_edit")) suite.addTest(Index.index("test_edit")) filename = " ../../../Result/{0}Report.html " .format(time.strftime( " %Y%m%d%H%M%S " , time.localtime()) ) #Define a report storage path, support relative path f = file(filename, ' wb ' ) #The result is written to an HTML file runner = HTMLTestRunner.HTMLTestRunner(stream=f, title= ' Test report ' , description= ' XXX system interface automation Test test report ' ,verbosity=2) #Use HTMLTestRunner configuration parameters to output report path, report title, description runner.run(suite)