Python unit test

Unit test is very import to improve the code robust. Each time, when we do some changes or code refactor, we need to verify our code can work well. And those errors can be exposed in develop process not in the test process.

Tools

For pythhon, we usually use two type of tools to run python test nose2 and pytest, and they both based on unittest. In my mind, if you want to test or debug only one unit case, I recommand to use pytest.

Mock

Mock is very useful method to write unit test, and there is a very important and confuse about mock.

    @patch.object(Version, 'validate_version')
    @patch.object(Version, 'get_version')
    @patch('pre_validation.linux.verify_linux_version.get_version_from_install_xml')
    def test_verify_mstr_linux_version(self, m_install_xml, m_get_version, m_validate_version):
        m_install_xml.return_value = ("0.107.5044", "false")
        m_get_version.return_value = "version_short", "version_build"
        m_validate_version.return_value = True
        return_result = verify_linux_version()
        self.assertTrue(return_result)
  1. Here you can see the order in from right to left not from left to right.
  2. For install_xml, you can see it use the absoulte path and this function is call in that file, not the function define in that file.
Built with Hugo
Theme Stack designed by Jimmy