How to Unit Test a Function Than Does Not Return Anything?

How would I unit test a function that does not return anything?

This is an example function:

package main

import (
	"fmt"
	"log"
	"os/exec"
)

func listFilesAndFolders() {
        cmd, err := exec.Command("ls", "-l").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(cmd))
	fmt.Println("Function executed")
}

func main() {
	listFilesAndFolders()
}

Reassign os.Stdout and check the output written to it. In Go, how do I capture stdout of a function into a string? - Stack Overflow

2 Likes

Thank you. That looks good. Another method is to write the output to a file then check it.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.