AI - Write the Unit Test

I just asked ChatGPT to write my Unit Test

That was easy... but maybe it didn't get every thing just right.


I've been working on a Kaktovik Calculator - a base 20 number system iPhone App.  And once again I've created the core functionality without writing unit test.  But now I'd like to do some refactoring of the code and it occurs to me ... wouldn't it be nice to have unit tests for this calculator - before I refactor and start changing the operations from base-10 to base-20.

So I'm kinda a spaceaged monkey... my caveman skills extend to the edge of the web...  I'll just get an AI to do the tedious work for me.... Firing up OpenAI's ChatGPT and ask it to write unit test for my 300 lines of complex if-statements should be easy...  Let's see!

I purposely used the simplest prompt
I could think of...

... and this worked.

And the Tests answered...

I did have a bit of structure to add to my code as my Views are using an EnvironmentObject to hold some global state - so I have to fix that code up for my test to function.  No big deal.  Now running the test - I'd expect them to all pass.

Surprise!

That's not what I expected. 

But I got started writing unit test.  And extending these to GOOD unit test is going to be easy.  And adding more functional test should go fast.  Maybe there is a better way to prompt for GOOD unit test.

I know that the clear button and the radix button are currently big thorns in my side.  So going to unit testing will help me figure out what is right and what needs to be reworked.

Let's get a math problem...

Next, I prompted the AI to write some math unit test.

ChatGPT doesn't mind a math error

Notice that I gave ChatGPT a bit of an error in the math problem... it doesn't mind that 2 * 14.5 is not equal to 33.  It wrote a unit test - but couldn't reason that the answer is not 33!  It has also made up the .digit(String) function that does not exist in the code. But the intent is clear and easy to edit.