python unittest howto

后端 未结 3 1959
栀梦
栀梦 2021-01-14 10:38

I`d like to know how I could unit-test the following module.

def download_distribution(url, tempdir):
    \"\"\" Method which downloads the distribution from         


        
3条回答
  •  有刺的猬
    2021-01-14 11:09

    Unit test propositioners will tell you that unit tests should be self contained, that is, they should not access the network or the filesystem (especially not in writing mode). Network and filesystem tests are beyond the scope of unit tests (though you might subject them to integration tests).

    Speaking generally, for such a case, I'd extract the urllib and file-writing codes to separate functions (which would not be unit-tested), and inject mock-functions during unit testing.

    I.e. (slightly abbreviated for better reading):

    def get_web_content(url):
        # Extracted code
        url_handler = urllib2.urlopen(url)
        content = url_handler.read()
        url_handler.close()
        return content
    
    def write_to_file(content, filename, tmpdir):
        # Extracted code
        file_handler = open(os.path.join(tempdir, filename), "w")
        file_handler.write(content)
        file_handler.close()
    
    def download_distribution(url, tempdir):
        # Original code, after extractions
        distribution_contents = get_web_content(url)
        filename = get_file_name(url)
        write_to_file(distribution_contents, filename, tmpdir)
        return True
    

    And, on the test file:

    import module_I_want_to_test
    
    def mock_web_content(url):
        return """Some fake content, useful for testing"""
    def mock_write_to_file(content, filename, tmpdir):
        # In this case, do nothing, as we don't do filesystem meddling while unit testing
        pass
    
    module_I_want_to_test.get_web_content = mock_web_content
    module_I_want_to_test.write_to_file = mock_write_to_file
    
    class SomeTests(unittest.Testcase):
        # And so on...
    

    And then I second Daniel's suggestion, you should read some more in-depth material on unit testing.

提交回复
热议问题