PyPa setup.py测试脚本

时间:2017-11-22 20:48:44

标签: python python-2.7 pip python-packaging

我试图遵循python packaging docs中的建议和结构。在设置功能中,您可以使用tests_require指定测试的依赖关系。您只需指定scripts即可在安装时运行脚本。但是,我可以使用仅在测试设置的情况下运行的脚本吗?

<小时/> 编辑:我的setup.py

的重要部分
from setuptools import setup
# To use a consistent encoding
from codecs import open
from os import path
import subprocess
from setuptools import setup
from setuptools.command.test import test

class setupTestRequirements(test, object):
    def run_script(self):
        cmd = ['bash', 'bin/test_dnf_requirements.sh']
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        ret = p.communicate()
        print(ret[0])

    def run(self):
        self.run_script()
        super(setupTestRequirements, self).run()

...
setup(
    ...
    scripts=['bin/functional_dnf_requirements.sh'],
    install_requires=['jira', 'PyYAML'],
    tests_require=['flake8', 'autopep8', 'mock'],

    cmdclass={'test': setupTestRequirements}
)

1 个答案:

答案 0 :(得分:1)

这并不意味着scripts中的文件将在包安装时执行。关键字scripts用于标记包中的python文件,这些文件旨在作为程序包安装后作为独立程序运行(可能名称有点误导)。示例:您的文件spam包含以下内容:

#!/usr/bin/env python

if __name__ == '__main__':
    print('eggs!')

如果您将此文件标记为脚本,只需将其添加到scripts中的setup.py

from setuptools import setup

setup(
    ...
    scripts=['spam'],
)

在软件包安装之后,您可以在终端中作为独立程序运行spam

$ spam
eggs!

阅读this tutorial以获取有关命令行脚本的更多信息。

现在,如果要在测试时执行自定义代码,则必须覆盖默认的test命令。在setup.py

from setuptools.command.test import test

class MyCustomTest(test):

    def run(self):
        print('>>>> this is my custom test command <<<<')
        super().run()

setup(
    ...
    cmdclass={'test': MyCustomTest}
)

现在,您将在运行测试时注意到其他打印件:

$ python setup.py test
running test
>>>> this is my custom test command <<<<
running egg_info
...
running build_ext

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

编辑:如果要在执行测试之前运行自定义bash脚本,请调整MyCustomTest.run()方法。脚本示例script.sh

#!/usr/bin/env bash
echo -n ">>> this is my custom bash script <<<"

MyCustomTest中调整setup.py课程:

import subprocess
from setuptools import setup
from setuptools.command.test import test


class MyCustomTest(test):

    def run_some_script(self):
        cmd = ['bash', 'script.sh']
        # python3.5 and above
        # ret = subprocess.run(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        # print(ret.stdout)

        # old python2 versions
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        ret = p.communicate()
        print(ret[0])

    def run(self):
        self.run_some_script()
        super().run()

输出:

$ python setup.py test
running test
>>> this is my custom bash script <<<
running egg_info
writing spam.egg-info/PKG-INFO
writing dependency_links to spam.egg-info/dependency_links.txt
writing top-level names to spam.egg-info/top_level.txt
reading manifest file 'spam.egg-info/SOURCES.txt'
writing manifest file 'spam.egg-info/SOURCES.txt'
running build_ext

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK