We wanted to experiment with ChatGPT. We wanted to see what would happen if we supplied some example Python code and then ask it how to verify that the code worked. The results were quite satisfying.
We created a GitHub repo if you want to try the code out yourself.
The initial prompt
Let's copy and paste the code from nhl_api.py directly below our simple prompt of "Given the following Python code, what are some tests that could be written to verify it works as expected:"
ChatGPT was off to a great start - offering a response that identified several functions in the Python code and offered sample input with expected output values for tests that could be written.
OK, ChatGPT. How would I write those tests?
The next step in our experiment was to see how ChatGPT might guide us in writing some initial tests to verify the code.
In this example, ChatGPT opted to show us how to write simple assert statements in Python that could verify functionality:
What would those tests look like written in...%#@%#%@
In my adventures with Python, I've worked on several projects that used pytest for unit testing.
What would these ChatGPT suggested tests look like written in pytest?
Well, turns out autocomplete decided I didn't want to ask about pytest - and instead submitted a nonsensical prompt to ChatGPT - "What would these tests look like written in purest?"
Uhhhhh...what? I pictured ChatGPT momentarily stunned and shaking its head in disappointment.
Much to my surprise, ChatGPT politely told me it was unsure of what I meant by "purest" - and correctly suggested that if I wanted to write basic unit tests using pure Python, the following code might be useful.
Does it work?
If you're familiar with Python, you should be able to create two files based on the examples above OR refer to our example repo.
Assuming you are running Python from the directory containing these files, you should be able to run the original script on its own as well as the test script:
Well, look at that. Our five tests ran and passed as expected.
The point goes to ChatGPT. 🤓
How can we help?
Can we help you apply these ideas on your project? Send us a message! You'll get
to talk with our awesome delivery team on your very first call.