feat: MES 接口自动化测试框架(unittest + HTMLTestRunner + 数据驱动)
This commit is contained in:
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""设备管理-OEE 导出用例。
|
||||
|
||||
覆盖 post_file(multipart 上传)与二进制响应两种场景。
|
||||
"""
|
||||
import io
|
||||
import unittest
|
||||
|
||||
from core import auth, config
|
||||
from core.http_client import HttpClient
|
||||
from core.logger import setLog
|
||||
|
||||
|
||||
class TestOeeExport(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
auth.login()
|
||||
cls.http = HttpClient()
|
||||
|
||||
@setLog()
|
||||
def test_device_export(self):
|
||||
"""设备 OEE 导出:上传模板文件,断言返回文件流"""
|
||||
url = config.base_url() + '/mes/device/export'
|
||||
# 用内存合成文件构造上传内容,不依赖磁盘上的 Excel 模板
|
||||
files = {'file': ('oee_template.csv', io.BytesIO(b'date,oee\n2026-09-01,85.5\n'),
|
||||
'text/csv')}
|
||||
result = self.http.post_file(url, files)
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn('attachment', result.headers.get('Content-Disposition', ''))
|
||||
self.assertIn(b'demo-device-01', result.content)
|
||||
return {'bytes': len(result.content)}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""登录模块用例。
|
||||
|
||||
数据驱动:4 条场景来自 fixtures/login.yml。
|
||||
"""
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
|
||||
from core import config, data
|
||||
from core.logger import setLog
|
||||
|
||||
|
||||
def _login(username, password):
|
||||
url = config.base_url() + '/mes/login'
|
||||
payload = {'username': username, 'password': password, 'encrypt': 0}
|
||||
return requests.post(url, data=payload,
|
||||
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'})
|
||||
|
||||
|
||||
class TestLogin(unittest.TestCase):
|
||||
"""登录接口:成功 / 用户名为空 / 密码为空 / 均为空。"""
|
||||
|
||||
@setLog()
|
||||
def test_login_success(self):
|
||||
"""正确账号密码登录成功"""
|
||||
case = data.load('login')[0]
|
||||
result = _login(case.username, case.password)
|
||||
body = result.json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
self.assertIn('JSESSIONID', result.headers.get('Set-Cookie', ''))
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_login_empty_username(self):
|
||||
"""用户名为空"""
|
||||
case = data.load('login')[1]
|
||||
body = _login(case.username, case.password).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_login_empty_password(self):
|
||||
"""密码为空"""
|
||||
case = data.load('login')[2]
|
||||
body = _login(case.username, case.password).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_login_empty_both(self):
|
||||
"""用户名密码均为空"""
|
||||
case = data.load('login')[3]
|
||||
body = _login(case.username, case.password).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,53 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""模具管理用例。
|
||||
|
||||
业务链路:先查设备(dao),再按设备批量建模具(接口)。
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from core import auth, config
|
||||
from core.http_client import HttpClient
|
||||
from core.logger import setLog
|
||||
from dao.equipment import EquipmentDao
|
||||
|
||||
|
||||
class TestMold(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
auth.login()
|
||||
cls.http = HttpClient()
|
||||
|
||||
@setLog()
|
||||
def test_mold_add(self):
|
||||
"""按设备批量建模具"""
|
||||
dao = EquipmentDao()
|
||||
devices = dao.get_devices()
|
||||
self.assertGreaterEqual(len(devices), 1)
|
||||
|
||||
url = config.base_url() + '/mes/mold/add'
|
||||
results = []
|
||||
for dev in devices:
|
||||
payload = {
|
||||
'name': '演示模具-{0}'.format(dev.id),
|
||||
'serialNum': str(dev.id),
|
||||
'specifications': '',
|
||||
'moldDeviceIDs': [str(dev.id)],
|
||||
}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], 200)
|
||||
results.append(body)
|
||||
dao.close()
|
||||
return results
|
||||
|
||||
@setLog()
|
||||
def test_mold_add_empty_name(self):
|
||||
"""新增模具-名称为空"""
|
||||
url = config.base_url() + '/mes/mold/add'
|
||||
body = self.http.post_json(url, {'name': ''}).json()
|
||||
self.assertEqual(body['code'], 400)
|
||||
self.assertEqual(body['message'], '模具名称不能为空')
|
||||
return body
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,58 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""生产计划-工单用例。"""
|
||||
import unittest
|
||||
|
||||
from core import auth, config, data
|
||||
from core.http_client import HttpClient
|
||||
from core.logger import setLog
|
||||
from dao.work import WorkDao
|
||||
|
||||
|
||||
class TestWorkOrder(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
auth.login()
|
||||
cls.http = HttpClient()
|
||||
|
||||
@setLog()
|
||||
def test_work_order_save(self):
|
||||
"""工单保存-正常"""
|
||||
case = data.load('work_orders')[0]
|
||||
url = config.base_url() + '/mes/work/order/save'
|
||||
payload = {
|
||||
'serialNum': case.serial_num,
|
||||
'productId': case.product_id,
|
||||
'planYield': case.plan_yield,
|
||||
'organizationId': case.organization_id,
|
||||
'routeId': case.route_id,
|
||||
'startTime': case.start_time,
|
||||
'endTime': case.end_time,
|
||||
}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_work_order_save_empty_product(self):
|
||||
"""工单保存-产品为空"""
|
||||
case = data.load('work_orders')[1]
|
||||
url = config.base_url() + '/mes/work/order/save'
|
||||
payload = {'serialNum': case.serial_num, 'productId': case.product_id,
|
||||
'planYield': case.plan_yield}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
def test_work_order_dao(self):
|
||||
"""dao:工单列表查询返回 namedtuple 行"""
|
||||
dao = WorkDao()
|
||||
rows = dao.get_work_orders()
|
||||
self.assertGreaterEqual(len(rows), 1)
|
||||
self.assertTrue(hasattr(rows[0], 'plan_yield'))
|
||||
dao.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""质检管理用例。
|
||||
|
||||
用例保留
|
||||
「dao 查质检点 → 组装质检类型/方案」的链路,数据改为合成。
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from core import auth, config
|
||||
from core.http_client import HttpClient
|
||||
from core.logger import setLog
|
||||
from dao.quality import QualityDao
|
||||
|
||||
|
||||
class TestQuality(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
auth.login()
|
||||
cls.http = HttpClient()
|
||||
cls.mes_id = auth.get_mes_id()
|
||||
|
||||
@setLog()
|
||||
def test_quality_point_add(self):
|
||||
"""新增质检点"""
|
||||
url = config.base_url() + '/mes/qualityPoint/add'
|
||||
body = self.http.post_json(url, {'name': '外观缺陷', 'remark': ''}).json()
|
||||
self.assertEqual(body['code'], 200)
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_quality_point_add_empty(self):
|
||||
"""新增质检点-名称为空"""
|
||||
url = config.base_url() + '/mes/qualityPoint/add'
|
||||
body = self.http.post_json(url, {'name': ''}).json()
|
||||
self.assertEqual(body['code'], 400)
|
||||
return body
|
||||
|
||||
def test_quality_dao(self):
|
||||
"""dao:质检点/质检类型查询"""
|
||||
dao = QualityDao()
|
||||
points = dao.get_points(self.mes_id)
|
||||
types = dao.get_types(self.mes_id)
|
||||
self.assertGreaterEqual(len(points), 1)
|
||||
self.assertGreaterEqual(len(types), 1)
|
||||
dao.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""系统管理-物料用例:物料保存 + 导入场景的合成数据驱动。"""
|
||||
import unittest
|
||||
|
||||
from core import auth, config, data
|
||||
from core.http_client import HttpClient
|
||||
from core.logger import setLog
|
||||
|
||||
|
||||
class TestMaterial(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
auth.login()
|
||||
cls.http = HttpClient()
|
||||
|
||||
@setLog()
|
||||
def test_material_save(self):
|
||||
"""物料保存-正常"""
|
||||
case = data.load('materials')[0]
|
||||
url = config.base_url() + '/mes/material/save'
|
||||
payload = {'materialCode': case.material_code,
|
||||
'materialName': case.material_name, 'unit': case.unit}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_material_save_empty_code(self):
|
||||
"""物料保存-编码为空"""
|
||||
case = data.load('materials')[1]
|
||||
url = config.base_url() + '/mes/material/save'
|
||||
payload = {'materialCode': case.material_code,
|
||||
'materialName': case.material_name, 'unit': case.unit}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,59 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""系统管理-用户用例。
|
||||
|
||||
数据驱动的接口断言用例,
|
||||
并用 dao 层做「接口写入 vs 库记录」的数据比对演示。
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from core import auth, config, data
|
||||
from core.http_client import HttpClient
|
||||
from core.logger import setLog
|
||||
from dao.user import UserDao
|
||||
|
||||
|
||||
class TestUserManage(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
auth.login() # 获取 JSESSIONID + token(落 artifacts/session.json)
|
||||
cls.http = HttpClient()
|
||||
|
||||
@setLog()
|
||||
def test_add_user(self):
|
||||
"""新增用户-正常"""
|
||||
case = data.load('users')[0]
|
||||
url = config.base_url() + '/mes/user/add'
|
||||
payload = {
|
||||
'accountNum': case.account_num,
|
||||
'name': case.name,
|
||||
'organizationId': case.organization_id,
|
||||
'mesRoles': [{'id': case.role_id}],
|
||||
'password': 'demo_pass',
|
||||
}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
@setLog()
|
||||
def test_add_user_empty_account(self):
|
||||
"""新增用户-账号为空"""
|
||||
case = data.load('users')[2]
|
||||
url = config.base_url() + '/mes/user/add'
|
||||
payload = {'accountNum': case.account_num, 'name': case.name}
|
||||
body = self.http.post_json(url, payload).json()
|
||||
self.assertEqual(body['code'], case.code)
|
||||
self.assertEqual(body['message'], case.result)
|
||||
return body
|
||||
|
||||
def test_user_dao_row_object(self):
|
||||
"""dao 层 namedtuple 行对象:按属性取字段"""
|
||||
dao = UserDao()
|
||||
rows = dao.get_user('demo_user')
|
||||
self.assertEqual(len(rows), 1)
|
||||
self.assertEqual(rows[0].name, '张三')
|
||||
dao.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user