在 virtualenv 中运行覆盖 [英] Running coverage inside virtualenv
问题描述
我最近偶然发现了在虚拟环境中运行覆盖率测量的一些问题.我不记得过去有类似的问题,我也无法在网上找到解决方案.
I recently stumbled upon some issue with running coverage measurements within virtual environment. I do not remember similar issues in the past, nor I was able to find solution on the web.
基本上,当我尝试在 virtualenv 中运行测试套件时,它工作正常.但是,当我尝试使用 coverage
进行操作时,由于缺少所需的模块而失败.根据 StackOverflow 上的一些答案,我检查了我的脚本,发现 coverage
使用不同的解释器,即使从同一个 virtualenv
内部运行.
Basically, when I am trying to run test suite in virtualenv, it works fine. But as soon, as I try to do it using coverage
, it fails because of lack of modules it requires. Based on some answer on StackOverflow I checked my script and found out that coverage
uses different interpreter, even if running from inside the same virtualenv
.
复制方法如下:
$ virtualenv --no-site-packages venv
New python executable in venv/bin/python
Installing Setuptools................................................done.
Installing Pip.......................................................done.
$ source venv/bin/activate
(venv)$ echo 'import sys; print(sys.executable)' > test.py
(venv)$ python test.py
/home/tadeck/testground/venv/bin/python
(venv)$ coverage run test.py
/usr/bin/python
问题是:如何让coverage
与虚拟环境无缝协作?我可以更改 sys.path
或在系统范围内安装所需的模块,但必须有更简洁的方法.
The question is: how to make coverage
work with virtual environment seamlessly? I could alter sys.path
or install required modules system-wide, but there has to be a cleaner way.
推荐答案
pip install coverage
in your new venv
pip install coverage
in your new venv
[alex@gesa ~]$ virtualenv venv
[alex@gesa ~]$ source venv/bin/activate
(venv)[alex@gesa ~]$ pip install coverage
(venv)[alex@gesa ~]$ echo 'import sys; print(sys.executable)' > test.py
(venv)[alex@gesa ~]$ python test.py
/home/alex/venv/bin/python
(venv)[alex@gesa ~]$ coverage run test.py
/home/alex/venv/bin/python
(venv)[alex@gesa ~]$
这篇关于在 virtualenv 中运行覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!