{"id":1202,"date":"2013-08-11T01:00:00","date_gmt":"2013-08-10T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1202"},"modified":"2013-08-13T20:14:03","modified_gmt":"2013-08-13T19:14:03","slug":"python-unit-testing","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1202","title":{"rendered":"Python Unit Testing"},"content":{"rendered":"<p>I discussed C++ unit testing last time; I thought this time I\u00e2\u20ac\u2122d make a quick mention of unit testing in Python with the <a href=\"http:\/\/docs.python.org\/2\/library\/unittest.html\">unittest<\/a> module (which uses a very similar structure to CppUnit \u00e2\u20ac\u201c mostly because they are both based on JUnit).<\/p>\n<p>The introspective abilities of dynamic languages like Python make unit test frameworks really easy to use.<\/p>\n<pre class=\"sourceCode python\"><code class=\"sourceCode python\"><span class=\"co\">#<\/span>\n<span class=\"co\"># Class:    TemplateUnitTest<\/span>\n<span class=\"co\"># Description:<\/span>\n<span class=\"co\">#<\/span>\n<span class=\"kw\">class<\/span> TemplateUnitTest(unittest.TestCase):\n    <span class=\"kw\">def<\/span> setUp(<span class=\"ot\">self<\/span>):\n        <span class=\"co\"># Called once per test<\/span>\n        logging.basicConfig(<span class=\"dt\">format<\/span>=<span class=\"st\">&#39;[<\/span><span class=\"ot\">%(levelname)-5.5s<\/span><span class=\"st\">]  <\/span><span class=\"ot\">%(message)s<\/span><span class=\"st\">&#39;<\/span>, level=logging.INFO)\n        logging.info(<span class=\"st\">&quot;*** UNITTEST Start&quot;<\/span>)\n\n    <span class=\"co\"># tests have to be prefixed with &quot;test_&quot;<\/span>\n    <span class=\"kw\">def<\/span> test_example(<span class=\"ot\">self<\/span>):\n        <span class=\"co\"># self.assertTrue(element in self.seq)<\/span>\n        <span class=\"co\"># self.assertEqual(self.seq, range(10))<\/span>\n        <span class=\"co\"># self.assertRaises(TypeError, random.shuffle, (1,2,3))<\/span>\n        <span class=\"kw\">pass<\/span>\n\n\n<span class=\"co\"># -------------- Module check<\/span>\n<span class=\"co\">#<\/span>\n<span class=\"co\"># __name__ is set to &quot;__main__&quot; when this is the top module<\/span>\n<span class=\"co\"># if this module is loaded because of an &quot;import&quot; then this<\/span>\n<span class=\"co\"># won&#39;t get run -- perfect<\/span>\n<span class=\"kw\">if<\/span> <span class=\"dt\">__name__<\/span> == <span class=\"st\">&quot;__main__&quot;<\/span>:\n    suite = unittest.TestLoader().loadTestsFromTestCase(TemplateUnitTest)\n    sys.exit(unittest.TextTestRunner(verbosity=<span class=\"dv\">2<\/span>).run(suite))<\/code><\/pre>\n<p>While this is perfectly acceptable, for a real project you\u00e2\u20ac\u2122ll likely want to run unit tests using the command line interface. That leaves you able to run your own code from the \u00e2\u20ac\u0153if <strong>name<\/strong>\u00e2\u20ac\u009d block. You can do that with python\u00e2\u20ac\u2122s \u00e2\u20ac\u0153<code>-m<\/code>\u00e2\u20ac\u009d switch for running a particular module directly.<\/p>\n<pre><code>$ python -m unittest YOUR_MODULE<\/code><\/pre>\n<p>Alternatively, you can run test discovery automatically<\/p>\n<pre><code>$ python -m unittest discover -p &quot;*.py&quot;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I discussed C++ unit testing last time; I thought this time I\u00e2\u20ac\u2122d make a quick mention of unit testing in Python with the unittest module (which uses a very similar structure to CppUnit \u00e2\u20ac\u201c mostly because they are both based on JUnit). The introspective abilities of dynamic languages like Python make unit test frameworks really\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1202\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[108,6,106],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1202"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1202"}],"version-history":[{"count":1,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1202\/revisions"}],"predecessor-version":[{"id":1203,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1202\/revisions\/1203"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}