Python coverage子进程coverage.process_startup()错误

问题描述 投票:1回答:1

Python版本3.7.5,覆盖范围5.0.3。

[试图学习,如何衡量子流程的覆盖率,我创建了一个由3个文件组成的小项目。

main.py:

import subprocess
import coverage
coverage.process_startup()
subprocess.run(['python', 'subprocess_script.py'])

subprocess_script.py:

print('123')

。coveragerc:

[run]
branch = True
data_file = coverage-data-new
source = .

环境变量“ COVERAGE_PROCESS_START”链接到该项目中的.coveragerc。

执行coverage run main.py后,出现此错误:

C:\Users\Kirill.Brusinets\PycharmProjects\test_subprocess_coverage>coverage run main.py
123
self._collectors:
  <Collector at 0x2f75f48: CTracer>
           _run_module_as_main : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\runpy.py:193
                     _run_code : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\runpy.py:85
                      <module> : C:\Users\Kirill.Brusinets\AppData\Local\Programs\Python\Python37\Scripts\coverage.exe\__main__.py:9
                          main : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py:828
                  command_line : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py:555
                        do_run : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py:700
                         start : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\control.py:518
               _init_for_start : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\control.py:449
                      __init__ : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\collector.py:111
  <Collector at 0x2fede08: CTracer>
           _run_module_as_main : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\runpy.py:193
                     _run_code : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\runpy.py:85
                      <module> : C:\Users\Kirill.Brusinets\AppData\Local\Programs\Python\Python37\Scripts\coverage.exe\__main__.py:9
                          main : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py:828
                  command_line : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py:555
                        do_run : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py:703
                           run : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\execfile.py:247
                      <module> : main.py:3
               process_startup : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\control.py:1094
                         start : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\control.py:518
               _init_for_start : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\control.py:449
                      __init__ : c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\collector.py:111
Traceback (most recent call last):
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Kirill.Brusinets\AppData\Local\Programs\Python\Python37\Scripts\coverage.exe\__main__.py", line 9, in <module>
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py", line 828, in main
    status = CoverageScript().command_line(argv)
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py", line 555, in command_line
    return self.do_run(options, args)
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\cmdline.py", line 708, in do_run
    self.coverage.stop()
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\control.py", line 542, in stop
    self._collector.stop()
  File "c:\users\kirill.brusinets\appdata\local\programs\python\python37\lib\site-packages\coverage\collector.py", line 333, in stop
    "Expected current collector to be %r, but it's %r" % (self, self._collectors[-1])
AssertionError: Expected current collector to be <Collector at 0x2f75f48: CTracer>, but it's <Collector at 0x2fede08: CTracer>

如果将coverage.process_startup()放入subprocess_script.py,则不会出现任何错误,但是覆盖率仅在main.py上进行测量。如何运作?

python subprocess coverage.py
1个回答
0
投票

subprocess.run(['python', 'subprocess_script.py'])开始一个新的子过程。它与您在pycharm中所做的无关。启动覆盖时,覆盖将在同一解释器上进行侦听。子流程在当前执行环境之外创建一个新的解释器。因此,您的报道范围永远不会听其他翻译。

为什么要将其作为子流程运行?另一种解决方案是将subprocess_script导入主脚本并调用可执行函数。

© www.soinside.com 2019 - 2024. All rights reserved.