Introduction
For this weeks lab, I was tasked with implementing Continuous Integration into my release 0.1 project, using github actions. This has allowed my tests to be ran with every push and pull request to github, ensuring the most recently committed version of the project passes the tests. In this blog, I will be going over how I implemented CI on my project and added a test to another persons project in order to test their CI.
How did you set up your GitHub Actions CI Workflow? What did the YAML for this workflow look like?
To setup github actions, I went to the actions tab and choose to make a new workflow for a node environment. It automatically generated an entire yml file that runs the tests. There was only one issue which was I needed to add an environment variable to the yml file and my github environment. Once I did this, my yml file looked like this:
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Node.js CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
env:
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
How did your partner's repo and testing setup differ from yours? What was it like writing tests for a project you didn't create?
My partner for this lab used python for their project, so they needed to configure a yml file that ran python dependencies. And the testing framework for python was quite different from node, so it was challenging trying to understand how the tests worked. Instead of making it too complicated for myself, I just added a test case that was similar to one that already existed, except it just checks for a different case. The original test looked like this:
class TestFileInput(unittest.TestCase):
@patch("sys.stdout", new_callable=StringIO)
@patch("sys.stderr", new_callable=StringIO)
@patch("sys.argv", new=["docbot.py"])
def test_no_files_provided(self, mock_stderr, mock_stdout):
# Run the main function with no files provided
with self.assertRaises(SystemExit): # Expecting the program to exit
main()
# Check that the error message about missing files is printed to stderr
error_message = mock_stderr.getvalue().strip()
self.assertEqual(
error_message, "error: the following arguments are required: file"
)
This test would check if the user tried to run the program (docbot.py) without providing a file for it to feed to the LLM, and if the appropriate error would occur. I took this test and used it to create my own:
class TestInvalidFileInput(unittest.TestCase):
@patch("sys.stdout", new_callable=StringIO)
@patch("sys.stderr", new_callable=StringIO)
@patch("sys.argv", new=["docbot.py", "nonexistentfile.py"])
def test_no_files_provided(self, mock_stderr, mock_stdout):
# Run the main function with an invalid file provided
with self.assertRaises(SystemExit): # Expecting the program to exit
main()
# Check that the error message about missing files is printed to stderr
error_message = mock_stderr.getvalue().strip()
self.assertEqual(
error_message, "Error: Source file nonexistentfile.py not found."
)
This test would check if the user tried to provide a file that doesn't exist, and sees if the appropriate error message shows up. As of writing this blog, my partner hasn't merged my changes since he needed to go to bed, but I'm confident it will pass since it passed on my local machine.
What do you think of CI now that you've set it up for yourself?
Honestly, I like how CI is implemented through github actions. Not only does the UI feel user friendly and fresh, but it is a genuinely useful tool for checking that your projects tests pass automatically. I will make sure to take advantage of github actions in the future whenever I need to add tests to a project.
Top comments (0)