Writing tests in bash

by: Ansel Robateau
3/5/2023

I have been writing testing scripts in bash for a while now and decided to do some practice of our company's development process over the weekend.  In order to simulate the code review process, I used ChatGPT at https://chat.openai.com to act as my code reviewer.

Here's the testing code and ChatGPT's response.

The Code

ChatGPT's code review

Prompt

Perform a code review:
<paste_code_here>

The provided script is a test script, which tests the functionality of a command-line tool. It's not an actual application but just a shell script. Therefore, I can't comment on the functionality of the actual tool. However, I can comment on the quality of the test script.

The script appears to be well-structured and follows a given-when-then approach. The test scenarios are defined as comments, and each scenario has multiple steps. The test steps are also well-defined and described in detail.

The function assert_equals is used to compare expected results with actual results. This function ensures that the test passes only if the expected and actual results are the same.

The script sets up temporary files and mock commands to simulate different test cases, which is a good approach for testing.

One issue is that the script relies on environment variables to pass arguments to the command being tested. It would be better to pass the arguments directly to the command to avoid any confusion caused by the environment variables.

Overall, the script appears to be well-organized and well-written, and it's following best practices for testing.

Verdict

I'm pretty happy with this workflow and glad to know that my testing script is well structured.

DISCLAIMER

I used ChatGPT on personal code that is open source that I wrote on my own time.  It is not recommended that you share company secrets with ChatGPT.