jeudi 9 mars 2017

Testing Code In IPython Under Tox Control

I have a fairly extensive multi-version test suite based on tox and pytest. It's working well, with 80% coverage across 8 versions of Python:

[tox]
envlist = py2{6,7}, pypy, py3{3,4,5,6}, pypy3

According to coverage.py, the vast majority of untested lines are designed to integrate with interactive execution, particularly IPython (secondarily the out-of-the-box Python REPL). I tried adding ipython instances to the tox config.

[tox]
envlist = py2{6,7}, pypy, ipython2, py3{3,4,5,6}, pypy3, ipython3

This runs great! Except...they don't trigger any actual IPython features. I am testing "is it running under IPython?" thus:

try:
    from IPython import get_ipython
    __IPYTHON__
    _IPY = True
except (NameError, ImportError):
    _IPY = False

This discrimination code works great in practice, and it works great if dropped into a file and run with either python or ipython:

$ python itest.py
running under IPython: False
$ ipython itest.py
running under IPython: True

It decidedly does not work as expected, however, when run under tox. The coverage report shows the _IPY = True line (and everything that depends on it) is never executed. It's as though, under tox, standard Python interpreter code is being used rather than IPython.

The still-untested code in question absolutely depends on IPython structures such as get_ipython().history_manager.input_hist_parsed, and it only has trouble accessing it under tox. I guess I could gin-up some sort of Expect-like interface, running IPython or the REPL as test slaves. Though getting solid coverage numbers out of that setup seems dicey. Rather than that kind of Rube Goldberg device, how can I test code on IPython, but running under standard tox/pytest control?

Aucun commentaire:

Enregistrer un commentaire