From what I’ve read, unit test should test only one function/method at a time. But I’m not clear on how to test methods that only set internal object data with no return value to test off of, like the setvalue() method in the following Python class (and this is a simple representation of something more complicated):
class Alpha(object):
def __init__(self):
self.__internal_dict = {}
def setvalue(self, key, val):
self.__internal_dict[key] = val
def getvalue(self, key):
return self.__internal_dict[key]
If unit test law dictates that we should test every function, one at a time, then how do I test the setvalue() method on its own? One "solution" would be to compare what I passed into setvalue() with the return of getvalue(), but if my assert fails, I don't know which method is failing - is it setvalue() or getvalue()? Another idea would be to compare what I passed into setvalue() with the object's private data, __internal_dict[key] - a HUGE disgusting hack!
As of now, this is my solution for this type of problem, but if the assert raises, that would only indicate that 1 of my 2 main methods is not properly working.
import pytest
def test_alpha01():
alpha = Alpha()
alpha.setvalue('abc', 33)
expected_val = 33
result_val = alpha.getvalue('abc')
assert result_val == expected_val
Help appreciated
Aucun commentaire:
Enregistrer un commentaire