Running single test in unittest python
<p>I have a test_method() in a directory /tests. I want to run a single test from the command line. Instead of using test_module.TestClass.test_method I wish to just mention it as</p>

<p>$ ./test-runner test_method</p>

<p>and let the discover decide the test_method belongs to which test_module and in which TestClass. I am able to get the test_module by running a search based on strings in all test_modules present as test_module is python file. But how to get the TestClass holding the test_method within the file? Any idea?</p>

<p>Instead of doing that yourself, you ought to use <a href="http://nose.readthedocs.io/en/latest/" rel="nofollow">nose</a> with the <code>nosetests</code> command, or, better, use <a href="http://doc.pytest.org/en/latest/" rel="nofollow">py-test</a> (and <a href="https://testrun.org/tox/latest/" rel="nofollow">tox</a>).</p>

<p>The classic usage with unit test is to add a main in your module:</p>

<pre><code>if __name__ == "__main__":
    unittest.main()
</code></pre>

<p>See the <a href="https://docs.python.org/2/library/unittest.html#basic-example" rel="nofollow">Basic example</a> in the doc</p>

<p>The main function has parameters used to choose the tests to run. See <a href="https://docs.python.org/2/library/unittest.html#unittest.main" rel="nofollow">unittest.main</a> in the doc. </p>

python python-unittest