2

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.

4
  • 1
    Please keep in mind that Python 2.7 has been unmaintained, even for essential security fixes, since Jan 1, 2020. It is approximately as old as Windows 7. If Python 2.7 came with your computer then do not attempt to remove it, as this can seriously damage your operating system; but please try to write code for an up-to-date Python version, make sure you have one installed, and make sure you can enforce that the code runs with that version instead. (Nothing I see here appears to be 2.x-dependent.) Commented Jun 17 at 16:50
  • Don't mock str.strip. Mock os.path.exists() so it return False whenever the name begins or ends with a space.
    – Barmar
    Commented Jun 17 at 16:56
  • @KarlKnechtel The 'runner' is for code that uses the python package that only exists for 2.7 that I mentioned at the start. This function cannot be separate from that code, so I have to use 2.7. I've double checked, there is no Python 3 package for the program I'm scripting for. I don't have a choice.
    – Réka
    Commented Jun 17 at 16:59
  • @Barmar Same result as example 2, it doesn't appear to update the return value during the input_check call.
    – Réka
    Commented Jun 17 at 17:01

1 Answer 1

2

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):

  • test_input_check_exists_after_strip(): the file test and the file test don't exist (your code raises a ValueError Exception)
  • test_file_exist_after_strip(): the file test doesn't exist, but the file test (after strip()) exists
  • test_directory_exist_after_strip(): the file test doesn't exist, but the directory test (after strip()) exists
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
2
  • I ended up fixing it by simply moving .strip() before the first .exists() check - but thank you! I didn't fully understand how .side_effect works, and this makes so much sense. It will be very helpful!
    – Réka
    Commented Jun 24 at 17:33
  • With side_effect() you can select what the function/method returns after the first, second, third, ... execution. All these values must be inserted inside a list in the desired order. In your case you have to set the different return value of the execution of patchexist.
    – User051209
    Commented Jun 25 at 6:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.