You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
2 Hrs
56 Min
0 Seg

Creando pipelines

8/30
Resources

How to set up automated testing with GitHub Actions in a Go project?

If you're looking to automate your tests and ensure the quality of your code on an ongoing basis, GitHub Actions is an ideal tool for integrating automated tests into your workflow. Here's how to incorporate GitHub Actions into a Go project, from initial setup to automatically running tests when you push.

How to synchronize dependencies in Go?

When working with Go, it is common to face problems with dependencies, especially if you change branches or remove dependencies without updating the corresponding files. To solve these problems, ensure that dependencies are correctly synchronized by using certain commands:

  1. Update dependency collection:

    go mod tidy

    This command updates the dependencies in go.mod and go.sum.

  2. Update the vendor directory:

    go mod vendor

    Gathers dependencies and places them in the vendor directory.

  3. Resolve inconsistencies:

    go mod verify

    Verify dependency hashes to avoid inconsistencies.

How to create a basic test in Go?

To start running tests with GitHub Actions, first create a dummy test in Go. Follow these steps:

  1. Create a test file:

    Create a file called main_test.go at the same level as your main package main. Your test might look like this:

    package main
    import ( "testing" "github.com/stretchr/testify/require")
    func TestDummy(t *testing.T) { result := 42 // expected value require.Equal(t, 42, result, "The result should be 42")}
  2. Add test dependencies:

    Install the require dependency from the testify package if you haven't already done so:

    go get github.com/stretchr/testify/requirego mod tidy

How to configure GitHub Actions to run tests in Go?

Setting up GitHub Actions requires creating YAML files that define the workflows. Here's how to do it so that it runs every time you do a push:

  1. Create required directories:

    In the root of your repository, create a hidden directory called .github, and inside it, another one called workflows.

  2. Create workflow configuration file:

    Inside workflows, create a validate-test.yaml file with the following content:

    name: Test Workflow
    on: push: branches: - '*'
    jobs: code-validation: runs-on:  ubuntu-22.04
     steps: - name: Checkout code uses: actions/checkout@v4
     - name: Set up Go uses:  actions/setup-go@v2 with: go-version: 1.18
     - name: Install dependencies run: go mod vendor working-directory:  github-tracker
     - name: Run tests run: go test ./... working-directory:  github-tracker

What to do after configuring the workflow?

Once the workflow is configured, upload it to your repository. GitHub Actions will start running the automated tests every time you push to any branch. This not only automates the testing process, but also helps ensure that every code change is automatically validated before merging with the rest of the project.

Final recommendations

  • Consult the official documentation: If you need more customization or are working with other languages, refer to the GitHub Actions documentation.
  • Extend your tests: Make sure to add more tests to cover different cases and improve the quality of the software.
  • Set up other automations: GitHub Actions is not only useful for running tests, but can handle tasks such as deployments, integrations and more. Explore its capabilities!

This guide has given you a basic approach to start automating tests in Go using GitHub Actions. Continuing to learn and experiment is key to mastering the full potential of automations in modern development.

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Que gusto trabajar sobre un archivo yml, lo conozco de Docker nada m谩s.
Se puedo integrar SonarQube con Github pipelines?, tengo entendido que SonarQube tambi茅n hace una an谩lisis de posibles bug de seguridad en el c贸digo, adem谩s de la revisar la cobertura de pruebas