How to Configure GitHub to Run Unit Tests Automatically

Introduction

Setting up automated unit tests in GitHub can streamline your development process, ensuring code quality and reliability. This guide demonstrates how to create a simple Java project with unit tests and configure GitHub Actions t…


This content originally appeared on DEV Community and was authored by Olga Strijewski

Introduction

Setting up automated unit tests in GitHub can streamline your development process, ensuring code quality and reliability. This guide demonstrates how to create a simple Java project with unit tests and configure GitHub Actions to run these tests automatically.

You can check out the GitHub repository used for this project here: github-workflow-demo.

Creating a Project with Unit Tests

We will now create a simple Java project with a unit test. This will serve as the foundation for setting up automated unit testing in GitHub.

Generate Code Using Spring Initializr:

  • Open IntelliJ IDEA.
  • Go to File > New > Project.
  • Select Spring Boot and configure your project (Group, Artifact, etc.).
  • Choose "Spring Web" as a dependency.
  • Click Create, and IntelliJ IDEA will generate the project code for you.

This project can be found at github-workflow-demo GitHub repository.

Now we will "put some meat" into the project. Let's add a controller:

Image description

...and a unit test:

Image description

We will launch the application and see how it works. Right-click GithubWorkflowDemoApplication class and choose "Run 'GithubWorkflow....main()':

Image description

The application will start very quickly:

Image description

In a browser, navigate to http://localhost:8080/, and you will see the message "Hello, World!" as we programmed in our application:

Image description

Now let's launch the tests. Right-click GithubWorkflowDemoApplicationTests and choose "Run GithubWorkflowDemoApplicationTests:"

Image description

The tests are successful:

Image description

Great! You now have a simple application with a unit test. Don't forget to commit and push your new code to the repository.

Creating a Unit Test Workflow in GitHub

We will start with GitHub out-of-the-box template workflow for unit tests. Go to the "Actions" tab:

Image description

...and then click on the "New workflow" button:

Image description

Search for "test":

Image description

The very first option is what we are looking for - we have a Maven project. Click "Configure":

Image description

GitHub will generate a sample workflow for you:

Image description

After you check it in, it will run it. However, unfortunately, the default workflow fails:

Image description

You will need to add to it to make it succeed.

What's Wrong with the Out-of-the-Box Workflow?

Amazingly, the out-of-the-box workflow doesn't do any testing! I had to modify it as follows to make it run the tests:

Image description

I also added the surefire Maven plugin to pom.xml:

Image description

Now it actually runs the tests, however we still have errors.

Dependency Graph Errors

The errors are security related, and are about the dependency graph:

Image description

GitHub template has these lines at the end, that are indicated as "optional", and they are giving errors:

Image description

To fix the dependency graph permissions, let's create a personal access token with all the needed permissions. First, go to your profile > Settings:

Image description

Then navigate to "Developer Settings" at the bottom:

Image description

Create a personal access token. Make sure you check all the needed permissions:

Image description

Copy your token:

Image description

Then navigate to your repository's Settings > Secrets and variables > Actions, and create a secret with the name GH_TOKEN and the value - the token that you just created:

Image description

Add your token to the workflow configuration:

Image description

Now the workflow succeeds!

Image description

Testing the Workflow

Our workflow seems to be working well. Let's see what happens if our tests fail.

I made the test fail as follows:

Image description

Now when I launch them from the IDE, one of them fails:

Image description

After committing the changes, I found that the workflow also failed:

Image description

You can see the details, e.g., which tests failed and why, in the log:

Image description

I also see the failure in my email that is associated with my GitHub account:

Image description

Once I fixed the tests, the workflow was successful again:

Image description

Conclusion

By following this guide, you have successfully set up a simple Java project with unit tests and configured GitHub Actions to automate the testing process. This ensures continuous integration and helps maintain code quality as your project evolves.


This content originally appeared on DEV Community and was authored by Olga Strijewski


Print Share Comment Cite Upload Translate Updates
APA

Olga Strijewski | Sciencx (2024-07-06T05:43:59+00:00) How to Configure GitHub to Run Unit Tests Automatically. Retrieved from https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/

MLA
" » How to Configure GitHub to Run Unit Tests Automatically." Olga Strijewski | Sciencx - Saturday July 6, 2024, https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/
HARVARD
Olga Strijewski | Sciencx Saturday July 6, 2024 » How to Configure GitHub to Run Unit Tests Automatically., viewed ,<https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/>
VANCOUVER
Olga Strijewski | Sciencx - » How to Configure GitHub to Run Unit Tests Automatically. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/
CHICAGO
" » How to Configure GitHub to Run Unit Tests Automatically." Olga Strijewski | Sciencx - Accessed . https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/
IEEE
" » How to Configure GitHub to Run Unit Tests Automatically." Olga Strijewski | Sciencx [Online]. Available: https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/. [Accessed: ]
rf:citation
» How to Configure GitHub to Run Unit Tests Automatically | Olga Strijewski | Sciencx | https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.