pyintaller pyqt5 pytest打包后 找不到测试实例

2024-08-23 17:52

本文主要是介绍pyintaller pyqt5 pytest打包后 找不到测试实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pyintaller pyqt5 pytest打包后 找不到测试实例
1.项目目录

my_project/
├── src/
│   ├── tests/
│   │   ├── test_example.py
│   ├── main.py
├── my_project.spec

2.main.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
import pytest
import osclass TestApp(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setWindowTitle('Run Pytest Example')layout = QVBoxLayout()self.runButton = QPushButton('Run Tests', self)self.runButton.clicked.connect(self.run_tests)layout.addWidget(self.runButton)self.setLayout(layout)def run_tests(self):current_dir = os.path.dirname(os.path.abspath(__file__))test_dir = os.path.join(current_dir, 'src/tests')pytest.main([test_dir])if __name__ == '__main__':app = QApplication(sys.argv)ex = TestApp()ex.show()sys.exit(app.exec_())

3.编写测试用例:在 src/tests/test_example.py 中编写测试用例。

def test_example():assert 1 + 1 == 2

4.创建 .spec 文件:使用 PyInstaller 生成 .spec 文件,并在其中添加资源文件

# my_project.spec
# -*- mode: python ; coding: utf-8 -*-block_cipher = Nonea = Analysis(['src\\main.py'],pathex=['.'],binaries=[],datas=[('src\\tests', 'src\\tests')],hiddenimports=[],hookspath=[],runtime_hooks=[],excludes=[],win_no_prefer_redirects=False,win_private_assemblies=False,cipher=block_cipher,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(pyz,a.scripts,[],exclude_binaries=True,name='my_project',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,upx_exclude=[],runtime_tmpdir=None,console=True,
)
coll = COLLECT(exe,a.binaries,a.zipfiles,a.datas,strip=False,upx=True,upx_exclude=[],name='my_project',
)

5.使用 PyInstaller 打包:在命令行中运行以下命令来打包你的应用程序:

pyinstaller my_project.spec
pyinstaller --onefile --hidden-import=unittest.mock --add-data "src\tests;src\tests" src/main.py

即可解决

这篇关于pyintaller pyqt5 pytest打包后 找不到测试实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1100088

相关文章

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.