DEV Community

Isabella Liu
Isabella Liu

Posted on

Unit Test & Github Action

The task for lab 8 is to add unit tests and CI to my urlCHECKER program, which was created in the former labs. I spent some time learning about unit test in Golang, and the grammar is not that hard in Go, which can be told from the example below:

func TestExtractURL(t *testing.T) {
    result := extractURL("https://www.google.ca, https://www.facebook.com")

    expected := []string{"https://www.google.ca", "https://www.facebook.com"}
    if !reflect.DeepEqual(result, expected) {
        t.Errorf("Expected: %v, but got: %v", expected, result)
    }
}
Enter fullscreen mode Exit fullscreen mode

The terminal will show the below information after the test is run successfully.
Alt Text

To run the code coverage tool I used for my project, the below commands need to be run:

go test -coverprofile cover.out 
go tool cover -html=cover.out
Enter fullscreen mode Exit fullscreen mode

Github action

name: Go

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.13

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Build
      run: go build -v ./...

    - name: Test
      run: go test -v ./...

Enter fullscreen mode Exit fullscreen mode

The CI workflow result can be found through the following link:

https://github.com/isabellaliu77/urlChecker/actions

Linter

For the linter, the package needs to be got using the below command, which I have learned the hard way.

    - name: Lint
      run: |
           go get -u golang.org/x/lint/golint
           golint ./...
Enter fullscreen mode Exit fullscreen mode

Top comments (0)