1

I am using vimrunner-python library to test my vim plugin written in python with py-test and pytest-cov.

Vimrunner python executes a vim server and controls a client vim instance via the server remote interface.

However, pytest-cov (obviously) does not see the lines executed by the vim process. Is there a way how to make this work, i.e. point the coverage to the vim's server PID?

4

1 回答 1

0

You need to run the coverage measurement from the plugin itself, i.e. like this:

# Start measuring coverage if in testing
if vim.vars.get('measure_coverage'):
    import os
    import atexit
    import coverage
    coverage_path = os.path.expanduser('~/coverage-data/.coverage.{0}'.format(os.getpid()))
    cov = coverage.coverage(data_file=coverage_path)
    cov.start()

    def save_coverage():
        cov.stop()
        cov.save()

    atexit.register(save_coverage)

If the plugin was invoked multiple times, you will need to combine the coverage files, using the coverage tool:

 $ cd ~/coverage-data
 $ coverage combine

This will generate combined .coverage file, which can be then used to generate the desired report.

Note: Make sure you're executing the measurement part only once per vim instance, otherwise the coverage file might get rewritten. In such case, another source of uniqueness (i.e. random number) other than PID should be used to generate the name of the .coverage file.

于 2015-03-27T22:35:44.563 回答