coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Coverage=5.3, Python=2.7, when i send new request to my server and collect the coverage, it does not include my new requests

Open Chenyu-dev353 opened this issue 1 year ago • 3 comments

The background of this issue is that I need to collect the coverage data of running Python project without interrupting the process. However I found that it always only shows the original coverage data for some constants, so I wrote a demo for this and found the same problem.

For some reason we have to use the shell file to create a new bootstrap.py file and start the application:

#!/bin/bash

# create bootstrap.py file
cat << 'EOF' > bootstrap.py
import gc

from coverage import Coverage
import signal
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
cov = Coverage(source=['/Users/bytedance/Desktop/demo'], data_suffix=True)
cov.erase()
cov.start()
count = 1
def exit_signal_handler(sig=None, frame=None):
    global count
    logging.info("exit signal handler for cov type: {}".format(count))
    count += 1
    global cov
    try:
        logging.info("stop cov")
        cov.stop()
        logging.info("stop cov success")
    except Exception as e:
        logging.error("stop cov fail: {}".format(e))
    try:
        logging.info("save cov")
        cov.save()
        logging.info("save cov success")
    except Exception as e:
        logging.error("save cov fail: {}".format(e))
    try:
        del cov
        gc.collect()
        logging.info("create new cov instance")
        cov = Coverage(source=['/Users/bytedance/Desktop/demo'], data_suffix=True)
        cov.start()
        logging.info("new cov instance started successfully")
    except Exception as e:
        logging.error("new cov instance start fail: {}".format(e))

signal.signal(signal.SIGUSR2, exit_signal_handler)

from flask import Flask

app = Flask(__name__)

from views import *

if __name__ == '__main__':
    app.run(debug=True, threaded=False, processes=10)

EOF

export COVERAGE_PROCESS_START=/Users/bytedance/Desktop/demo/.coveragerc

exec python -m coverage run bootstrap.py

the code above is my bootstrap.py, where I started my Python project, and it also has one useful subfile(views.py) as below:

1. from bootstrap import app
2. import time
3. import time
4. 
5. 
6. @app.route('/home')
7. def home():
8.    return 'Hello, Flask!'
9.
10.
11. @app.route('/demo')
12. def demo():
13.     x = 1
14.     return str(x)
15.
16.
17. @app.route('/test')
18. def test():
19.    x = 1
20.    y = 2
21.    return str(x + y)

The coveragerc file is also attached here:

[run]
parallel = True
concurrency = multiprocessing

[report]
exclude_lines =
    pragma: no cover
    def __repr__
    if self\.debug
    raise AssertionError
    raise NotImplementedError
    if 0:
    if __name__ == .__main__.:
    ^import\s+
    ^from.*import.*

ignore_errors = True
fail_under = 0
skip_covered = False
sort = Cover

The version for Coverage.py and my Python version is 5.3 and 2.7 separately, and here is what i do:

  1. Start the project by running bootstrap.sh
  2. execute this:
pids=$(ps -ef | grep Python | grep -v grep | awk '{print $2}') 
kill -SIGUSR2 $pids
  1. execute: coverage combine && coverage report && mv .coverage .coverage.before (otherwise the original coverage data file will be covered by coverage combine but that's not that important)
  2. I found that the missing lines of views.py are line 6-21, which is right because I have excluded all the "import" lines
  3. Then I sent a GET request to 127.0.0.1:5000/home, and execute as step 2 and 3
  4. the missing lines of views.py stay as line 6-21, which is incorrect.

Please inform me if you have any idea of this problem, as our project is using Python 2.7 and cannot be updated to Python 3 temporarily, Thank you for your help

Chenyu-dev353 avatar Sep 13 '24 03:09 Chenyu-dev353