Using Command Line Parameters with pytest --pyargs
<p>I have written pytest unit tests for a package I've written with a structure like below:</p>

<ul>
<li>packagedir

<ul>
<li>setup.py</li>
<li>mypackage

<ul>
<li>__init__.py</li>
<li>functionpackage

<ul>
<li>__init__.py</li>
<li>functionmodule.py</li>
</ul></li>
<li>test

<ul>
<li>__init__.py</li>
<li>conftest.py</li>
<li>unit_tests

<ul>
<li>__init__.py</li>
<li>functionmodule_test.py</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>

<p>Now packagedir/mypackage/test/conftest.py establishes two required command line parameters to be created as fixtures for the tests. When I run something like:</p>

<pre><code>pytest packagedir/mypackage/ --var1 foo --var2 bar
</code></pre>

<p>All the tests run and the command line parameters are read in correctly. However, when I pip install the package (which installs the test and unit_tests packages correctly) and then try something like this:</p>

<pre><code>pytest --pyargs mypackage --var1 foo --var2 bar
</code></pre>

<p>I get an error like this:</p>

<pre><code>usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --var1 foo --var2 bar
</code></pre>

<p>If I run:</p>

<pre><code>pytest --pyargs mypackage
</code></pre>

<p>the tests run but fail because the command line arguments are not present.</p>

<p>Can someone explain to me what might be going wrong here? I could guess and say this is because the --pyargs argument changes the interpretation of the command line parameters for pytest, but it's just a guess. I'd like to know if there is an actual way I could fix this. </p>

<p><strong>The overarching goal would be to allow someone to pip install mypackage and then be able to easily test it without navigating to the site-packages location of the install.</strong></p>