// using mocks in unittests


always_ff @(posedge clk) begin

In my previous internship, my role as a Software Testing Automation Intern threw me into the world of mocks. I hadn’t 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.

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 ONLY 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’s 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.

To provide a quick visual example:

# module name: practiceModule.py
import requests

def practice_method():
  “””
    Makes a get request to 'https://oregonstate.edu/' and
    returns the object returned by the request.
    if exception occurs, returns string with exception message.
  ”””
  try:
    obj = requests.get('https://oregonstate.edu/')
    return obj
  except Exception:
    return "Exception occurred"
# module name: testModule.py
from unittest import TestCase, mock
import practiceModule


class TestPracticeModule(TestCase):
  # will intercept the get function called in practiceModule
  @mock.patch('practiceModule.requests.get’)
  def test1(self, mock_requests):
    ‘’’
      example of using return_value
    ’’’
    # setting the return value
    mock_requests.return_value = "<(Mock)Response [999]>"
    self.assertTrue(practiceModule.practice_method(),    
                    mock_requests.return_value)
    mock_requests.assert_called()


  @mock.patch(‘practiceModule.requests.get')
  def test2(self, mock_requests):
    ‘’’
      example of using side_effect
    ’’’
    # force raising the exception, 'Exception'
    # note: if I had multiple exceptions to test, I would
    revise as such -> mock_requests.side_effect = [KeyError,
    ValueError, (etc.)] and each call to the practice_module 
    function would return the next exception in the list.
    mock_requests.side_effect = Exception
    self.assertTrue(practiceModule.practice_method(), 
                    "Exception occurred")
    # asserts that the patched function was called
    mock_requests.assert_called()

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.

References: https://www.geeksforgeeks.org/get-post-requests-using-python/ (to figure out how to make a get request in python)

end
Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *