接下来我们想对modbusbase做一下简单的测试,证明功能正常。首先想到的是跑个单元测试,对比一下数据帧是否符合其它Modbus工具输出的内容。
查文档发现,仓颉具备单元测试包。只需要在测试类上声明:@Test,在方法上声明:@TestCase就可以了。单元测试包在std/unittest中,@Assert进行单元测试断言(更多功能待挖掘,暂时先只用这么多了)
于是搭建一个测试类,简单验证一下:
package modbus
from std import unittest.*
from std import unittest.testmacro.*
@Test
public class ModbusBase_Test {
@TestCase
public func createRTURequestTest(): Unit {
let linkLayer = ModbusRTUFrameLinkLayer()
let frame = ModbusBase.createRequest(1,3,0,10,linkLayer)
let array = frame.toArray()
println("${array}")
@Assert(array.size, 8)
@Assert(array[0], 1)
@Assert(array[1], 3)
@Assert(array[2], 0)
@Assert(array[3], 0)
@Assert(array[4], 0)
@Assert(array[5], 10)
}
@TestCase
public func createTCPRequestTest(): Unit {
let linkLayer = ModbusTCPFrameLinkLayer(transactionID:1)
let frame = ModbusBase.createRequest(1,3,0,10,linkLayer)
let array = frame.toArray()
println("${array}")
@Assert(array.size, 12)
@Assert(array[6], 1)
@Assert(array[7], 3)
@Assert(array[8], 0)
@Assert(array[9], 0)
@Assert(array[10], 0)
@Assert(array[11], 10)
}
@TestCase
public func createRTUResponseTest(): Unit {
let linkLayer = ModbusRTUFrameLinkLayer()
let datas:Array<Byte> = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10]
let frame = ModbusBase.createResponse(1,3,datas,linkLayer)
let array = frame.toArray()
println("${array}")
@Assert(array.size, 25)
@Assert(array[0], 1)
@Assert(array[1], 3)
@Assert(array[2], 20)
}
@TestCase
public func createTCPResponseTest(): Unit {
let linkLayer = ModbusTCPFrameLinkLayer()
let datas:Array<Byte> = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10]
let frame = ModbusBase.createResponse(1,3,datas,linkLayer)
let array = frame.toArray()
println("${array}")
@Assert(array.size, 29)
@Assert(array[6], 1)
@Assert(array[7], 3)
@Assert(array[8], 20)
}
@TestCase
public func createRTUByBuffer1Test(): Unit {
let linkLayer = ModbusRTUFrameLinkLayer()
let buffer:Array<Byte>=[1,3,0,0,0,10,197,205]
let frame = ModbusBase.createRequest(buffer,linkLayer)
@Assert(frame.Id,1)
@Assert(frame.Command,3)
@Assert(frame.StartRegNo,0)
@Assert(frame.RegCount,10)
@Assert(linkLayer.checkCRC(),true)
}
@TestCase
public func createTCPByBuffer1Test(): Unit {
let linkLayer = ModbusTCPFrameLinkLayer()
let buffer:Array<Byte>=[0,1,0,0,0,6,1,3,0,0,0,10]
let frame = ModbusBase.createRequest(buffer,linkLayer)
@Assert(frame.Id,1)
@Assert(frame.Command,3)
@Assert(frame.StartRegNo,0)
@Assert(frame.RegCount,10)
@Assert(linkLayer.TransactionId,1)
@Assert(linkLayer.TotLen,6)
@Assert(linkLayer.UnitId,1)
}
@TestCase
public func createRTUByBuffer2Test(): Unit {
let linkLayer = ModbusRTUFrameLinkLayer()
let buffer:Array<Byte>=[1,3,20,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,143,22]
let frame = ModbusBase.createResponse(buffer,linkLayer)
@Assert(frame.Id,1)
@Assert(frame.Command,3)
@Assert(frame.DataLen,20)
@Assert(linkLayer.checkCRC(),true)
}
@TestCase
public func createTCPByBuffer2Test(): Unit {
let linkLayer = ModbusTCPFrameLinkLayer()
let buffer:Array<Byte>=[0,0,0,0,0,23,1,3,20,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10]
let frame = ModbusBase.createResponse(buffer,linkLayer)
@Assert(frame.Id,1)
@Assert(frame.Command,3)
@Assert(frame.DataLen,20)
@Assert(linkLayer.TransactionId,0)
@Assert(linkLayer.TotLen,23)
@Assert(linkLayer.UnitId,1)
}
}
运行一下测试:cjpm test
[1, 3, 0, 0, 0, 10, 197, 205]
[0, 1, 0, 0, 0, 6, 1, 3, 0, 0, 0, 10]
[1, 3, 20, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 143, 22]
[0, 0, 0, 0, 0, 23, 1, 3, 20, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10]
TP: hello/modbus, time elapsed: 4200700 ns, Result:
TCS: ModbusBase_Test, time elapsed: 4066400 ns, RESULT:
[ PASSED ] CASE: createRTURequestTest (1030200 ns)
[ PASSED ] CASE: createTCPRequestTest (1050800 ns)
[ PASSED ] CASE: createRTUResponseTest (961800 ns)
[ PASSED ] CASE: createTCPResponseTest (932800 ns)
[ PASSED ] CASE: createRTUByBuffer1Test (34900 ns)
[ PASSED ] CASE: createTCPByBuffer1Test (13000 ns)
[ PASSED ] CASE: createRTUByBuffer2Test (8700 ns)
[ PASSED ] CASE: createTCPByBuffer2Test (6200 ns)
Summary: TOTAL: 8
PASSED: 8, SKIPPED: 0, ERROR: 0
FAILED: 0
cjpm test success
测试通过。
之后将后续完成:写入命令与响应,错误回应报文,各种数据类型,即可,不定期后续增补代码。直到完成串口与TCP访问。
本文所涉及源代
版权归原作者 lossage 所有, 如有侵权,请联系我们删除。