Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to verify using another public method while unit testing a public method

I am trying to come up with a test case for a Queue class that I have not yet implemented. While trying to write the code for testEnqueue method I could not find ways to verify apart from using size() or dequeue().

Is it fine to to use other public methods(for ex: size/dequeue) to verify the test case for a public method(enqueue) of the same class?

like image 862
ChrisOdney Avatar asked Mar 29 '26 17:03

ChrisOdney


2 Answers

Yes, it is doubtlessly an appropriate means of verification.

like image 72
LaC Avatar answered Apr 02 '26 12:04

LaC


Yes, testing through the external interface of an object is ideal (vs hacking into its internals for verification). As long as each behavior is tested, you should not have any problem. If Enqueue depends on Size() and Size is already proved to be working as expected, you can safely write the test like you mention.

For this example, I probably start by implementing Size() [0 elements, queue created with 1, n elements]. Then move to Enqueue() and verify if Size is incremented by 1.

You're on the right track.

like image 22
Gishu Avatar answered Apr 02 '26 13:04

Gishu