14

How would I test against user input from fmt.Scan/Scanf/Scanln?

For example how could I test that the function input will accept "4 5\n" and "1 2 3 4\n" from STDIN and return n == 5 and array == [1, 2, 3, 4].

package main

import (
    "fmt"
)

// input gets an array from the user.
func input() (m int, array []int) {
    fmt.Print("Enter the size of the array, n, and the difference, m: ")
    var n int
    _, err := fmt.Scanf("%d %d", &n, &m)
    if err != nil {
        panic(err)
    }

    fmt.Print("Enter the array as a space seperated string: ")
    array = make([]int, n)
    for i := 0; i < n; i++ {
        _, _ = fmt.Scan(&array[i])
    }

    return m, array
}

func main() {
    m, array := input()
    fmt.Println(m, array)
}

2 Answers 2

21

Here's a very rough draft to illustrate the principle.

program.go

package main

import (
    "fmt"
    "os"
)

// input gets an array from the user.
func input(in *os.File) (m int, array []int) {
    if in == nil {
        in = os.Stdin
    }

    fmt.Print("Enter the size of the array, n, and the difference, m: ")
    var n int
    _, err := fmt.Fscanf(in, "%d %d", &n, &m)
    if err != nil {
        panic(err)
    }

    fmt.Print("Enter the array as a space seperated string: ")
    array = make([]int, n)
    for i := 0; i < n; i++ {
        _, _ = fmt.Fscan(in, &array[i])
    }

    return m, array
}

func main() {
    m, array := input(nil)
    fmt.Println(m, array)
}

program_test.go

package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "testing"
)

func TestInput(t *testing.T) {
    var (
        n, m  int
        array []int
    )

    in, err := ioutil.TempFile("", "")
    if err != nil {
        t.Fatal(err)
    }
    defer in.Close()

    _, err = io.WriteString(in, "4 5\n"+"1 2 3 4\n")
    if err != nil {
        t.Fatal(err)
    }

    _, err = in.Seek(0, os.SEEK_SET)
    if err != nil {
        t.Fatal(err)
    }

    n, array = input(in)
    if n != 5 || fmt.Sprintf("%v", array) != fmt.Sprintf("%v", []int{1, 2, 3, 4}) {
        t.Error("unexpected results:", n, m, array)
    }
}

Output:

$ go test
ok      command-line-arguments  0.010s
0
-10

You can't. At least not so easily, such that, it would be worth the effort.

3
  • 4
    Then how could I change my code so that it would be testable in some form?
    – dmikalova
    Jul 3, 2013 at 21:10
  • You cannot do that. Basically you cannot (at least not with resonable amount of code) write a unit test against fmt.Scanf. You should split your code into: 1) Get the input. 2) Process the input. Write a test for 2. The example above does this split with 1) working on an os.File (which might be os.Stdin) and 2) a input() function operating on this. But note: The answer by Peter is a clever one but does NOT check that fmt.Scanf works as the input is read by fmt.Fscanf. (I know it's just an wrapper). I still hold: You cannot write unit tests against user input.
    – Volker
    Jul 4, 2013 at 7:17
  • I get what you're saying, but peterSO's solution is functionally equivalent as long as you assume that Go itself works as expected. peterSO's solution also solves my problem exactly, and any discrepancy is solely from my inability to properly word my question.
    – dmikalova
    Jul 16, 2013 at 6:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.