0

A test program should post a json object to a rest service OrderRequest and receives an OrderResponse. The test program should tests against the quality assurance stage (qa) and against the production stage (prod).

type Endpoint struct {
    Login      string
    CheckOrder string
}
// https://go.dev/play/p/3vCgU2rA7iR?v=goprev
func main() {

    stages := map[string]Endpoint{
        "prod": Endpoint{Login: "https://acme.test/login/", 
                         CheckOrder: "https://acme.test/checkorder/"},
        "qa":   Endpoint{Login: "https://q.acme.test/login/", 
                         CheckOrder: "https://q.acme.test/checkorder/"},
    }
}

My current code looks like this

// checkorder_test.go
package main

import (
    "fmt"
    "testing"
)

// 
func TestKidBike_K9(t *testing.T) {

    tokenStruct:= Login()
    // got orderResponse as response to json posted from KidBike_K9()
    orderResponse := PostConfiguration(KidBike_K9(), tokenStruct)

    // want
    expectedResponse := &OrderResponse{
        Name: "Kid K9 SWAT bike",
        Id:    "K9",
        Price: "160.00",
    }

    if orderResponse.Price != expectedResponse.Price {
        t.Errorf("Price was incorrect, got: %s, want: %s.", 
                         orderResponse.Price, 
                         expectedResponse.Price )
    }
    if orderResponse.Name != expectedResponse.Name {
        t.Errorf("Name was incorrect, got: %s, want: %s.", 
                         orderResponse.Name, 
                         expectedResponse.Name)
    }
}

Currently the url is hardcoded inside Login() and PostConfiguration().

  • How should the map be used to pass the urls to the different stages? Is there a flag option to switch between qa or prod?
  • Would you create something like a test client and create an instance for each stage: Testclient prodclient = new Testclient('prod')?

Update

After reading Peter Bourgon's view about configuration and his advice to define and parse your flags in func main with this sample

package main

import (
    "flag"
    "net/http"
)
func main() {
    var (
        httpAddr = flag.String("http", ":5050", "HTTP service address")
    )
    flag.Parse()
    serve(*httpAddr)
}
func serve(addr string) {
    http.ListenAndServe(addr, nil)
}

And reading Go by Example: Command-Line Flags i wonder if and how flags can be send to TestMain and wonder if the setup to get the OrderResponse should be put outside the test(s). Also how to use an existing Login-Session

  • to retrieve multiple results orderResponse_1, orderResponse_2, orderResponse_3 and
  • then run different tests on them test_1(orderResponse_1), test_2(orderResponse_2) and so on ...
2
  • This is typically handled as configuration. Your tests would have one config file per environment. Many testing frameworks support some sort of configuration, and even if yours doesn't, there's nothing stopping you from creating your own config system. Go certainly has file access functions, and that's all you need. Commented Feb 3 at 1:12
  • Hi Greg see my update above.
    – surfmuggle
    Commented Feb 3 at 6:28

0

Your Answer

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