How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn?

流过昼夜 提交于 2019-12-01 22:25:45

Use coverage run to run your pexpect program and gather data:

If you usually do:

pexpect.spawn("python rift")

Then instead do:

pexpect.spawn("coverage run rift.py")

(Source)

After testing you will likely want to combine the pexpect results with the "regular" unit test results. coverage.py can combine multiple files into one for reporting.

Once you have created a number of these files, you can copy them all to a single directory, and use the combine command to combine them into one .coverage data file:

$ coverage combine

(Source)

Two additional details from testing:

  • In the test program (test_sys_2n_l0_l1.py) in this example, you must make sure that you have a delay between the moment that you terminate the pexpect spawn and the moment you terminate the test itself. Otherwise, coverage will not have time to write the results to .coverage. I added a sleep(1.0).

  • Used "coverage run --parallel-mode rift". This was needed to (a) make sure .coverage was not overwritten by later runs and (b) make "coverage combine" work (which is automatically run by "pytest --cov")

You basically have to enable subprocess coverage tracking.

I recommend using https://pypi.org/project/coverage_enable_subprocess/ to enable this easily.

Using parallel = 1 is recommended/required then, and you have to export COVERAGE_PROCESS_START, e.g. export COVERAGE_PROCESS_START="$PWD/.coveragerc".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!