> Unit tests by definition test ONLY internal logic and transformations
Output formatting is still a type of transformation! The function explicitly takes the numbers and prints them as decimal integers with newlines between each.
A test to confirm that it IS in that format is still a unit test.
BTW I gave ChatGPT the prompt I would give, and I have to say the answer looks pretty good, even if I'm not a Python programmer and it's not the way I'd do it (which would be to change the function to allow passing in an output stream):
class MyFunctionTestCase(unittest.TestCase):
def test_my_function(self):
expected_output = "0\n1\n2\n"
with patch('sys.stdout', new=StringIO()) as fake_out:
my_function(3)
self.assertEqual(fake_out.getvalue(), expected_output)
With a few more prompts I also managed to get it give me this version:
def my_function(x: int) -> str:
output = ""
for i in range(x):
output += str(i) + "\n"
return output
Which I'd argue somewhat changes the code that was originally written, but it's still a pretty decent answer.
There's no doubt there's some impressive stuff going on that it can do such things, the real issue for me is that when I've tried on far more complex functions it's tended to break down (quite badly in some cases).
Output formatting is still a type of transformation! The function explicitly takes the numbers and prints them as decimal integers with newlines between each. A test to confirm that it IS in that format is still a unit test.
BTW I gave ChatGPT the prompt I would give, and I have to say the answer looks pretty good, even if I'm not a Python programmer and it's not the way I'd do it (which would be to change the function to allow passing in an output stream):
With a few more prompts I also managed to get it give me this version: Which I'd argue somewhat changes the code that was originally written, but it's still a pretty decent answer. There's no doubt there's some impressive stuff going on that it can do such things, the real issue for me is that when I've tried on far more complex functions it's tended to break down (quite badly in some cases).