簡體   English   中英

如何使用“ pip install”運行單元測試?

[英]How to run unit tests with “pip install”?

在工作中,我們正在考慮配置用於內部軟件部署的本地pypi存儲庫。 使用“ pip install”進行部署會很方便,但是我擔心應該在添加新軟件包后執行單元測試以確保正確安裝。 我一直以為pip正在執行此操作,但是在pip文檔中沒有發現與測試相關的內容。

您可以通過pip將參數傳遞給setup.py:

--install-option附加參數將提供給setup.py安裝命令(使用–install-option =” – install-scripts = / usr / local / bin”)。 使用多個–install-option選項可以將多個選項傳遞給setup.py安裝。 如果將選項與目錄路徑一起使用,請確保使用絕對路徑。

pip install --install-option test

將發出

setup.py test

那么您需要將setup.cfg與setup.py放在同一目錄中:

# setup.cfg
[aliases]
test=pytest

示例setup.py:

# setup.py
"""Setuptools entry point."""
import codecs
import os

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup


CLASSIFIERS = [
    'Development Status :: 5 - Production/Stable',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: MIT License',
    'Natural Language :: English',
    'Operating System :: OS Independent',
    'Programming Language :: Python',
    'Topic :: Software Development :: Libraries :: Python Modules'
]

dirname = os.path.dirname(__file__)

long_description = (
    codecs.open(os.path.join(dirname, 'README.rst'), encoding='utf-8').read() + '\n' +
    codecs.open(os.path.join(dirname, 'CHANGES.rst'), encoding='utf-8').read()
)

setup(
    name='your_package',
    version='0.0.1',
    description='some short description',
    long_description=long_description,
    long_description_content_type='text/x-rst',
    author='Your Name',
    author_email='your@email.com',
    url='https://github.com/your_account/your_package',
    packages=['your_package'],
    install_requires=['pytest',
                      'typing',
                      'your_package'],
    classifiers=CLASSIFIERS,
    setup_requires=['pytest-runner'],
    tests_require=['pytest'])

有一種方法:

import pkg_resources as pkr
packages = [(v.project_name, v.version) for v in pkr.working_set]
print (packages)
# [('zope.interface', '4.5.0'), ..., ('absl-py', '0.2.2')]

這將為您提供一個元組列表,然后您可以對其進行過濾和搜索以查找它們是否與您所需的匹配。
簡單的例子:

if packages[-1] == ('absl-py', '0.2.2'):
    print ('aye')
#aye

package_dict = dict(packages) 
#this converts the packages into a dictionary format
#However, you can simply open a file via
#with open('packages.txt', 'r') as f:
#process your packages into a dict object here, then use the below code    

for i,v in packages:
    if package_dict[i] == v:
        print ('yay') #will print yay multiple times

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM