pythonpython-2.7unit-testingpython-unittestpython-unittest.mock

Is there a way to mock .strip() for unit testing in Python 2.7's unittest module?


I am using Python 2.7 for a coding project. I'd love to switch to Python 3, but unfortunately I'm writing scripts for a program that only has a python package in 2.7 and even if it had one in 3 our codebase would be impractical to switch over, so it's not possible.

My code involves checking if a path it is given in string form exists, then because I don't know if os.path.exists does this itself, if it does not, it runs .strip() on the file name and tries again.

I am trying to unit test this. I've run a test on it not existing at all by patching os.path.exists to return False. But I can't figure out how to unit test the case where it returns False before .strip(), and True after.

Here is the relevant portion of the function being tested (the if/elif/else is relevant for the unit tests):

import os

class Runner:
  def __init__(self, fname):
    self.fname = fname
  
  def input_check(self):
    if not os.path.exists(self.fname):
      self.fname = self.fname.strip()
      if not os.path.exists(self.fname):
        raise ValueError('input is not a valid path')
    if os.path.isfile(self.fname):
      self.ftype = 'file'
    elif os.path.isdir(self.fname):
      self.ftype = 'folder'
    else:
      raise ValueError('how is your input neither a file nor a folder??')

And two examples of what I have tried for unit testing: Example 1

import unittest
from mock import patch

class TestRunner(unittest.TestCase):
  @patch('.strip')
  @patch('os.path.exists')
  def test_input_check_exists_after_strip(self, patchexist, patchstrip):
    runner = Runner('test ')
    patchstrip.return_value = 'test'
    patchexist.return_value = False if runner.fname[-1] == ' ' else True
    with self.assertRaisesRegexp(ValueError, 'how is your input neither a file nor a folder??'):
      runner.input_check()

This one, seems I can't figure out how to get it to actually patch .strip, and the answers I have found through Google seem to say that there is no way to patch certain builtin functions (I also tried builtins.strip, didn't work either.) It says empty module name or no module named builtins.

Example 2

import unittest
from mock import patch

class TestRunner(unittest.TestCase):
  @patch('os.path.exists')
  def test_input_check_exists_after_strip(self, patchexist):
    runner = Runner('test')
    patchexist.return_value = patchexist.called
    with self.assertRaisesRegexp(ValueError, 'how is your input neither a file nor a folder??'):
      runner.input_check()

This one returns with the 'input is not a valid path' ValueError. I am guessing that the patch's return value is simply not updated during the running of input_check(), which makes sense even if it's inconvenient for me.

Is there a way to test this? Is this even necessary, or does os.path.exists() deal with there being extraneous whitespace already? I am pretty new to unit testing and even newer to the concept of mocking, so I would appreciate any help.


Solution

  • Use side_effect for os.path.exists; not mock strip()

    I suggest you to use the attribute side_effect (see here for documentation) of Mock object patchexist instead return_value.
    In this way you can return False at first called of os.path.exists() and True at second called (after strip()).

    Furthermore:

    It is not necessary to patch the strip() function.

    Python 3 and Python 2.7

    I have tested the code with Python 3 and not with Python 2.7; I think it is easy for you adapt it for Python 2.7; for example:

    from unittest.mock import patch
    

    in Python 2.7 becomes:

    from mock import patch
    

    For other info about the difference between Python 3 and Python 2.7 see this post.

    Test code

    Below I'll show you the test code which contains 3 tests (which are 3 test cases):

    import unittest
    from runner import Runner
    from unittest.mock import patch
    import os
    
    class TestRunner(unittest.TestCase):
    
        @patch('os.path.exists')
        def test_input_check_exists_after_strip(self, patchexist):
            # the file `test ` doesn't exist; the file `test` doesn't exist
            patchexist.side_effect = [False, False]
            runner = Runner('test ')
            with self.assertRaises(ValueError):
                runner.input_check()
    
        @patch('os.path.isfile')
        @patch('os.path.exists')
        def test_file_exist_after_strip(self, patchexist, patchisfile):
            # the file `test ` doesn't exist; the file `test` exist
            patchexist.side_effect = [False, True]
            patchisfile.return_value = True
            runner = Runner('test ')
            runner.input_check()
            self.assertEqual('file', runner.ftype)
    
        @patch('os.path.isdir')
        @patch('os.path.isfile')
        @patch('os.path.exists')
        def test_directory_exist_after_strip(self, patchexist, patchisfile, patchisdir):
            # the file `test ` doesn't exist; the directory `test` exist
            patchexist.side_effect = [False, True]
            patchisfile.return_value = False
            patchisdir.return_value = True
            runner = Runner('test ')
            runner.input_check()
            self.assertEqual('folder', runner.ftype)
    
    if __name__ == '__main__':
        unittest.main()
    
    

    The execution of all tests calls the real method strip() without mocking it.

    Below the output of the execution of the tests in my system:

    ...
    ----------------------------------------------------------------------
    Ran 3 tests in 0.002s
    
    OK