0Pricing
DevOps Bootcamp · Lesson

Running Tests with GitHub Actions

Integrate automated unit and integration tests into your CI pipeline to ensure code quality and prevent regressions.

Running Tests with GitHub Actions is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Automate Tests in CI?

Imagine making a change to your code and immediately knowing if you broke something else. That's the power of automated testing in your CI pipeline!

Continuous Integration (CI) is about frequently merging code changes into a central repository. Automated tests are the guardians of this process.

They catch bugs early, give fast feedback, and build confidence in every code change.

Types of Tests in Your Pipeline

When we talk about automated tests, two main types often come up: Unit Tests and Integration Tests.

  • Unit Tests: Focus on small, isolated parts of your code, like a single function or method. They're fast and pinpoint exact issues.
  • Integration Tests: Check if different parts of your system work correctly together, like your code interacting with a database or an API.

Both are vital to ensure your software is robust and reliable.

Automating Tests with GitHub Actions

GitHub Actions can act as your personal test runner, automatically executing your tests whenever you push new code.

You define a workflow that specifies when to run, what environment to use, and which commands to execute.

This ensures every change is validated consistently, without manual effort.

Preparing a Java Project for Testing

To demonstrate, we'll use a simple Java project managed with Maven, a popular build automation tool.

Maven helps manage project dependencies and provides commands to compile code and run tests.

Our project will have a simple Java class and a corresponding JUnit test (conceptually).

Our First Runnable Test Example

Here's a simple Java class and a basic test to check its functionality. We're simulating a unit test without a full JUnit setup for simplicity.

The main method acts as our test runner, verifying if the add method works as expected.

Try running it to see the output!

public class Calculator {
  // Method to be tested
  public int add(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    Calculator calc = new Calculator();
    // Simulate a test case
    int expected = 5;
    int actual = calc.add(2, 3);

    if (actual == expected) {
      System.out.println("Test Passed: 2 + 3 = " + actual);
    } else {
      System.out.println("Test Failed: Expected " + expected + ", Got " + actual);
    }
  }
}

Building Your Test Workflow

Now, let's see how a GitHub Actions workflow file (.github/workflows/test.yml) would look to run our tests.

This YAML file defines the steps GitHub Actions will take to set up the environment and execute our test commands.

We'll use Maven's test command to run any actual JUnit tests in a real project.

name: Java CI with Maven

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: maven
      - name: Run Maven Tests
        run: mvn test

Understanding Each Workflow Step

Let's break down the key steps in our test workflow:

  • on: push / pull_request: Triggers the workflow on code pushes or pull requests to the main branch.
  • runs-on: ubuntu-latest: Specifies the operating system for the virtual machine (runner) where the workflow will execute.
  • actions/checkout@v4: A pre-built action that checks out your repository's code onto the runner.
  • actions/setup-java@v4: Another action to set up the Java Development Kit (JDK) on the runner.
  • run: mvn test: This is the crucial step! It executes the Maven command to compile your code and run all defined tests.

Handling Test Failures in CI

A key benefit of CI testing is immediate feedback. If any test fails, the GitHub Actions workflow job will fail.

This clearly signals that the recent code change has introduced a bug or a regression.

You can then review the workflow logs to pinpoint exactly which test failed and why, helping you fix issues quickly.

The Impact: Quality & Confidence

Integrating automated tests into your CI pipeline is a cornerstone of modern software development.

It actively prevents regressions, ensures consistent code quality, and significantly reduces the risk of deploying broken features.

This process builds confidence in your codebase, allowing your team to innovate faster and more reliably.

Workflow Test Check

Consider a GitHub Actions workflow designed to run tests. What is the primary purpose of the run: mvn test step in a Java project?

Recap: Testing in Your CI Pipeline

In this lesson, we explored how to integrate automated tests into your GitHub Actions CI pipeline.

We covered:

  • The importance of automated testing for code quality.
  • Differentiating between unit and integration tests.
  • How to structure a GitHub Actions workflow to run Java tests using Maven.
  • Understanding workflow steps like checkout and setup-java.
  • The critical role of mvn test in executing your tests.
  • How GitHub Actions reports test failures.

Automated testing is crucial for catching bugs early and maintaining a robust codebase!

Frequently asked questions

Is the “Running Tests with GitHub Actions” lesson free?

Yes — the full text of “Running Tests with GitHub Actions” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Running Tests with GitHub Actions”?

Integrate automated unit and integration tests into your CI pipeline to ensure code quality and prevent regressions. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Running Tests with GitHub Actions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Workflow Triggers and Events
  2. Running Tests with GitHub Actions
  3. Linting and Code Quality Checks
  4. Caching Dependencies for Faster Builds
← Back to DevOps Bootcamp