Say I have a python function called sample(). It takes no arguments. It returns a dictionary in the end though. How can I perform unit testing for such a function ? Can I test it with status code like 200 ? How can i test if the function is written correctly ?
2 Answers
if your function returns a dict you may test it by equality:
assert function_to_test() == {'key': 'value'}
This is the simplest case and if your function only returns a dict without parameter, there are great chances your dict has variations over each calls. So you may test it with some expected content. Let say you have this dict as function return value:
{
    'key': [1,2,3], 
    'other_key': None
}
Then you may test some dict constants as following
#  the key 'key' should exist
assert 'key' in function_to_test()
#  the 'key' value of the dict is an array with every time at least 1 value
assert len(function_to_test().get('key')) > 1
#  the 'other_key' field is always set to None
assert function_to_test().get('other_key') is None
    
samplesounds as if randomness was involved. Does it create random values in a specific way and return them?