title: How to test a Python CLI program with click, coverage.py, and Tox?
body: <p>I'm working on a CLI program using <a href="https://github.com/pallets/click" rel="noreferrer">click</a>, and I want to start adding some tests with code coverage analysis using <a href="https://coverage.readthedocs.io/en/v4.5.x/" rel="noreferrer">coverage.py</a>.</p>
  
  <p>I thought a good way to implement the tests would be to run the CLI itself using subprocess. However, coverage.py reports zero code coverage, presumably because the Python instance spawned by subprocess doesn't have the coverage.py instrumentation.</p>
  
  <p>I found <a href="https://coverage.readthedocs.io/en/latest/subprocess.html" rel="noreferrer">this link</a> that says I can drop a <code>sitecustomize.py</code> file in my PYTHONPATH to always force Python to start coverage measurement, but I'm using Tox to create a venv and run tests. I couldn't find any Tox settings that deal with this.</p>
  
  <p>I found <a href="https://stackoverflow.com/a/51663154/25005">this answer</a> that says I should just run my CLI through <code>coverage run</code>, but it looks like that only works if given a path to a Python script, and I'm trying to run my CLI through the entry point defined in <code>setup.py</code>. i.e. I have to change all my command lines in test code from <code>myprogram</code> to <code>coverage run myprogram/cli/cli.py</code>. I'd rather not do this because it's not the way I expect users to run the program.</p>
  
  <p>So it seems like the two options are:</p>
  
  <ol>
  <li><p>figure out a way to make <code>sitecustomize.py</code> work in Tox environments, or </p></li>
  <li><p>all command lines in test code to use a script path instead of an entry point (probably easier in the long run, but makes tests slightly more brittle and harder to understand). Wondering if there's anything else I'm missing.</p></li>
  </ol>