HslCommunication 写入及获取简单用例

这篇博客介绍了如何在WinForm应用程序中使用HslCommunication库进行简单的数据写入和接收操作。通过引用HslCommunication.dll,演示了如何设置默认的127.0.0.1和502端口进行本地测试,并强调了在接收数据时需要注意的数据类型匹配问题。此外,还提到了除int32之外的其他写入格式选项。

HslCommunication 简单运用

此为winform窗体应用程序前端模型

本地测试,为127.0.0.1 默认端口为502,正式使用需注意端口号,地址,起始地址

HslCommunication 写入功能实现 后端代码

引用HslCommunication.dll (HslCommunication官方网站下载,HslCommunication是免费使用的)

除int 32 写入方式外,还有其他写入格式,如:

接收HslCommunication 写入数据

为了测试方便采用winform控制台应用程序循环接收

首先引用HslCommunication.dll

开始接收数据(采用循环接收) 

*接收数据时需注意接收对象 short为接收int类型数据 int类型不可接收ReadInt16数据*

在 Pytest 中获取测试用的执行结果,可以通过钩子函数(hook)或插件机制实现。Pytest 提供了丰富的内部机制来捕获测试执行过程中的状态和结果信息,如通过 `pytest_runtest_makereport` 钩子获取测试用的执行报告,或者利用 `pytest` 内置的 `TestReport` 对象来访问测试结果。 ### 使用 `pytest_runtest_makereport` 获取执行结果 该钩子函数在测试用执行过程中被调用三次:`setup`、`call` 和 `teardown`。通过判断 `report.when` 的值,可以仅在测试用实际执行(即 `call` 阶段)时获取结果。 以下是一个示,展示如何在 `conftest.py` 文件中定义钩子函数以捕获测试用的执行结果: ```python from _pytest.runner import pytest_runtest_makereport as orig_pytest_runtest_makereport @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() if report.when == 'call': # 获取测试用执行结果 if report.failed: # 测试用失败时的操作,如截图 print(f"Test {item.name} failed") elif report.passed: # 测试用通过时的操作 print(f"Test {item.name} passed") ``` ### 通过 `pytest` 内置对象获取结果 在测试用中,可以通过 `request` 对象获取测试用的执行上下文信息,包括参数化数据、执行状态等。 如,定义一个 fixture 来获取测试用的执行结果: ```python import pytest @pytest.fixture(scope="function") def capture_test_result(request): def fin(): if request.node.rep_call.failed: print(f"Test {request.node.name} failed") elif request.node.rep_call.passed: print(f"Test {request.node.name} passed") request.addfinalizer(fin) # 测试用中使用该 fixture def test_example(capture_test_result): assert True ``` ### 通过 `pytest` 插件获取结果 Pytest 提供了多种插件来增强测试结果的获取和报告能力,如: - `pytest-html`:生成 HTML 格式的测试报告。 - `pytest-xdist`:并行执行测试用。 - `pytest-metadata`:附加元数据到测试报告中。 安装 `pytest-html` 后,可以使用以下命令生成 HTML 报告: ```bash pytest --html=report.html ``` ### 示:结合截图功能的执行结果捕获 以下示展示了如何在测试失败时自动截图,这通常用于自动化测试中调试问题: ```python from _pytest.runner import pytest_runtest_makereport as orig_pytest_runtest_makereport import os @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() if report.when == 'call': if report.failed: # 自动截图逻辑 screen_shot_dir = "screenshots" if not os.path.exists(screen_shot_dir): os.makedirs(screen_shot_dir) screenshot_path = os.path.join(screen_shot_dir, f"{item.name}.png") item.funcargs["browser"].save_screenshot(screenshot_path) print(f"Screenshot saved to {screenshot_path}") ``` ###
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值