Python test environment db automatic synchronization

Share topic

How to synchronize the baseline database level with multiple test environments?

 

Application scenarios

When there are multiple sets of test environments at work, in order to ensure the consistency of the basic environment configuration, the database structure of all test environments needs to be consistent.

For example: A requirement is tested in the beta1 environment, and there is a new table sql in the A requirement test order, and the B requirement is tested in the beta2 environment. Since the A requirement is released before the B requirement, it is in the process of the B requirement test When publishing, the main code needs to be merged into the current demand branch (for integration testing, it can be detected in advance whether the online demand has an impact on the current demand under test). After the code is merged, the corresponding configuration must also keep up, otherwise An error will be reported when the program is running, so you need to update the sql of the new table in the beta1 environment A in the beta2 environment.

Because every release and online will do a database-level synchronization update, if there are only two or three test environments, it is also possible to use manual updates. If there are many test environments and the database update content is large, manual manual updates are still used. The efficiency will be very low, and it will also cause some human operation errors. At this time, it is very important to automatically update the database synchronously. In terms of efficiency and accuracy, it is a win over manual update.

 

Code

 1#coding:utf-8
 2import pymysql
 3
 4
 5dbDict = {"test1":"l-test1.beta.ep.tx1.test.io","test2":"l-test2.beta.ep.tx1.test.io",
 6          "test3":"l-test3.beta.ep.tx1.test.io","test4":"l-test4.beta.ep.tx1.test.io",
 7          "test5":"l-test5.beta.ep.tx1.test.io","test6":"l-test6.beta.ep.tx1.test.io"}
 8
 9#这是定义了一个连接db的类,初始化方法里建立连接,并定义了sql 的执行与查询的两个方法
10
11class DBUtils():
12    def __init__(self,test):
13        print(dbDict.get(test))
14        self.conn = pymysql.connect(dbDict.get(test), "root", "123456")
15        self.cursor = self.conn.cursor()
16
17    def dbExcute(self,sql):
18        print ("execute sql")
19        self.cursor.execute(sql)
20        print(sql)
21        self.dbClose()
22
23    def dbSelect(self,sql):
24        print ("------------------------------------")
25        print(sql)
26        resultList = []
27        self.cursor.execute(sql)
28        result = self.cursor.fetchall()
29        columns = self.cursor.description
30        for val in result:
31            tempDict = {}
32            for cloNum in range(len(columns)):
33                tempDict[str(columns[cloNum][0])] = val[cloNum]
34            resultList.append(tempDict)
35        print("---------------------打印查询结果----------------------")
36        print(resultList)
37        self.dbClose()
38        return resultList
39
40    def dbClose(self):
41        self.conn.commit()
42        self.cursor.close()
43        self.conn.close(
44
45
46def main(flag,sql):
47    dbname = "test"
48    envlist=[1, 2, 3, 4, 5, 6]
49
50    for i in envlist:
51        dbname += str(i)
52        print("*" * 20 + "正在执行的环境是:", dbname + "*" * 20)
53        test= DBUtils(dbname)
54        if(flag=="exe"):
55            test.dbExcute(sql)
56        else:
57            test.dbSelect(sql)
58
59        dbname = "test"
60
61if __name__ == "__main__":
62    sql="ALTER TABLE change_record change operatorEmail email varchar(100) NOT NULL DEFAULT '' COMMENT 'email'"
63    main("exe",sql)

 

to sum up

As can be seen from the code implementation part, with this automatic synchronization script, when the database is updated, only the updated sql statement can be passed in to automatically synchronize the database information of multiple sets of test environments with one click, which is very efficient.

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [Receive Resources]
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/115305148