Interface Test Notes: Test Data

(1) Description

It is believed that both GUI automated tests and API automated tests cannot be separated from test data.

The test data I understand here is divided into two parts:

1. Pre-conditions required before use case execution, such as verifying the correct account and password for successful login, require a registered user first.

2. Input and expected results required by the test case itself.For example, to invoke an API, I need to set different request parameters to verify that the response under different request parameters is as expected.The request parameters and expected results here are also our test data.

The main idea here is how to manage our test data in API automation or GUI automation (this is for the second kind of test data).The idea is probably: Apply factory design patterns

1. Factory Class: Accept data in different formats, parse and process data in DataFrame format

2. Processing data in DataFrame format into a dictionary or list.

The advantage of using factory mode is that it is easy to expand later, for example, if there is a new file format, simply modify the implementation of the new file format in the factory class.

(2) Simple implementation code

 1 import os
 2 import pandas as pd
 3 
 4 
 5 class ParamConfigSourceFactory():
 6 
 7     def __init__(self,filename,sheet_name=None):
 8         """
 9         Factory Design Mode: Accept data in different formats, parse and convert to DataFrame Format data
10         :param filename:
11         :param sheet_name:
12         """
13         self.filename = filename
14         self.sheet_name = sheet_name
15         self.df = self.get_file_data()
16 
17     def get_file_data(self):
18         file_type = os.path.splitext(self.filename)[1]
19         if file_type == '.xlsx':
20             df = pd.read_excel(io=self.filename,
21                                sheet_name=self.sheet_name)
22         else:
23             raise ValueError("Unsupported file format{},Only supported xlsx format".format(file_type))
24         return df
25 
26 
27 class ParamConfigSource():
28 
29     def __init__(self,filename,sheet_name=None):
30         self.filename = filename
31         self.sheet_name = sheet_name
32         self.df = ParamConfigSourceFactory(filename=self.filename,sheet_name=self.sheet_name).df
33 
34     def get_all_test_data_format_dict(self,flag=0):
35         """
36         :param flag
37             flag=0 Return {0: {key1:value1,key2:value2..},1:{...}}Format data
38             flag=1 Return {0:[value1,value2...],1:[...]}Format data
39         :return:
40         """
41         col_name = [col for col in self.df]
42         data = {}
43         num = 0
44         for indexs in self.df.index:
45             if flag == 0:
46                 row_data = {}
47                 for i in range(len(self.df.loc[indexs].values)):
48                     row_data[col_name[i]] = self.df.loc[indexs].values[i]
49             elif flag == 1:
50                 row_data = []
51                 for i in range(len(self.df.loc[indexs].values)):
52                     row_data.append(self.df.loc[indexs].values[i])
53             else:
54                 raise  ValueError("Unsupported parameters:{}".format(flag))
55             data[num] = row_data
56             num += 1
57         return data
58 
59     def get_all_test_data_format_list(self):
60         """
61         Return[(value1,value2...),(...)]Format data
62         :return:
63         """
64         data = []
65         for indexs in self.df.index:
66             row_data = []
67             for i in range(len(self.df.loc[indexs].values)):
68                 row_data.append(self.df.loc[indexs].values[i])
69             data.append(tuple(row_data))
70         return data
71     
72 # Execute to see the results
73 pcs = ParamConfigSource(filename="./Test 1.xlsx",sheet_name="Sheet1")
74 print(pcs.get_all_test_data_format_dict())
75 print(pcs.get_all_test_data_format_dict(flag=1))
76 print(pcs.get_all_test_data_format_list())

Posted on Wed, 29 Apr 2020 16:56:16 -0700 by etully