Writing a unit test for a platform dependent unit

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật