Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将Pytest文件打包为可执行文件_Python_Python 3.x_Python 2.7_Pytest - Fatal编程技术网

Python 将Pytest文件打包为可执行文件

Python 将Pytest文件打包为可执行文件,python,python-3.x,python-2.7,pytest,Python,Python 3.x,Python 2.7,Pytest,因此,我设计了一个Pytest测试包,它由一个python脚本组成: class TestClass(): test_a() test_b() 我想知道是否可以将测试打包为可执行文件并运行它? 原因是我希望避免在我将要执行测试的所有机器上安装python和python包。创建python文件并使用pytest包从那里调用测试: import pytest pytest.main(['mytestdir']) 像对待任何python项目一样,将main.py和测试文件打包到可

因此,我设计了一个Pytest测试包,它由一个python脚本组成:

class TestClass():

   test_a()

   test_b()
我想知道是否可以将测试打包为可执行文件并运行它?
原因是我希望避免在我将要执行测试的所有机器上安装python和python包。

创建python文件并使用pytest包从那里调用测试:

import pytest

pytest.main(['mytestdir'])
像对待任何python项目一样,将main.py和测试文件打包到可执行文件中。将main.py作为可执行的入口点


有关从python运行pytest的更多信息:

如果脚本的编写方式适合作为exe运行,则可以使用PytInstaller。如果你使用水蟒,那么你只需要跑

pyinstaller --onefile path_to_your_file
虽然我通常运行类似这样的操作以避免PyQt5出现错误

pyinstaller --onefile --exclude-module PyQt5 --noconsole C:\username\etc\script.py

请参阅以下内容,了解如何使用pytest冻结脚本

“冻结试验

如果您使用PyInstaller之类的工具冻结应用程序,以便将其分发给最终用户,那么最好也打包您的测试运行程序,并使用冻结的应用程序运行测试。这样,打包错误(如未包含在可执行文件中的依赖项)可以及早检测到,同时还允许您发送向用户测试文件,以便他们可以在自己的机器上运行这些文件,这有助于获取有关难以复制的错误的更多信息

幸运的是,最近的PyInstaller版本已经为pytest提供了一个自定义的钩子,但是如果您正在使用另一个工具来冻结可执行文件,例如cx_freeze或py2exe,那么您可以使用pytest.freeze_includes()获取内部pytest模块的完整列表。但是,如何配置工具以查找内部模块因工具而异

与将pytest runner冻结为单独的可执行文件不同,您可以在程序启动期间通过一些巧妙的参数处理使冻结的程序作为pytest runner工作。这允许您拥有单个可执行文件,这通常更方便。请注意,pytest使用的插件发现机制(setupttools入口点)不适用于冻结的可执行文件,因此pytest无法自动找到任何第三方插件。要包含pytest timeout等第三方插件,必须显式导入它们并将其传递给pytest.main。“


这个大段引用来自。

在没有看到您想要完成什么的情况下,这里是我得到的。我使用了您的代码的修改版本,可以自己运行

import pytest

class TestSanity(): 
    def test_step_1(self): 
        assert 2==2 
#        if __name__ == "__main__": 
        test=pytest.main(['-v', '-x', '--durations=0'])
        print(test)


TestSanity().test_step_1()
我将其保存为“pytest_with_pyinstaller.py”

然后我在anaconda提示符中使用这一行来打包pyinstaller文件

pyinstaller --exclude-module PyQt5 --hidden-import pytest.mark --hidden-import py._builtin    --onefile --hidden-import pytest.main U:\PLAN\BCUBRICH\Python\Tests\pytest_with_pyinstaller.py
然后,我从windows cmd控制台运行生成的.exe

>Run>cmd
>cd "your output director"
>"your_exe.exe"
C:\Users\bcubrich\dist\test2>pytest\u with\u pyinstaller.exe ===========================================测试会话开始============================= 平台win32--Python 3.7.1、pytest-4.0.2、py-1.7.0、Plugy-0.8.0--C:\Users\bcubrich\dist\test2\pytest\u with_pyinstaller.exe cachedir:.pytest\u缓存 rootdir:C:\Users\bcubrich\dist\test2,文件: 收集了0个项目

========================================0.01秒内未运行任何测试=========================

五,


最后的5似乎是pytest行,与我在spyder中运行此代码时得到的数字相同,因此我认为它工作正常,只是pytest脚本将尝试测试dist目录中的任何内容。

如果您将pytest脚本编写为EXE运行,那么这就是全部问题所在。然后您会破坏pytes的整个结构它只是另一个py/exe脚本,既不生成控制台输出报告,也不生成Junit XML报告。Junit XML报告允许Jenkins等生成报告。如果不使用“-no console”,PyInstaller应该写入控制台参数,但我不知道pytest如何写入控制台。嘿@bart cubrich,所以我尝试实现您建议的解决方案,我使用
code pyinstaller pythonscript.py
命令生成可执行文件(它附带了pyinstaller文档中描述的dist文件夹)。问题是它没有在脚本中选择任何测试。该脚本是在以下事项中构建的:
代码类测试健全性:“
代码测试1`
代码测试2
如果{uuuu name\=“\uuuuu main\uuuu=”:
代码pytest.main(['-v',-x','-durations=0','-junitxml',junit path]))
执行时,我会收集0个项目。为了帮助您完成更多操作,我需要查看一些代码,还需要查看您在命令提示符下运行的行以初始化pyinstaller。您好,代码如下:
代码类TestSanity(BaseTest):def test\u步骤1(self):assert 2==2如果“\uuuuu name\uuuu==”\ uuuuu main\uuuuu“:pytest.main(['-v','-x','--durations=0')
我用来初始化pyinstaller的命令:
codepyinstaller mytestscript.py
我熟悉pytest.main实现:
codepytest.main(['-v','-durations=0','-junitxml',junit_path,'-tb',long'])
请参阅我对上述建议的答复。您可以在测试文件中使用
pytest.main([“-s”,_u文件u])
来简单地使用
python test_file.py
运行它。非常感谢,我将尝试使用您建议的解决方案进行小规模的实验
>Run>cmd
>cd "your output director"
>"your_exe.exe"