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 (

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

func main() {

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


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.