How to get Pycharm's coverage tool to work with monkey patching?

def test_do_something():
    import custom_pandas_methods # patches collapse into pd.DataFrame
    df = pd.DataFrame({
        'a': [1,2,3],
        'b': [1,2,3],
    })
    assert df.collapse('a') == 5

I'm running the above test in Pycharm using pytest (with the --cov option). It works fine when run without Pycharm's coverage tool.

However, when I "run with coverage", the test fails i.e.,

FPatching collapse into pandas.core.frame.DataFrame
E       RuntimeError: dictionary changed size during iteration

The RuntimeError occurs in collapse.py, which contains the collapse method. Why would I get such an error?

1 comment

I asked this question on stackoverflow as well:

https://stackoverflow.com/questions/54581331/how-to-get-pycharms-coverage-tool-to-work-with-monkey-patching?noredirect=1#comment95962312_54581331 

Note that, on further review, monkey patching does NOT appear to be the issue.  I rewrote the test using a simple function and I get the same result i.e., Pycharm "test" works; Pycharm "test with coverage" fails.  I still struggling to see what could cause this.

0

Please sign in to leave a comment.