我想测试golang命令行应用的输出,但我不太确定如何使用go的测试库。
我们说我有一个这样的程序:
package main
import (
"flag"
"fmt"
)
func main() {
const (
cityDefault = "San Francisco"
cityDoc = "the city you want the forecast for"
)
var city string
flag.StringVar(&city, "city", cityDefault, cityDoc)
flag.StringVar(&city, "c", cityDefault, cityDoc)
flag.Parse()
fmt.Println(city)
}
我想测试这两个:
$ ./myapp -c "Los Angeles"
$ ./myapp -city "Los Angeles"
...输出Lost Angeles
。所以,我想问题是,你如何进行集成测试golang命令行应用程序的输出?
答案 0 :(得分:4)
这是解析命令行参数的一个不好的例子,但它显示了我用来在我的应用程序中测试命令行参数的框架。
<强> main.go 强>
float uvv, u, v, diffU, diffV, F, K, lapU, lapV;
float[] MINI = { 0.00, 0.01, 0.03, 0.005 };
float[] MAXI = { 0.15, 0.08, 0.11, 0.05 };
PImage reactionDiffusion (PImage img) {
int W = img.width, H = img.height;
int[][] offsetW = new int[W][2], offsetH = new int[H][2];
float[][] U = new float[W][H], V = new float[W][H];
float time = millis();
// INITIALISATION
for (int i = 0; i < W; ++i) {
for (int j = 0; j < H; ++j) {
U[i][j] = 0.15 ;
V[i][j] = 0.7 ;
}
}
// init parameters
float[][][] fkuv = new float[W][H][4];
for (int i = 0; i<W; ++i){
for (int j = 0; j<H; ++j){
fkuv[i][j][0] = map( i, 0, H, MINI[0], MAXI[0] );
fkuv[i][j][1] = map( j, 0, W, MAXI[1], MINI[0] );
fkuv[i][j][2] = map(150,0,200,0,MAXI[2]);
fkuv[i][j][3] = map(50,0,200,0,MAXI[3]);
}
}
//Set up offsets
for (int i=0; i < W; ++i) { offsetW[i][0] = i-1; offsetW[i][1] = i+1; }
for (int i=0; i < H; ++i) { offsetH[i][0] = i-1; offsetH[i][1] = i+1; }
offsetW[0][0] = 0; offsetW[W-1][1] = W-1;
offsetH[0][0] = 0; offsetH[H-1][1] = H-1;
for (int n = 0; n< 2000 ; ++n){ // itérations
for (int i = 0; i < W; ++i) {
for (int j = 0; j < H; ++j) {
F = fkuv[i][j][0] ;
K = fkuv[i][j][1] ;
u = U[i][j];
v = V[i][j];
uvv = u*v*v;
lapU = U[offsetW[i][0]][j] +U[offsetW[i][1]][j] +U[i][offsetH[j][0]] +U[i][offsetH[j][1]] -4*u;
lapV = V[offsetW[i][0]][j] +V[offsetW[i][1]][j] +V[i][offsetH[j][0]] +V[i][offsetH[j][1]] -4*v;
U[i][j] += ( U*lapU - uvv + F*(1 - u) ) * 1.38 ;
V[i][j] += ( V*lapV + uvv - (K+F)*v ) * 0.63 ;
}
}
}
img.loadPixels();
int pShift;
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
pShift = int( U[i][j]*255 ) ;
img.pixels[j*W+i] = 0xff000000 | (pShift << 16) | (pShift << 8) | pShift ;
}
}
img.updatePixels();
println("time : "+ ( millis()-time ) /1000) ;
return img;
}
<强> main_unit_test.go 强>
package main
import (
"log"
"os"
)
func main() {
var city string
parseFlags(&city, os.Args)
log.Println(city)
}
func parseFlags(result *string, args []string) {
cityDefault := "San Francisco"
switch len(args) {
case 3:
*result = args[2]
default:
*result = cityDefault
}
}
我通常使用this package来解析所有应用中的命令行参数。我将按如下方式构建我的代码(测试未显示,但它们通常遵循上面显示的测试要点):
<强> main.go 强>
package main
import (
"log"
"testing"
)
// TestParseFlags - test our command line flags
func TestParseFlags(t *testing.T) {
var parseFlagsTests = []struct {
flags []string // input flags to the command line
expected string // expected
}{
{[]string{"/fake/loc/main"}, "San Francisco"},
{[]string{"/fake/loc/main", "-c", "Los Angeles"}, "Los Angeles"},
{[]string{"/fake/loc/main", "--city", "Los Angeles"}, "Los Angeles"},
}
for _, tt := range parseFlagsTests {
var output string
parseFlags(&output, tt.flags)
if output != tt.expected {
t.Errorf("expected: %s, actual: %s", tt.expected, output)
}
}
}
<强> /myDir/cli.go 强>
package main
import (
"log"
"os"
"myDir/cli"
)
func main() {
// Grab the user inputed CLI flags
cliFlags := cli.FlagsStruct{}
cliErr := cli.StartCLI(&cliFlags, os.Args)
if cliErr != nil {
log.Println("Error grabbing command line args")
log.Fatal(cliErr)
}
// Do stuff ...
}
答案 1 :(得分:2)
test "$(./myapp -c "Los Angeles")" = "Los Angeles"
怎么样
同样的 - 城市。这与Go无关,只需让您的集成测试套件进行测试即可。