| I think you don't understand what unit testability means. It means removing IO and side effects from your code. How the hell do I test a print function? I take the print function and match it with what? It has no output so how can I test it printed the correct thing? I can't. I can test a list. I just match it with another list. Making your code unit testable is about segregating IO from logic. Write pure logic where all functions have inputs and outputs and those things can be tested. Your io prints should be small because all functions that do io cannot be fully tested. IO is pollution. Any output to IO is the program exiting the logical mathematical universe of the program and that output can be verified only by an external entity. Either your eyes for stdout or another process or files or a bunch of other ways. Unit tests are about internal local tests that touch local functionality and logic. If you want something unit testable it needs a local output and an input and it shouldn't rely on io in it's data path. I think your complaint here is an example of chatGPT superiority. It understood something you didn't. Well now you know. Removing the print function from the logic and returning the data is 100 percent the correct move. Do you understand? |
Of course you can make the function with a print statement more unit testable without completely changing it's semantics!
You pass in an outputstream and use that as the target for print.
Then your unit test can create its own stream and test the content of the stream whilst production code can pass in standard out.
That way you don't completely change the semantic meaning of the code.
And once again that GPT function is useless. It is identical to list(range()) and it doesn't do what the first function does. Anyone can make anything more unit testable if it doesn't have to do the same thing.