Skip to main content

Featured

Week 12: Release 0.4 Part: 3

Release 0.4 Part 3 This is going to be my final post for this class which covers my final update on my Release 0.4. Earlier this week I made a PR that ports SearchBar to NextJS but I'm still waiting for it to be reviewed some more. I've had some feedback that I have implemented and have also requested a review again. Overall this Release went pretty smoothly for the actually GitHub side of things like setting up the issue, making the PR, and so on. In Release 0.3 I wasn't so certain on how this process happened with Telescope, but now I never had these issues for 0.4. Issue #1470 Fix #1470: Port SearchBar from Gatsby -> NextJS #1503 Did I Meet My Goals? Going into this release I had two main goals: 1. Setup the Issue/PR with no issues 2. Learn about NextJS I feel like I meet both of these goals at the end. I had no issues setting up my branch, updating my master, making the issue, making the PR, and following through on review comments so far. When it comes to learning m

Week 10: Lab 8: Automated Testing and Continuous Integration

 Lab 8

This lab was about introducing automated testing and continuous integration into are URL checker programs. Automated testing helps makes sure new code runs and doesn't break the current version of the program by running a variety of user defined tests. Using GitHub Actions we can add continuous integration to are programs. The purpose of this is to automatically run are tests when a PR is made to the project, before we had to run the tests manually. With both of these concepts put in place we can ensure new code introduced in a PR doesn't break the program.


Completing the Lab

I'm going to warn you now since this lab was so long, this will be a longer blog post then normal. Have fun.

The first steps for the lab had me researching how to add some basics unit tests to my project. Since I'm using GO, I used the "testing" package for this. My first test simply just checked what happens when the readFile function was supplied an empty file.

To test my branch I had to run "go test", this automatically searched for my defined testing functions and ran them giving me the output in the console.

After this I made two new unit tests to test my checkStatus function for when it is given a 200 and 404 error. Todo this I had to make a "mock" to simulate a URL that would respond with a 200 or 404. Todo this I used the "net/http" package. I'm still confused on the whole process of setting these mock responses up, but I understand the purpose for them.

Here's the 200 status code test I made. The WriteHeader() on line 4 was where I set which status code I wanted to receive, in this case it was set to "StatusOK". 

func Test200Response(t *testing.T) {
  ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, `response from the mock server goes here`)
    w.WriteHeader(http.StatusOK)
    w.Write([]byte("200 - OK!"))
  }))
  defer ts.Close()

  mockServerURL := ts.URL

  checkStatus(mockServerURL, 1false1)
}

Once this was done I added in code coverage analysis. The purpose of this is to see how much of the code is covered by unit testing. The higher the percentage the better, but it also depends on how complex the code is. To add this in I used GO's built in testing framework and added the "-cover" flag when using "go test". The output of this command gave me a percentage of how much of my code was covered. I also made a report using "go test -coverprofile=coverage.out". This made a .out file where I could view the coverage.

Once all these steps were done I squashed and merged my branch into master.

The next step was to add automatic testing when making a PR. Todo this I added in continuous integration via GitHub Actions. There was already a GO workflow made so I choose this one for the project. Doing this added a YAML file to the project. I made a new PR to test both passing and failing to see how it all worked.

Here's what the default GO workflow looked like:

nameGo

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

jobs:

  build:
    nameBuild
    runs-onubuntu-latest
    steps:

    - nameSet up Go 1.x
      usesactions/setup-go@v2
      with:
        go-version^1.13

    - nameCheck out code into the Go module directory
      usesactions/checkout@v2

    - nameGet dependencies
      run|
        go get -v -t -d ./...
        if [ -f Gopkg.toml ]; then
            curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
            dep ensure
        fi

    - nameBuild
      rungo build -v ./...

    - nameTest
      rungo test -v ./...

After all this I added more information to the CONTRIBUTING doc about how to test and check the code coverage.

The last part of the lab was to add in a new unit test on someone else's project. For this I paired up with Michael Brackett who's project was made using Python. Setting up the project was pretty straight forward, but I had some issues running his tests. I think this was an issue with miss-matched versions of Python since I was using 3.9 and he was using 3.7, but in the end I got it working and made a PR that passed his CI. 


Final Thoughts

I think this lab was very important for the future of my coding. Every major project has these systems in place and each language may differ but they all follow the same concepts. Overtime I hope to improve with unit testing since its such an important concept and skill to have as a developer.


Thanks,

-Matt

Comments

Popular Posts