DEV Community

Genne23v
Genne23v

Posted on • Updated on

Now JUnit Test Running in GitHub CI Actions

Fortunately I wrote a CI yml file before. I'm familiar with YAML syntax. It's a simple CI that runs unit tests. In addition, I have a step to set up Java JDK and Gradle. Here's my YAML file.

name: OpenSSG ci

on:
  pull_request:
    branches:
      - master

  push:
    branches:
      - master

jobs:
  unit-tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 18
        uses: actions/setup-java@v3
        with:
          java-version: '18'
          distribution: 'adopt'
      - name: Set up Gradle
        uses: gradle/gradle-build-action@v2
      - name: Run unit tests
        run: gradle test
Enter fullscreen mode Exit fullscreen mode

I also wrote a test for my partner who wrote rost_gen in Rust. Although I never wanted to learn Rust, he posted a message to find someone who can write a test for his project. So I wanted to help and use this opportunity to learn something new. It has C style underscore variable and function naming which looks fast like C. And a lot of short words like fn, mut, pub, etc. Since most functions don't have return value, I tried to capture console message to assert. I thought it would not be very difficult to get console output either to temporary buffer or to a file. But I had to spend many hours to get this single test working. It wasn't the end. Different from my expectation, it captures all output messages for entire tests. I made three different versions based on what I learned from internet, but I couldn't find a way to make the whole test work. I shared my code with repo owner to get his help. I found that the dependency that I used is not working in his machine. I use Mac and he has Windows. And code should be written in both platforms. I spent entire day to figure it out and literally tried everything on the internet, but it didn't work. I think Rust community is not so big. So I couldn't find many articles about capturing console output.

I think it's good to add CI actions in GitHub so that I make sure that things are not broken by new code. Especially when anybody can update my code, it's very important that I tell everyone automatically that tests should pass or code must be linted and formatted. Also I have learned many tools for Java and Rust this week. Learning new tools and eco-system is very important when learning a new language.

Top comments (2)

Collapse
 
cicirello profile image
Vincent A. Cicirello

You are using actions/checkout twice in your workflow. You just need one of them. I'd say keep the first one where you are using version v3 and get rid of the second one.

Collapse
 
genne23v profile image
Genne23v

Thank you for the reply. I just checked it's working fine without v2.