Skip to content
Batuhan Ipci edited this page Nov 30, 2022 · 49 revisions

Lab 9

Due Date

Friday November 18 by Midnight.

Overview

This week we are focused on managing project complexity through the use of Continuous Integration (CI) Pipelines and Test Automation. We will build on the work you did writing tests in Lab 8, and use automated ways to run our tests, and systems to continually guarantee that these tests are run before we merge or ship new versions.

NOTE: I highly recommend watching this week's video lectures before you start this lab.

This lab will help you learn to work with GitHub Actions Workflows, and also learn about test coverage.

Step 2: Add Continuous Integration

What is CI?

Our test framework has been integrated and we've written a number of tests, but so far they only run if you remember to run them. Developers are human and make mistakes. It's easy to forget to run the tests before pushing new code or merging a pull request. What if that new code breaks our tests?

A central idea of testing on a large project is that no developer is ever allowed to break the main branch. If someone's changes would break the tests, we can't merge them; if we do, we'll introduce a bug that will derail the work of other developers on the project until it gets fixed, and also affect users.

To help us keep the default branch working at all times, we need to add Continuous Integration (aka CI) to our project. CI is a method whereby we build and run our tests (i.e., integrate everything in our project) automatically whenever anything is pushed to the repo, or a new pull request is made. With CI we do this continually and ideally on every change. This lets us monitor the effect of a given change and assess its quality.

For CI to work, we need dedicated machines to be available to checkout our code, build it, and run our tests. We could set up our own machines and have them listen for various events on GitHub in order to trigger a build; or we can use any number of existing CI cloud providers who will do this for us. Many of these are free for open source developers.

GitHub Actions

GitHub provides its own CI service called GitHub Actions. Actions allow us to automate workflows in response to events in a GitHub repo (e.g., merging to main, creating a pull request, etc).

We can add a GitHub Action via the GitHub UI for our repo. Doing so will create a new file YAML file with information about when to trigger the action, and which steps to perform.

You can read more about how to create a CI workflow for your language using these GitHub guides:

There are also starter workflow files for many other languages, including dotnet core, C/C++, Rust, Go, etc.

Create a GitHub Actions Workflow

In your repo, create a GitHub Actions Workflow that runs your tests. It should be triggered on any push to your default branch (e.g., main), and for any pull_request to your default branch. You can add, edit, and commit the file directly in GitHub. When you do, it should trigger a build that you can inspect in the Actions tab of your repo.

NOTE: if you commit the .yml workflow file via the GitHub web page, make sure you git pull origin master to your local repo so that you also get this change on your local machine's repo!

Step 3. Create a Pull Request to Test your CI

To make sure that our CI workflow is correct, we'll try creating a pull request against our own repo.

Pick a third area of your code that needs some tests, for example another function. Create a new branch for this work (e.g., add-more-tests) and write your new tests. Use your coverage tool to help you know when you've done enough work.

When all of your tests are passing, commit your new test cases to the add-more-tests branch, and push this branch to your origin (don't merge it to master):

$ git push origin add-more-tests

When this completes, git will let you create a new Pull Request (or you can do it manually via the GitHub UI). Create a pull request from your add-more-tests branch to your main branch.

NOTE: this might seem odd to you--why would I ever make a pull request to my own repo? As strange as this sounds, it's often done in projects where a community of developers is working, and one of the people owns the repo. This allows the owner to also have their code reviewed and go through CI.

In GitHub, make sure your pull request has triggered your CI workflow, and you have a report of whether it is running, passed, or failed. If you don't see this in the pull request, something is wrong in Step 6, and you should go back to investigate.

If you do see your CI workflow being run, congratulations! Try making another commit that breaks the tests (e.g., make a new commit on your branch that breaks the logic in your function, so the tests will fail). Push this broken commit to GitHub and watch your CI build to make sure it fails as you expect. Go and inspect the workflow's logs to see the error message that your tests produced. Does it make sense to you?

Once you can confirm that a failed test will break your CI workflow, and know how to inspect the logs, make another change to fix the failing test and push it. Confirm that this change fixes the CI and everything is green.

Once you have successfully passed, failed, and passed CI again, go ahead and merge your pull request to master.

Step 4. Add Tests to Another Project

Now that you've done all the hard work of setting up automated tests, code coverage, and CI, you might as well include more more tests!

For this step, find a partner in the class and send each other a pull request that adds one more test case to their project. If you're feeling brave, pick a partner that uses a different language/framework than you. It's great to get experience writing tests in different environments.

Work with your partner to figure out which function or file you should add tests for, and read their CONTRIBUTING.md document to learn how to use their testing tools. If anything is unclear to you, file issues to have the owner improve their repo's documentation.

Do all of your work in a separate branch and send a pull request when you are finished and the tests run locally. Make sure that GitHub's CI runs the pull request's new tests and passes before this gets merged. If anything fails or needs fixing, do that in the pull request by adding more commits.

Step 5. (Optional) Integrate your Linter to the CI Workflow

In the same way that we added automated testing to our CI workflow, you can also add a command line script to run your linter and other static analysis tools. You might do this by overloading your test script to call both the linter and your test runner one after the other; or you might add a dedicated script just for the linter.

Many projects run linters before the automated tests, and a successful CI build will mean passing all lint and test conditions. This is done to help developers remember to run the linting steps, and avoid merging silly typos and bugs that could be easily caught at development time. It also reduces the amount that a reviewer has to do, since they know that linting and test cases are being checked by the CI machines.

Step 6: Write a Blog Post

When you have completed all of the required steps above, please write a detailed blog post. In your post, discuss the following:

  • How did you set up your GitHub Actions CI Workflow? What did the YAML for this workflow look like?
  • How did your partner's repo and testing setup differ from yours? What was it like writing tests for a project you didn't create?
  • What do you think of CI now that you've set it up for yourself?

When you have completed all the requirements above, please add your details to the table below.

Name Blog Post (URL) Your Repo's Successful GitHub Actions Run (URL) Testing PR to Another Repo
Denes Adam Dolhay https://dev.to/dadolhay/osd600-lab9-5279 job 24
Maxim Nosov https://dev.to/mnosov622/continuous-integration-pipeline-3dcd 3402442050 18
Mario Leonardo https://dev.to/ririio/adding-my-first-continuous-integration-ci-2d61 5764081485 7799387258
Anshul Gandhi https://dev.to/anshul137/dps909-blog-lab-9-add-continuous-integration-4597 5768048381 19
Gulnur Baimukhambetova https://dev.to/gulyapulya/continuous-integration-for-nodejs-project-269a 3457404706 10
Piotr Drozd https://dev.to/pdr0zd/using-github-actions-to-automate-my-tests-4p4c 3465296238 8c581ff
Tong Liu https://dev.to/liutng/reflect-of-lab09-2p4f 3464401840 3466622227
Neil An https://dev.to/neilan99/adding-ci-to-my-ssg-4m7p 3457131724 3484458424
Artem Tanyhin https://dev.to/devils2ndself/continuous-integration-and-go-1fd1 3485605180 19
Chan Dinh (Oscar) Phu https://dev.to/lostbutton/adding-continuous-integration-46oh 3447202846 18
Chen-Yuan Chu https://dev.to/cychu42/auto-run-tests-test-via-continuous-integration-ib6 5809524858 26
Alexander Samaniego https://dev.to/alexsam29/dps909-blog-lab-9-continuous-integration-2hk5 3485009215 PR 21
Rudy Chung https://dev.to/rudychung/osd600-lab-9-5adm 5763816544 #15
Stefan Frunza https://dev.to/sfrunza13/ci-continuous-integration-4f01 5848630276 #18
Wonkeun No https://dev.to/genne23v/now-junit-test-running-in-github-ci-actions-1a05 235566e #52
Eakampreet Singh https://dev.to/eakam/osd-lab-9-setting-up-a-ci-workflow-8jb 5852052132 18
Ivan Gabrovsky https://blogforwebdevelopment.blogspot.com/2022/11/lab-9-blog.html 3502034 19
Batuhan Ipci https://dev.to/batunpc/makefiles-can-be-helpful-in-your-ci-workflow-36cl 3518481164 15
Samina Raman Purba 3520409878 17
Tymur Levtsun https://dev.to/myrfion/making-sure-izyum-is-always-fresh-3p06 3544817265 28
Taimoor Dawami https://dev.to/tdaw/osd600-adding-ci-to-siteit-c71 5972437885 PR #21
Clone this wiki locally