Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You must make sure that you consistently follow some naming conventions (which you seem to be doing):</p> <ol> <li><p>All tests are named with the same prefix (<code>test_</code> is the norm), followed by the name of the module you wish to test.</p> <p><code>prog.py</code> => <code>test_prog.py</code></p></li> <li><p>Tests reside in <code>test/</code> directory.</p></li> </ol> <p>Then you can do something like this:</p> <h2>prog.py</h2> <pre><code>import sys ... ... do module stuff here... ... if __name__ == "__main__": # Check if we want to run the tests for this file if "--unittest" in sys.argv: import unittest test_filename = 'test_' + __file__ test_directory = 'test' suite = unittest.TestLoader().discover(test_directory, pattern=test_filename) unittest.TextTestRunner(verbosity=2).run(suite) </code></pre> <hr> <p>What we are doing, is:</p> <ul> <li><p>Checking the command arguments to see if <code>--unittest</code> is present (since that's the only time you want to run the tests).</p></li> <li><p>If it is, then we create the <code>test_prog.py</code> - following the naming conventions we have set.</p></li> <li><p>Then we pass that to the <code>TestLoader().discover</code> function. </p> <p><a href="http://docs.python.org/2/library/unittest.html#unittest.TestLoader.discover" rel="noreferrer">discover(...)</a> starts at the specified directory and finds all test modules (recursing into subdirectories ) that match the pattern provided.</p> <p>In our case, it will look inside the <code>test/</code> directory for any module named <code>test_prog.py</code>. When it does, it loads it and creates a TestSuite with the TestCases that we want to run.</p></li> <li><p>Lastly, we manually test <code>unittest</code> to run the <code>suite</code> obtained in the previous step.</p></li> </ul> <hr> <p>Normally, <code>unittest</code> will do all of this for us in the background, but since we are trying to run a specific test module, we have to tell exactly how and where to get it from.</p> <p>Also, note that you will have to do this for <strong>every</strong> file where you want to do this at.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload