Consider the function foo
provided by package X
in Python. I want to test the different functionalities of X.foo
, and then use X.foo
in my code. To make sure that I am using X.foo
as it was tested, I am thinking of encapsulating it inside a function, and then call that function instead. However, I am not sure if this is overkill, and that I should just call X.foo
directly.
For example, to test X.foo
, it is placed inside a function called main
, which in turn is in a file called example.py
:
# example.py
import X
def main(arg1,arg2,arg3):
X.foo(arg1,arg2,arg3,...) # X.foo may have other arguments other than arg1,arg2, and arg3
Next, to unit-test X.foo
, a new file called test_example.py
is created in the same directory as example.py
containing different functions that test the different functionalities of X.foo
:
# test_example.py
from example import main
def test_foo1():
main(...)
# some assertions here
def test_foo2():
main(...)
# some assertions here
def test_foo3():
main(...)
# some assertions here
I would then run each of test_foo1,test_foo2,
and test_foo3
to make sure that X.foo
runs as expected. However, to use X.foo
in my code, I am not sure whether to call X.foo
directly, or to call main
as I did in the tests above. That is, in another file, I could write either
import X
X.foo(...)
or
from example import main
main(...)
I feel that the latter option is "safer" because I am restricting my usage of X.foo
to how it was tested and nothing more.