Reputation: 2222
I have to write some unit tests which depends on other unit tests. For an example, there is a function to move files: move_file()
. To test that function, first I'm required to create a file using the function save_file()
which involves another method in the same class. Calling save_file
function involves calling a cpanel UAPI which involves some authentication. So, calling save_file
function is essential to create a file. Is it acceptable to write a unittest to move_file()
method using save_file()
method to create a file?
Upvotes: 1
Views: 64
Reputation: 1844
It sounds like you are calling the cpanel UAPI from your class, so your tests are integration tests, really. Does that make a difference? Of course.
Generally speaking, in order to test something, you have to set up your system in a meaningful way. How to do this depends on what you are testing.
In unit tests, you're testing the subject in isolation. If it depends on something external, like an API, it's best to create an abstraction or a thin wrapper for it. That's something that you can easily mock or fake in your tests.
But even this abstraction or wrapper has to be tested with the real thing at some point. And that's when you can't fake it any more. You can't move a file that does not exist. Given that, it's perfectly acceptable to call another method to bring your test scenario into the right state.
Upvotes: 1