Test Driven Development Pipeline using GitHub Actions

Test Driven Development Pipeline using GitHub Actions

Ever wished for a smoother development workflow in your team? Imagine writing code with confidence, knowing automated tests catch errors early, and deployments happen seamlessly. This dream becomes a reality by combining Test-Driven Development (TDD) with GitHub Actions.

Test driven development

Test-Driven Development (TDD) flips the script on coding. Instead of writing code first, you write failing tests that define what your code should do. It ensures your code is constantly tested and focused on achieving the desired functionality.

Streamlining Collaboration with GitHub Actions

GitHub Actions is a built-in platform within GitHub that allows you to automate various tasks triggered by events like code pushes, pull requests, or scheduled intervals. GitHub Actions can be configured to run your TDD test suite automatically on every push or pull request. This provides instant feedback to developers and reviewers, highlighting potential issues before code is merged. Automated testing acts as a safety net before code gets merged into the main branch.

This article explores how this powerful duo empowers your development team to write reliable code, collaborate efficiently, and deliver high-quality features faster.

Prerequisites:

  • Active GitHub Account:?You'll need a GitHub account to create and manage repositories. If you don't have one, sign up for a free account at?https://github.com/.
  • Existing GitHub Repository: Have a project repository set up on GitHub. This will be the location where you'll enable branch protection and define your workflows

Learning Objectives:

In this guide, we'll cover two essential practices for enhancing your GitHub workflow:

  1. Branch Protection: We'll explore how to enable branch protection rules to safeguard critical branches in your repository. This helps prevent accidental or unauthorized changes that could disrupt development.
  2. Creating GitHub Workflows : We'll delve into defining and running GitHub Actions workflows.

We’ll also be writing some simple tests for a function to test our workflows on GitHub.

Let’s get started!

1. Branch Protection

To enable protection for your target branch follow these steps:

  • Access Your Repository:?Navigate to your project repository on GitHub in your web browser.
  • Open Settings:?Click on the "Settings" tab for your repository. It's usually located on the right side of the main repository page.
  • Locate Branch Settings:?Under the "Settings" menu, find the section related to branches. This might be labeled as "Branches".
  • Enable Protection Rules:?Within the branch settings section, look for an option to "Add branch protection rule". Click on this button to initiate the rule creation process.
  • You will be asked to define a branch name pattern. This pattern can use wildcards or regular expressions to specify the branches you want to protect. For this example, we'll simply enter "main".

  • Once you enter the branch protection rule creation interface, you'll encounter a set of checkboxes for various protection settings. The specific options you select will depend on your project's requirements. Here, we'll focus on some commonly used options:

In essence, these options enforce a code review process. Any changes destined for the main branch must go through a pull request, which requires passing a set of tests before merging. This safeguards the integrity of your main branch.

With this protection in place, let's dive into the coding!

2. Creating GitHub Workflows

1. Creating the workflow directory

To get started, create a new directory named .github/workflows inside the root directory of your project. This directory will house your workflow YAML files. While your project can have multiple YAML files for various workflows, this example will focus on creating a single file named test.yml.

Your working directory should look like this:

2. Writing the workflow

  • Triggering the WorkflowThe workflow kicks off when a pull request is opened or updated on the main branch. This is specified in the on section of the workflow YAML file:

GitHub Actions offer various events to trigger workflows. Explore them in the official documentation here.

  • Job ConfigurationThe workflow consists of a single job named "tests" that runs on the latest version of the Ubuntu operating system. The job's configuration is defined as follows:

  • Matrix Strategy:The workflow utilizes a matrix strategy to run the job with different Node.js versions. In this example, the matrix is defined with a single Node.js version, 20.10.0. You can expand this matrix to test against multiple Node.js versions by adding more entries to the node-version array.

  • Job StepsWith the workflow configuration done, finally describe the steps that your job has to take.

your final test.yml file would look something like this:

Writing Units and Function

Let's write a JavaScript function to check if a string is a palindrome and create some tests to verify its functionality. We'll use the Jest library for testing.

Here's the implementation of the function:

Although one of the tests will fail, we'll focus on showcasing the workflow's functionality for now and address the failing test later.

Seeing Your GitHub Workflow in Action!

Pushing your branch and creating a pull request for the main branch will trigger the workflow. If everything runs smoothly, you should see the workflow complete successfully in the GitHub Actions tab. However, in this case, you might notice that not all tests passed.

Click on details to learn more:

You can see that all the steps you wrote in your job here and their respective statuses.

We can see that one or more of our unit tests failed during the workflow.

You can inspect further to see which tests failed specifically:

Now just fix these tests (or remove it wink wink) and you’re good to go. Your feature branch is ready to merge with main!

Conclusion

This article has provided a foundation for establishing a Test-Driven Development (TDD) pipeline using GitHub Actions. By leveraging GitHub Actions' capabilities, you've automated essential steps in your development workflow. This streamlined approach promotes continuous integration and delivery (CI/CD) practices, fostering faster feedback loops, improved code quality, and a more efficient development process. Remember, this is a starting point. You can customize your TTD pipeline further by incorporating additional tests, security checks, and deployment workflows tailored to your specific project needs.


This article is written by Usman Tahir , Full-Stack Engineer at Antematter.

要查看或添加评论,请登录

Antematter的更多文章

社区洞察

其他会员也浏览了