A function has been created that returns depending on the input a 0, 1, 2 or 3 exit code. In order to avoid that the code needs to be build and manually tested every time the code changes a test is required.
Aim: to test different exit codes in Golang
Attempts
Attempt one
Neither the code samples of this nor this Q&A answers this question.
Attempt two
func ExitCodeOne() {
fmt.Println("Exit code 1")
os.Exit(1)
}
results in:
Your username is "bla2".Your username is "bla3".Exit code 1
FAIL tool 0.012s
as the exit code 1 causes the whole test to fail.
Problem
At the moment the following problems are experienced:
- How to get the exit code?
- How to prevent that the exit code 1 let the test fail
- How to compare the actual exit code with the expected exit code?
Desired answer
A test that is able to compare the actual exit code with the expected exit code is desired, e.g.:
func TestArgs(t *testing.T) {
ExitCodeOne()
actual:=<current_exit_code_to_string>
expected:=1
if actual != expected {
t.Errorf("Test failed, expected: '%s', got: '%s'", expected, actual)
}
}
Aucun commentaire:
Enregistrer un commentaire