5

I have a method to be tested which depends on the OS, what's the best approach for testing the method depending on the OS? Is it to test on every OS that I have I require? Is there a better approach for running the tests on only one system?

Some more specific information is that I have a method which is dependent on the OS using Python's platform.system method in a if-else statement and I need to test that which all the possible outcomes from this method.

This method looks like:

def updateString():
    if platform.system() == 'Darwin':
        return "http://download.com/to/OSX/version"
    elif platform.system() == 'Linux':
        return "http://download.com/to/Linux/version"
    elif platform.system() == 'Windows':
        return "http://download.com/to/Windows/version"
    else: 
        return "Seek advice on how to update."
5
  • Does the functionality actually change on different systems (ie: when you call foo() on Unix Based you get time since unix epoch, while on Windows you get the time since Bill Gates was born) that would change how the test itself generates answers on different systems? Or can you do some form of mocking of the method so that you can test the functionality on each system?
    – user40980
    Dec 26 '13 at 20:41
  • No the functionality doesn't change, just the result as the method returns a string of where the new download location depending of the OS.
    – Dean
    Dec 26 '13 at 20:44
  • So you're talking about something more like getDefaultSaveLocation() that would return /path/to/loc on unix flavor and C:\path\to\loc on a windows flavor?
    – user40980
    Dec 26 '13 at 20:48
  • @MichaelT See my edit, i've added an example of my method.
    – Dean
    Dec 26 '13 at 20:54
  • If you actually hit the network, it is not a unit test anymore. Jan 21 at 22:40
7

If you target multiple OSs, you most probably have to test your program on each of them. These tests should typically include running your test suite on the desired OS. So the straightforward solution for unit testing the above method is to adapt the expected result on platform.system() exactly the same way you showed above:

def testUpdateString():
    var expectedResult=""
    if platform.system() == 'Darwin':
        expectedResult="http://download.com/to/OSX/version"
    elif platform.system() == 'Linux':
        expectedResult="http://download.com/to/Linux/version"
    elif platform.system() == 'Windows':
        expectedResult="http://download.com/to/Windows/version"
    else: 
        expectedResult="Seek advice on how to update."
    assertEqual(expectedResult,updateString())

Of course, the drawback is that you cannot get a full test coverage of your code when you run the tests on just one OS. In this example, this is probably acceptable, but if the "real" methods you want to test are more complicated, it may become more practicle to unit test the different parts without actually changing the OS. If that's the case, refactor the core part into a separate method which gets platform.system() as a parameter:

def updateStringPerSystem(opSys):
    if opSys == 'Darwin':
        return "http://download.com/to/OSX/version"
    elif opSys == 'Linux':
        return "http://download.com/to/Linux/version"
    elif opSys == 'Windows':
        return "http://download.com/to/Windows/version"
    else: 
        return "Seek advice on how to update."

def updateString():
     return updateStringPerSystem(platform.system())

Now you can write unit tests for updateStringPerSystem, passing all os strings you like for different tests, allowing to test the different branches for OS1, OS2, OS3, etc. even when you are running your test suite only under OS1.

Note that this works only for basic unit tests. As soon as you want to test a function which actually uses the return value for updateString in a OS dependent way, you cannot expect to test Windows functionality when running your suite under Linux, or vice versa.

1
  • Good solution. Note that this is usually called "dependency injection" (because you are injecting the operating system name into the method).
    – sleske
    Dec 26 '13 at 21:50
10

Using the mock framework, you can patch platform.system().

from unittest.mock import patch

…

def testUpdateStringWithDarwin():
    with patch('platform.system', MagicMock(return_value="Darwin")):
        result = updateString()

        assert result == "http://download.com/to/OSX/version", "should be Darwin!"
0
0

For me, directly patching platform.system with pytest-mock did not work. A simple trick though is to abstract the retrieval of this information in a utility function of your own (which you can successfully patch with pytest-mock this time).

Hence, in your implementation module/class:

def _get_system() -> str:
    return platform.system().lower()

def _is_windows() -> bool:
    return _get_system() == 'windows'

And in your test, here using pytest + pytest-mock:

def test_win_no_pad_code():
    with patch('module_name._get_system', MagicMock(return_value='windows')):
        assert module_name._is_windows()

This is one way of doing using module, but you can do the equivalent using a class instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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