如何将pytest与viralenv一起使用?
问题描述
我将pytest安装到虚拟环境中(使用virtualenv),并从该虚拟环境运行它,但它没有使用我在该虚拟环境中安装的软件包。相反,它使用的是主系统包。(使用python -m unittest discover,我实际上可以使用正确的python和包运行我的测试,但是我想使用py.test框架。)
是否可能py.test实际上没有在虚拟环境中运行pytest,我必须指定要运行哪个pytest?
如何让py.test仅使用我的viralenv中的python和包?
另外,由于我系统上有多个版本的Python,如何知道Pytest使用的是哪个Python?它将在我的虚拟环境中自动使用Python,还是必须以某种方式指定?解决方案
要使其正常工作需要一些技巧:
- 激活您的venv:
source venv/bin/activate - 安装pytest:
pip install pytest - 重新激活您的静脉:
deactivate && source venv/bin/activate
原因是pytest在venv中实际安装了pytest之后,才由source设置到pytest文件的路径。您不能在安装前设置某些内容的路径。
虚拟环境中安装的任何控制台入口点都需要重新启动activate。
相关文章