{"id":726,"date":"2022-02-24T13:58:55","date_gmt":"2022-02-24T18:58:55","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=726"},"modified":"2024-02-09T14:44:34","modified_gmt":"2024-02-09T19:44:34","slug":"python-unit-testing-example","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-unit-testing-example\/","title":{"rendered":"Python Unit Testing Example"},"content":{"rendered":"\n<p>Unit testing functionality is built into Python3. Following is a very simple example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def Arithmetic(a)\n    return a * a\n<\/code><\/pre>\n\n\n\n<p>Save this file as mycode.py. Save the following code as mycode_unittest.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import unittest\nfrom mycode import Arithmetic\n\nclass ArithmeticTest(unittest.TestCase):\n\n    def setUp(self):\n        self.arr1 = ((0,0),(1,1))\n        self.arr2 = ((1,2),(3,4))\n\n    def testCalculation(self):\n        for (i,val) in self.arr1:\n            self.assertEqual(Arithmetic(i), val)\n        for (i,val) in self.arr2:\n            self.assertNotEqual(Arithmetic(i), val)\n\n    def tearDown(self):\n        self.arr1 = None\n        self.arr2 = None\n\nif __name__ == \"__main__\": \n    unittest.main()\n<\/code><\/pre>\n\n\n\n<p>When you run the unit test code, you will not get any errors. Change assertEqual to assertNotEqual. Then you will get unit test failure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing functionality is built into Python3. Following is a very simple example: Save this file as mycode.py. Save the following code as mycode_unittest.py When you run the unit test code, you will not get any errors. Change assertEqual to assertNotEqual. Then you will get unit test failure.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[76,137],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-python","tag-programming","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=726"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":727,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/726\/revisions\/727"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}