python automation topic (1)

Question 1: The difference between json and dictionary?

Json is a lightweight data interaction format that saves data in the form of key-value key-value pairs with a clear structure. It can be said to be the most commonly used data interaction format in Internet project development.
A dictionary is also a key-value key-value pair to store data, which is a data type in python.

Question 2: When you do interface automation testing, where do you put the test data?

  1. For some basic configuration, such as database configuration, you can put it in the properties file (yaml file, don’t expand it if you haven’t used it)
  2. The data needed for interface testing can be placed in a database table or in excel

Question 3: What is data-driven?

Data-driven is to maintain test data through data structures such as excel, xml, database, etc., and process it through corresponding technologies to obtain test data for testing, thereby realizing the decoupling of test data and code. The essence of data-driven testing is advanced Parameterization.

Question 4: How to design your use case in excel?

  1. All interface information is maintained in one form
  2. Use case data about the interface is maintained in a form
  3. The interface parameters are passed in one column. By constructing a json format string, the problem of passing multiple parameters can be solved, and the maintainability of the use case is improved.

Question 5: How to get the data in excel?

Through the third-party library openpyxl to complete, the project only needs to introduce the openpyxl library, and the operation of excel can be completed through the api provided by it, and even we can do some secondary packaging ourselves to make the operation more convenient. One row of data is a use case.

Question 6: If some interfaces are authenticated (requires authentication authority), how to realize automated testing of these interfaces?

First of all, it is necessary to clarify the authentication mechanism through which the interface is implemented

  1. If the authentication is achieved through the session id (JSESSIONID), then first call the login interface-"get the data returned by it-"store it -" when you call other interfaces later, you can directly bring this data in the past. Up.
  2. If the authentication is achieved through tokens, then the same only needs to be done after calling the login interface-"get the data returned by it -" store it, and the following operations are the same as above.

First answer one of the basic implementation steps above, and then add the following implementation details:

  1. How to get this JSESSIONID or token

    ** How to obtain this information must first understand how it is returned, which may be in the following two ways:
    a. The response header of the
    login interface b. The response message of the login interface
    needs to be confirmed by the developer. Different methods have different methods.

  2. . How to store this JSESSIONID or token
    a. file (excel, properties files are all right)
    b. database
    c. public static variables defined in the code as a shared data

  3. . When requesting other interfaces that require authentication, how to bring this data in the past,
    first ask the developer to confirm, and then how to call these interfaces that require authentication to carry the authentication information (ticket).
    Generally, it is nothing more than the following:
    a. Splicing in the URL to upload the past for server verification
    b. Set it to the request body
    c. After understanding how to pass it through the request header, try to save the authentication information Take out the place —> Pass it over

Question 7: What is a conversation?

The so-called session is the session, and the session is a server caching technology, created by the server, when the user logs in to the system, generally the information of the logged-in user will be saved in the session object, and then the id (JSESSIONID) corresponding to the session will be saved Return, so most systems will use the session mechanism to achieve authentication. Session saves data in the form of key-value.

Question 8: What is a token?

The so-called token is actually a string returned by the server (somewhat similar to: xys73494954sdhcdr83435 such a string). Based on what algorithm is this data generated, it needs to be confirmed by development. Generally, this data is unique, and the token returned by the server will be different every time. same. The reason why Token can be used for authentication is based on the following principle:
User a calls the login interface-"logged in to the system b-"The server generates a unique token information (assuming c), and then takes the user's number id ( Suppose d) make a mapping: cd, and then store such a mapping relationship in the database or redis cache, and then return this token to the client-"When the client calls other interfaces that require authentication, it only needs Bring the cached token to the past for verification-"The server checks whether there is logged-in user information based on this token to determine whether the request is a logged-in authorized user. (Here, how the client gets the token, how to save it, and how to bring it with it when requesting again, as explained in the interface authentication section above).

Question 9: For an interface request, the parameter depends on the return data of the previous interface, how to deal with it (interface association)?

Scenario: When calling interface b, the parameters depend on the return data of interface a. Solution:
a. After calling the interface-"Remove the parameter that b depends on from the response-"Save it -" When b is called, take out this information-"Pass it as a parameter.

Question 10: The interface call relies on the third-party interface b, and the b interface has not been provided to you. How to deal with it?

Build your own mock system/platform through the mock framework (in fact, you develop your own interface), and you can simulate this third-party interface mock, so that this dependency is solved

Question 11: How to clean up junk data in interface testing?

You can prepare a separate environment (independent database) yourself, so that the data generated by the interface call can be deleted by executing the sql script after the entire suite is executed (@AfterSuite).

Question 12: You do interface automation, what database does the project use, and what database is used to operate this database?

Mysql, you can add, delete, check and modify the database through jdbc.

Question 13: How do you do data verification when you do interface automation?

Data verification mainly includes two aspects:

  1. Verification of interface response messages
    Use jsonpath to retrieve the values ​​of key fields in the response data (in most cases json), and use Assert to assert whether the values ​​of these key fields meet expectations. The expected value and jsonpath value expression can be configured in excel.
  2. To verify database table data, you must first understand two points for table data verification:
    a. Table data verification must first understand the basic business of this interface, which tables and which fields will be affected, and then prepare to query sql to be targeted Check the data.
    b. Not all use cases will operate on the database and affect table data. Therefore, only use cases that affect table data can query scripts be prepared.
    Specific method:
    Excel use case preparation aa, bb, cc three columns, aa transfer script, bb, cc two columns are used to write back the SQL query results before the use case execution and the SQL query results after the use case execution, the two data can be compared Whether the data changes meet expectations.

Question 14: How to improve the execution efficiency of the test suite

  1. Reduce excel reading and writing, reduce unnecessary io operations
  2. The excel data reads the test data and stores them all in static variables).
  3. To write back excel data, try to write in batches.

Question 15: Have you used a unit testing framework, what framework is it, and what are the common operations?

Used, unittest, pytest (if you are not familiar with it), these test frameworks all support us to define test suites to manage our test case code. At the same time, the rich annotations provided by these test frameworks can not only control the test conveniently The execution sequence of use cases controls the entire test process, and can also provide support for the realization of various test scenarios.
Common operations:
seutp, teardown, suitable for the initialization and environment cleaning of a single test case.
seutpClass, teardownClass: suitable for global initialization and environment cleaning of test classes.
assertXXXX: various assertion methods
testLoader’s discover: used to discover test cases
testSuite’s addTests: used to load test cases
@ddt data-driven
htmlTestRunner: used to generate html test reports

Question 16: Tell me about your understanding of interfaces

The interface is a service, a function point realization, a data transmission channel, and a server-side one that implements a certain protocol (such as http protocol...) and a mapping mechanism (when a URL is accessed, it will be resolved through the server-side mapping processing mechanism, Falling to the corresponding processing function), the interface parameter is the function parameter, and the response data of the interface is the return value of the function

Question 17: Have you ever done an interface test? What are some familiar tools?

Have done it (even if you haven't used a visualization tool to do interface testing before, but now you have learned interface automation testing even if you have experience).
Familiar tools:
Visual tools such as: jmeter, postman, soapui, etc. (what have been used, say which) Code: requests package technology to implement interface testing.

Question 18: What are the common request methods for interfaces?

GET, POST, PUT, DELETE, etc. (unfamiliar can be said)

Question 19: Please briefly talk about the difference between the two request methods: get and post?

Get

  1. Generally, requests for fetching data from the server can be set to get
  2. When passing parameters in the Get method, the parameters are usually directly spliced ​​on the url (for example: http://xxx?id=1&type=2)
  3. The amount of parameter data that can be passed by the Get request method is limited (because the general parameters are spliced ​​on the url, and the browser has a limit on the length of the url)
  4. Because the Get request directly splices the data on the URL, the security is not as strong as the post (relative), but the execution efficiency of the get is faster than the post

Post

  1. Generally, requests to submit data to the server will be set to post
  2. Passing parameters in Post mode generally puts the parameters in the request body instead of splicing them in the url
  3. The amount of data that can be submitted by the Post request method is not limited
  4. Post request parameter transfer is safer than get (relatively not absolute), but the execution efficiency of post is not as efficient as get

Question 20: What happens after I enter the URL in the browser and press Enter?

  1. URL resolution
  2. DNS lookup
  3. TCP connection
  4. Processing request
  5. Accept response
  6. Render the page

Question 21: To splice the list a = ["h","e","l","l","o"] into a string, please use various methods to achieve

a = ["h","e","l","l","o"]

方式一:字符串函数调用
print("".join(a))  

方式二:for循环
s = ""
for item in a:
    s += item
print(s)

Question 22: There is a file named data.log. The content in data.log is separated by a comma, and the TestID, TestTime, Success (0 success; 1 failure) of a test are sequentially stored. The data in the file are all non-negative integers. Please write a program (no language limit), for all successful (Success=0) tests, output:

  1. Print the maximum and minimum TestTime;
  2. Print the average TestTime with 2 decimal places.
    The content format of data.log is as follows:
    TestID,TestTime,Success
    0,149,0
    1,69,0
    2,45,0
    3,18,1
    4,18,1
"""
打开data.log文件 。除了第一行,剩下的第一行都是数据。
只需要按行解析数据,将success对应的值为0的提取出来,将TestTime放在一个新的列表当中。
取这个列表当中,最大的值,和最小的值。
同时,对这个列表的所有数据进行取平均值即可。
"""
import os

def anaysis_data():
    test_times = []
    # 打开data.log文件
    with open(os.getcwd() + "/data.log") as fs:
        for line in fs.readlines():  # 按行读取
            temp = line.strip("\n").split(",") # 去掉换行符之后,再按,分割
            print("temp",temp)
            if temp[-1] == str(0): # 筛选success字段为0的TestTime
                test_times.append(int(temp[-2]))

    if len(test_times) > 0:
        avg_time = sum(test_times) / len(test_times) # 平均值
        max_time = max(test_times)
        min_time = min(test_times)
        print("最大的TestTime: ",max_time,",最小的TestTime: ",min_time,",平均TestTime: ",avg_time)


if __name__ == '__main__':
    anaysis_data()
    
    """
    输出结果为:
    temp ['TestID', 'TestTime', 'Success']
    temp ['0', '149', '0']
    temp ['1', '69', '0']
    temp ['2', '45', '0']
    temp ['3', '18', '1']
    temp ['4', '18', '1']
    最大的TestTime:  149 ,最小的TestTime:  45 ,平均TestTime:  87.66666666666667
    """

Guess you like

Origin blog.csdn.net/weixin_44102466/article/details/104857230