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
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
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
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.