-
-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coverage fails with os.fork and os._exit #310
Comments
Your test run data is incorrect, it actually is:
I'm not sure what I can do to fix this. os._exit skips any further work in the process, including the work coverage.py needs to do to write out its measured data. |
Original comment by Marc Schlaich (Bitbucket: schlamar, GitHub: schlamar)
Bitbucket has stripped some new lines ;)
Yes, I feared this cannot be handled automatically. Anyway, it would be enough if I can do the clean up manually before os._exit but AFAIS there is no useful entry point in the API. Something like |
Issue #312 was marked as a duplicate of this issue. |
Original comment by Ryan Stuart (Bitbucket: rstuart85, GitHub: rstuart85) Here is a dirty hack to work around this issue:
It can be used as follows:
|
@rstuart85 If I read this code right, it's a way to get access to the coverage instance (if any), and then calling it explicitly from the product code. If the product code can be changed to accommodate this problem, then there's lots of possibilities, but people generally don't want to do that. |
@schlamar The method you want is currently called |
Original comment by Marc Schlaich (Bitbucket: schlamar, GitHub: schlamar) There is no other way than explicitly calling @rstuart85 pytest-cov can handle py.process.ForkedFunc since py's latest release yesterday. Relevant commits are: |
Original comment by Ryan Stuart (Bitbucket: rstuart85, GitHub: rstuart85) @schlamar if you want to acheive 100% test coverage then, AFAIK, pytest-cov is not a workable solution. It gets invoked too late to capture all lines. See http://stackoverflow.com/questions/16404716/using-py-test-with-coverage-doesnt-include-imports/16524426#16524426 for example. |
Hmm, we have a few ideas here, let's not close this just yet. And somehow, no one has suggested monkey-patching os._exit yet. |
Original comment by Marc Schlaich (Bitbucket: schlamar, GitHub: schlamar) @rstuart85 This is fixed in pytest-cov 1.7.0 (pytest-dev/pytest-cov#4), please give it a try! (This is even mentioned in the linked SO answer if you would have fully read it...) @nedbat Yes, that would work: pytest-dev/pytest-cov@fb100db :) However, I wouldn't dare to put something like this in production... |
Original comment by Marc Schlaich (Bitbucket: schlamar, GitHub: schlamar) @rstuart85 As already said, I have no idea how to implicitly support os._exit. pytest-cov supports However, in theory it should work with sys.exit. If that's not working, please report it at https://github.com/schlamar/pytest-cov with a test case. Thanks! |
Original comment by space one (Bitbucket: spaceone, GitHub: spaceone) Any news on this? My application doesn't use pytest. (does it work with pytest?). I use os.fork() and the forked process doesn't write any results anymore. I monkey patched os.['execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'fork', '_exit'] similar to https://bitbucket.org/ned/coveragepy/issues/43/coverage-measurement-fails-on-code but this only writes the results prior to forking. |
@spaceone can you provide a reproducible example? |
Original comment by Loic Dachary (Bitbucket: dachary, GitHub: dachary) @spaceone when running the proposed reproducer I get the following ouptut:
The code it contains is more complicated than the reproducer provided in the description of this issue. It would be great if you could explain why and how it demonstrates something different. |
* execute "python-coverage report" with python3 * coverage.process_startup() returns None if coverage was already started. This is the case on os.fork(). * update broken bitbucket links: nedbat/coveragepy#310 nedbat/coveragepy#43
I am not sure why is this "exotic". Using os._exit is the recommended way to exit from a forked child. Anyway, I workaround this by simple patching the
The resulting reports can be combined as usual after that. |
"exotic" wasn't intended to mean "low-priority", but "needs research into a thing I am not familiar with." Thanks for sharing a workaround. |
Originally reported by Marc Schlaich (Bitbucket: schlamar, GitHub: schlamar)
It is a common pattern to exit a child process with
os._exit
after a fork (ref). However, in this case coverage for the child process fails.Example:
Test run:
The text was updated successfully, but these errors were encountered: