[英]coverage.py does not cover script if py.test executes it from another directory
我有一個python腳本,它接受命令行參數,使用一些文件。 我正在寫成功測試與py.test
把這個腳本通過其步伐,與執行它subprocess.call
。
現在我想用coverage.py
分析代碼coverage.py
。 當通過pytest-cov
插件(內置子進程處理)使用時, 覆蓋范圍從使用py.test
的tmpdir
fixture創建的臨時測試目錄調用時, 看不到/覆蓋我的腳本 。 Coverage在其所在的目錄中調用時會看到我的腳本(並且filename參數指向遠程路徑)。
在這兩種情況下,我的測試通過 ! 覆蓋范圍3.6,pytest-2.3.5,pytest-cov 1.6,全部來自PyPi。
問題:即使在另一個目錄中執行腳本,如何識別我的腳本? 這是覆蓋范圍內的錯誤,還是一些無法做到的事情? 如果后者畢竟是tmpdir
是py.test的股票機制,那會感到驚訝......
最小的例子:
我得到了一個腳本my_script.py
,它只是arg_file.txt
通過命令行參數提供的文件arg_file.txt
的內容。 在兩個不同的測試中,這一次在tmpdir
調用,一次在腳本的位置。 兩個測試都通過了,但是在tmpdir測試中,我沒有得到任何覆蓋信息!
測試運行:
~/pytest_experiment$ py.test -s
=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 2 items
tests/test_in_scriptdir.py
set_up: In directory /tmp/pytest-52/test_10
Running in directory /home/cbuchner/pytest_experiment
Command: ./my_script.py /tmp/pytest-52/test_10/arg_file.txt
--Contents of arg_file.txt--
.
tests/test_in_tmpdir.py
set_up: In directory /tmp/pytest-52/test_11
Running in directory /tmp/pytest-52/test_11
Command: /home/cbuchner/pytest_experiment/my_script.py arg_file.txt
--Contents of arg_file.txt--
.
================================= 2 passed in 0.06 seconds =================================
覆蓋范圍:
~/pytest_experiment$ py.test --cov=my_script.py tests/test_in_scriptdir.py=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items
tests/test_in_scriptdir.py .
--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name Stmts Miss Cover
-------------------------------
my_script 3 0 100%
================================= 1 passed in 0.09 seconds =================================
~/pytest_experiment$ py.test --cov=my_script.py tests/test_in_tmpdir.py=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items
tests/test_in_tmpdir.py .Coverage.py warning: No data was collected.
--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name Stmts Miss Cover
---------------------------
================================= 1 passed in 0.09 seconds =================================
文件在這里: https : //gist.github.com/bilderbuchi/6412754
編輯: Interstingly,當使用-s
運行覆蓋測試時,也會有更好奇的輸出 - 覆蓋范圍警告說No data was collected
,顯然收集了數據,並且在tmpdir
測試中警告Module my_script.py was never imported.
??
~/pytest_experiment$ py.test -s --cov=my_script.py tests/test_in_scriptdir.py
=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items
tests/test_in_scriptdir.py
set_up: In directory /tmp/pytest-63/test_10
Running in directory /home/cbuchner/pytest_experiment
Command: ./my_script.py /tmp/pytest-63/test_10/arg_file.txt
--Contents of arg_file.txt--
Coverage.py warning: No data was collected.
.
--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name Stmts Miss Cover
-------------------------------
my_script 3 0 100%
================================= 1 passed in 0.09 seconds =================================
~/pytest_experiment$ py.test -s --cov=my_script.py tests/test_in_tmpdir.py=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 1 items
tests/test_in_tmpdir.py
set_up: In directory /tmp/pytest-64/test_10
Running in directory /tmp/pytest-64/test_10
Command: /home/cbuchner/pytest_experiment/my_script.py arg_file.txt
--Contents of arg_file.txt--
Coverage.py warning: Module my_script.py was never imported.
Coverage.py warning: No data was collected.
Coverage.py warning: Module my_script.py was never imported.
Coverage.py warning: No data was collected.
.Coverage.py warning: No data was collected.
--------------------- coverage: platform linux2, python 2.7.4-final-0 ----------------------
Name Stmts Miss Cover
---------------------------
================================= 1 passed in 0.09 seconds =================================
從tox調用“py.test --cov ...”時遇到了同樣的問題。 我在這個頁面上找到了一個提示: http : //blog.ionelmc.ro/2014/05/25/python-packaging/ ,盡管它沒有明確提到這一點。 對tox使用“--develop”將確保從覆蓋率分析的同一目錄中調用覆蓋數據收集。 tox.ini中的這一部分使我有一個覆蓋測試環境:
[tox]
envlist = ...,py34,cov
[testenv:cov]
# necessary to make cov find the .coverage file
# see http://blog.ionelmc.ro/2014/05/25/python-packaging/
usedevelop = true
commands = py.test --cov=<MODULE_NAME>
deps = pytest pytest-cov
當從另一個目錄運行測量的腳本時,這被證明是相對路徑混淆覆蓋的問題。 覆蓋率結果文件最終在該目錄中,而不是項目的根目錄。
為了解決這個問題,我停止使用pytest-cov
,而是使用純coverage
。 我使用完整路徑而不是相關路徑。
因此,例如,通過export COVERAGE_PROCESS_START=/full/path/to/.coveragerc
來定義啟用子export COVERAGE_PROCESS_START=/full/path/to/.coveragerc
覆蓋所需的環境變量。 在.coveragerc
,通過指定覆蓋結果文件
[run]
data_file = /full/path/to/.coverage
而且任何--source
和--include
選項也應該使用完整路徑。 然后就可以獲得正確的覆蓋率測量。
tox的另一個選項是在tox.ini
設置PYTHONPATH
:
[testenv] setenv = PYTHONPATH = {toxinidir} commands = pytest --cov=<your package> - codecov
根據這篇博客: https : //thomas-cokelaer.info/blog/2017/01/pytest-cov-collects-no-data-on-travis/
您應該在tests
文件夾中添加所有__init__.py
文件! 這個解決方案適合我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.