0

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 ?

1
  • Does the function always return the same thing? Or does it actually have some input, for example, an input file or a some data via http? sample sounds as if randomness was involved. Does it create random values in a specific way and return them? Commented Feb 19, 2018 at 7:50

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

A plain python function has no status code. Status codes are a part protocols like of HTTP.

Your test can call the function and check if the result is a dictionary. Without knowing anything more about your function this is the only thing I can suggest.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.