Sitemap

How to Unit Test with GitHub Actions

4 min readFeb 26, 2023

Hello. In this article, I will talk about how to do “Unit Testing” in C using GitHub Actions, which allows us to test each function separately. Let’s get started without wasting any time.

I have explained what GitHub Actions is, what it is used for, and a brief example in my previous article. You can click here to access it.

We will test a simple C application. We will then give points to these functions and create a .txt file in the same directory. If the function is correct, it will give us the score we have determined.

Alright, let’s write a simple application. Our code will contain four functions in C.

#include <stdio.h>

// Toplama Fonksiyonu
int topla(int sayi1, int sayi2) {
return sayi1 + sayi2;
}

// Çıkarma Fonksiyonu
int cikar(int sayi1, int sayi2) {
return sayi1 - sayi2;
}

// Çarpma Fonksiyonu
int carp(int sayi1, int sayi2) {
return sayi1 * sayi2;
}

// Bölme Fonksiyonu
float bol(float sayi1, float sayi2) {
if (sayi2 == 0) {
printf("Hata: sifira bolme hatasi.\n");
return 0;
} else {
return sayi1 / sayi2;
}
}

int main() {
int sayi1 = 10;
int sayi2 = 2;

// Toplama işlemi
int sonuc1 = topla(sayi1, sayi2);
printf("%d + %d = %d\n", sayi1, sayi2, sonuc1);

// Çıkarma işlemi
int sonuc2 = cikar(sayi1, sayi2);
printf("%d - %d = %d\n", sayi1, sayi2, sonuc2);

// Çarpma işlemi
int sonuc3 = carp(sayi1, sayi2);
printf("%d * %d = %d\n", sayi1, sayi2, sonuc3);

// Bölme işlemi
float sonuc4 = bol(sayi1, sayi2);
printf("%d / %d = %.2f\n", sayi1, sayi2, sonuc4);

return 0;
}

Let’s create a file named test.yml under the C-Unit-Test/.github/workflows/ root directory on this page. You can find the code for the test.yml section below.

name: C Kodu Testi

on:
push:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest
env:
total_score: 100

steps:
- name: Klonlama
uses: actions/checkout@v2

- name: Gereksinimleri yükle
run: sudo apt-get update && sudo apt-get install -y build-essential

- name: Toplama Fonksiyonu Testi
run: |
gcc -o fonksiyon fonksiyon.c
if [ $(./fonksiyon | grep "10 + 2 = 12" | wc -l) -eq 1 ]; then echo "Toplama fonksiyonu testi geçti."; else echo "Toplama fonksiyonu testi başarısız oldu."; fi
echo "${GITHUB_ACTOR} Toplama Fonksiyonu: 25" >> not.txt

- name: Çıkarma Fonksiyonu Testi
run: |
gcc -o fonksiyon fonksiyon.c
if [ $(./fonksiyon | grep "10 - 2 = 8" | wc -l) -eq 1 ]; then echo "Çıkarma fonksiyonu testi geçti."; else echo "Çıkarma fonksiyonu testi başarısız oldu."; fi
echo "${GITHUB_ACTOR} Çıkarma Fonksiyonu: 25" >> not.txt

- name: Çarpma Fonksiyonu Testi
run: |
gcc -o fonksiyon fonksiyon.c
if [ $(./fonksiyon | grep "10 * 2 = 20" | wc -l) -eq 1 ]; then echo "Çarpma fonksiyonu testi geçti."; else echo "Çarpma fonksiyonu testi başarısız oldu."; fi
echo "${GITHUB_ACTOR} Çarpma Fonksiyonu: 25" >> not.txt

- name: Bölme Fonksiyonu Testi
run: |
gcc -o fonksiyon fonksiyon.c
if [ $(./fonksiyon | grep "10 / 2 = 5.00" | wc -l) -eq 1 ]; then echo "Bölme fonksiyonu testi geçti."; else echo "Bölme fonksiyonu testi başarısız oldu."; fi
echo "${GITHUB_ACTOR} Bölme Fonksiyonu: 25" >> not.txt

- name: Not Dosyası Oluştur
run: |
total_score=0
while read -r line; do
score=$(echo "$line" | awk '{print $NF}')
total_score=$((total_score + score))
done < not.txt
export total_score
echo "${GITHUB_ACTOR} puanınız: $total_score" >> not.txt

- name: Sonucu yükle
uses: actions/upload-artifact@v2
with:
name: test-sonucu
path: not.txt

Please note that you need to edit the test.yml code according to your code. This is just an example to give you an idea. Also, there is a system in this example that gives points to functions in a file named not.txt in the same directory. I gave 25 points to each function, and if the function works, it will give 25 points. I set a total limit of 100 points. Additionally, when not.txt is created, it will write the name of the person who pushed the repository to the not.txt file.

After running the actions, you should see a similar image below.
After running the GitHub actions, you can download your .txt file from this section.
not.txt

Our .txt file is as follows:

Total Score: 0/100

Function 1: 0/25
Function 2: 0/25
Function 3: 0/25
Function 4: 0/25

Pushed by: Your GitHub username

I wanted to write and share this article as part of my learning process. My aim is not to write a professional article, but to convey my thoughts and knowledge. Thank you for taking the time to read it.

--

--

Barış Güleç
Barış Güleç

Written by Barış Güleç

AI Engineer Intern in Barcelona | Computer Engineering Student 3/4 | Researcher | CookieAI | Google Core Team Member

No responses yet