feat: MES 接口自动化测试框架(unittest + HTMLTestRunner + 数据驱动)
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+58
@@ -0,0 +1,58 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""数据库连接封装:cursor.description → namedtuple 行对象。
|
||||
|
||||
为什么不用 DictCursor —— namedtuple 是只读值对象,
|
||||
字段访问是属性而非字符串 key,写错字段名立刻 AttributeError,
|
||||
且内存更省;对「查出来做数据准备/断言」的测试场景更贴合。)
|
||||
|
||||
Demo 默认不连真 MySQL:未配置 MES_DB_HOST 时使用 dao.fake_db.FakeDB,
|
||||
接口与 pymysql 连接对象对齐(cursor / commit / rollback / close)。
|
||||
"""
|
||||
import os
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
def _connect():
|
||||
host = os.environ.get('MES_DB_HOST')
|
||||
if host:
|
||||
import pymysql # 仅在显式配置真实库时才需要
|
||||
return pymysql.connect(
|
||||
host=host,
|
||||
port=int(os.environ.get('MES_DB_PORT', '3306')),
|
||||
user=os.environ.get('MES_DB_USER', ''),
|
||||
password=os.environ.get('MES_DB_PASS', ''),
|
||||
database=os.environ.get('MES_DB_NAME', ''),
|
||||
charset='utf8',
|
||||
)
|
||||
from dao.fake_db import FakeDB
|
||||
return FakeDB()
|
||||
|
||||
|
||||
class DBConn:
|
||||
"""查询返回 namedtuple 行对象列表;写操作 commit / 异常 rollback。"""
|
||||
|
||||
def __init__(self):
|
||||
self.conn = _connect()
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def select(self, sql, params=None):
|
||||
result = []
|
||||
self.cursor.execute(sql, params)
|
||||
# 从 cursor.description 取字段名
|
||||
keys = [d[0] for d in self.cursor.description]
|
||||
Row = namedtuple('Row', keys)
|
||||
for row in self.cursor.fetchall():
|
||||
result.append(Row(*row))
|
||||
return result
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
try:
|
||||
self.cursor.execute(sql, params)
|
||||
self.conn.commit()
|
||||
except Exception as e:
|
||||
self.conn.rollback()
|
||||
raise e
|
||||
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""设备域 SQL。"""
|
||||
from dao.conn import DBConn
|
||||
|
||||
|
||||
class EquipmentDao(DBConn):
|
||||
def get_devices(self):
|
||||
sql = "select id, name from mes_device where del_flag=0 order by create_date desc"
|
||||
return self.select(sql)
|
||||
@@ -0,0 +1,81 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""演示用内存 DB:实现 pymysql 连接对象的最小接口
|
||||
(cursor / execute / fetchall / commit / rollback / close)。
|
||||
|
||||
只做一件事:把 "select ... from <table>" 映射到内置合成数据表,
|
||||
让 dao 层与用例在没有真实 MySQL 的环境下开箱可跑。
|
||||
"""
|
||||
import re
|
||||
|
||||
_TABLES = {
|
||||
'mes_user': {
|
||||
'columns': ['id', 'account_num', 'name', 'organization_id', 'del_flag'],
|
||||
'rows': [
|
||||
(1, 'demo_user', '张三', 10, 0),
|
||||
(2, 'operator01', '李四', 11, 0),
|
||||
],
|
||||
},
|
||||
'mes_device': {
|
||||
'columns': ['id', 'name', 'del_flag'],
|
||||
'rows': [(101, '注塑机-01', 0), (102, '注塑机-02', 0)],
|
||||
},
|
||||
'mes_mold': {
|
||||
'columns': ['id', 'name', 'del_flag'],
|
||||
'rows': [(201, '模具-A', 0), (202, '模具-B', 0)],
|
||||
},
|
||||
'mes_quality_inspection_point': {
|
||||
'columns': ['id', 'name', 'del_flag'],
|
||||
'rows': [(301, '外观缺陷', 0), (302, '尺寸不良', 0)],
|
||||
},
|
||||
'mes_quality_inspection_type': {
|
||||
'columns': ['id', 'name', 'del_flag'],
|
||||
'rows': [(401, '首检', 0)],
|
||||
},
|
||||
'mes_work_order': {
|
||||
'columns': ['id', 'product_id', 'plan_yield', 'del_flag'],
|
||||
'rows': [(501, 601, 300, 0)],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class FakeCursor:
|
||||
def __init__(self):
|
||||
self.description = None
|
||||
self._rows = []
|
||||
self._sql = ''
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
self._sql = sql
|
||||
m = re.search(r'from\s+(\w+)', sql, re.IGNORECASE)
|
||||
table = _TABLES.get(m.group(1) if m else '', None)
|
||||
if table is None:
|
||||
self.description = None
|
||||
self._rows = []
|
||||
return 0
|
||||
self.description = [(c,) for c in table['columns']]
|
||||
self._rows = list(table['rows'])
|
||||
# 支持演示所需的最小 WHERE 过滤:account_num = %s
|
||||
if params and 'account_num' in sql:
|
||||
idx = table['columns'].index('account_num')
|
||||
self._rows = [r for r in self._rows if r[idx] == params[0]]
|
||||
return len(self._rows)
|
||||
|
||||
def fetchall(self):
|
||||
return self._rows
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
|
||||
class FakeDB:
|
||||
def cursor(self):
|
||||
return FakeCursor()
|
||||
|
||||
def commit(self):
|
||||
pass
|
||||
|
||||
def rollback(self):
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""模具域 SQL。"""
|
||||
from dao.conn import DBConn
|
||||
|
||||
|
||||
class MoldDao(DBConn):
|
||||
def get_molds(self):
|
||||
sql = "select id, name from mes_mold where del_flag=0 order by create_date desc"
|
||||
return self.select(sql)
|
||||
@@ -0,0 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""质检域 SQL。
|
||||
|
||||
连接统一走 dao.conn 分发,模块内不写死租户 id。
|
||||
"""
|
||||
from dao.conn import DBConn
|
||||
|
||||
|
||||
class QualityDao(DBConn):
|
||||
def get_points(self, mes_id):
|
||||
sql = "select id, name from mes_quality_inspection_point where del_flag=0"
|
||||
return self.select(sql)
|
||||
|
||||
def get_types(self, mes_id):
|
||||
sql = "select id, name from mes_quality_inspection_type where del_flag=0"
|
||||
return self.select(sql)
|
||||
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""用户域 SQL。"""
|
||||
from dao.conn import DBConn
|
||||
|
||||
|
||||
class UserDao(DBConn):
|
||||
def get_user(self, account_num):
|
||||
sql = "select * from mes_user where del_flag=0 and account_num=%s"
|
||||
return self.select(sql, (account_num,))
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""生产工单域 SQL。"""
|
||||
from dao.conn import DBConn
|
||||
|
||||
|
||||
class WorkDao(DBConn):
|
||||
def get_work_orders(self):
|
||||
sql = ("select id, product_id, plan_yield from mes_work_order "
|
||||
"where del_flag=0 order by id desc")
|
||||
return self.select(sql)
|
||||
Reference in New Issue
Block a user