{"id":43,"date":"2022-04-21T14:52:04","date_gmt":"2022-04-21T14:52:04","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/alwaysff\/?p=43"},"modified":"2022-04-21T15:03:24","modified_gmt":"2022-04-21T15:03:24","slug":"using-mocks-in-unittests","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/alwaysff\/2022\/04\/21\/using-mocks-in-unittests\/","title":{"rendered":"\/\/ using mocks in unittests"},"content":{"rendered":"\n<pre class=\"wp-block-code has-medium-font-size\"><code>always_ff @(posedge clk) begin<\/code><\/pre>\n\n\n\n<p>In my previous internship, my role as a Software Testing Automation Intern threw me into the world of mocks. I hadn\u2019t taken Software Engineering II yet, so the concept of mocks itself was unknown to me. One of my projects that summer had me writing unit tests to provide code coverage for an in-house software. This was very much white box testing, and had me focusing on providing statement, decision and condition coverage.<\/p>\n\n\n\n<p>There were about 15 modules, and I created a test module for each individual module under test. Since this required me to write unit tests, I had to make sure that I was focused on testing <strong>ONLY<\/strong> that unit or method itself. By utilizing mocks, I could replace function calls, thereby eliminating any potential error from appearing that did not have to do with the unit\u2019s logic itself. Additionally, it allowed me to provide coverage by forcing an exception (to test that the unit handled the exception(s) properly) or returning specific values (to test that the unit responded as intended to specific values). Since a lot of the units had calls to other functions, I needed to use patch to be able to intercept the function call to do as I described above.<\/p>\n\n\n\n<p>To provide a quick visual example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><strong># module name: practiceModule.py<\/strong>\nimport requests\n\ndef practice_method():\n  <em>\u201c\u201d\u201d\n    Makes a get request to 'https:\/\/oregonstate.edu\/' and\n    returns the object returned by the request.\n    if exception occurs, returns string with exception message.\n  \u201d\u201d\u201d<\/em>\n  try:\n    obj = requests.get('https:\/\/oregonstate.edu\/')\n    return obj\n  except Exception:\n    return \"Exception occurred\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\" style=\"border-radius:0px\"><code><strong># module name: testModule.py<\/strong>\nfrom unittest import TestCase, mock\nimport practiceModule\n\n\nclass TestPracticeModule(TestCase):\n  <em># will intercept the get function called in practiceModule<\/em>\n  @mock.patch('practiceModule.requests.get\u2019)\n  def test1(self, mock_requests):\n    \u2018\u2019\u2019\n      example of using return_value\n    \u2019\u2019\u2019\n    <em># setting the return value<\/em>\n    mock_requests.return_value = \"&lt;(Mock)Response &#091;999]&gt;\"\n    self.assertTrue(practiceModule.practice_method(),    \n                    mock_requests.return_value)\n    mock_requests.assert_called()\n\n\n  @mock.patch(\u2018practiceModule.requests.get')\n  def test2(self, mock_requests):\n    <em>\u2018\u2019\u2019\n      example of using side_effect\n    \u2019\u2019\u2019<\/em>\n    <em># force raising the exception, 'Exception'\n    # note: if I had multiple exceptions to test, I would<\/em>\n    <em>revise as such -&gt; mock_requests.side_effect = &#091;KeyError,<\/em>\n    <em>ValueError, (etc.)] and each call to the practice_module <\/em>\n    <em>function would return the next exception in the list.<\/em>\n    mock_requests.side_effect = Exception\n    self.assertTrue(practiceModule.practice_method(), \n                    \"Exception occurred\")\n    <em># asserts that the patched function was called<\/em>\n    mock_requests.assert_called()<\/code><\/pre>\n\n\n\n<p>This is a very basic example (and may not be the best or most detailed one), but it demonstrates how useful the Mock library can be when it comes to unit testing. As testing moves on to integration and system level testing, the use of mocks would reduce significantly, for obvious reasons. But, from my experience at least, at the unit level the use of it is significant.<\/p>\n\n\n\n<p class=\"has-small-font-size\"><strong>References<\/strong>: https:\/\/www.geeksforgeeks.org\/get-post-requests-using-python\/ (to figure out how to make a get request in python)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>end<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my previous internship, my role as a Software Testing Automation Intern threw me into the world of mocks. I hadn\u2019t taken Software Engineering II yet, so the concept of mocks itself was unknown to me. One of my projects that summer had me writing unit tests to provide code coverage for an in-house software. [&hellip;]<\/p>\n","protected":false},"author":12365,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/users\/12365"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":12,"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":56,"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/posts\/43\/revisions\/56"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/alwaysff\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}