Running unittest discover ignoring specific directory
<p>I'm looking for a way of running <code>python -m unittest discover</code>, which will discover tests in, say, directories A, B and C. However, directories A, B and C have directories named <code>dependencies</code> inside each of them, in which there are also some tests which, however, I don't want to run.</p>

<p>Is there a way to run my tests satisfying these constraints without having to create a script for this?</p>

<p>It would seem that <code>python -m unittest</code> descends into module directories but not in other directories.</p>

<p>It quickly tried the following structure</p>

<pre><code>temp
  + a
  - test_1.py
  + dependencies
    - test_a.py
</code></pre>

<p>With the result </p>

<pre><code>&gt;python -m unittest discover -s temp\a
test_1
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
</code></pre>

<p>However, if the directory is a module directory (containing a file <code>__init__.py</code>) the situation is different.</p>

<pre><code>temp
+ a
  - __init__.py
  - test_1.py
  + dependencies
    - __init__.py
    - test_a.py
</code></pre>

<p>Here the result was </p>

<pre><code>&gt;python -m unittest discover -s temp\a
test_a
.test_1
.
----------------------------------------------------------------------
Ran 2 tests in 0.009s

OK
</code></pre>

<p>The usefulness of this answer for you now depends on whether it is acceptable for your folder <code>dependencies</code> not to be a module directory.</p>

<p>EDIT: After seeing your comment</p>

<p>Would using <code>pytest</code> be an option? This test runner has many command arguments, one specifically to exclude tests.</p>

<p>See <a href="http://doc.pytest.org/en/latest/example/pythoncollection.html" rel="nofollow">Changing standard (Python) test discovery</a></p>

<p>From their site</p>

<blockquote>
  <p>Ignore paths during test collection</p>
  
  <p>You can easily ignore certain test directories and modules during collection by passing the <code>--ignore=path</code> option on the cli. pytest allows multiple <code>--ignore</code>  options</p>
</blockquote>

python python-2.7 python-unittest