A set of reusable step definitions for testing REST APIs with Godog.
- godog > 0.10.0
To recommended way is to integrate with Godog and Go test as specified in the Godog documentation
package main
import (
"flag"
"os"
"testing"
apicontext "github.com/brpaz/godog-api-context"
"github.com/cucumber/godog"
)
var opts = godog.Options{
Format: "progress", // can define default values,
}
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opts)
}
func TestMain(m *testing.M) {
flag.Parse()
opts.Paths = flag.Args()
apiContext := apicontext.New("<base_url>")
status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: apiContext.InitializeScenario,
Options: &opts,
}.Run()
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
You can see a complete example together with Feature files in examples folder.
^I set query param "([^"]*)" with value "([^"]*)"$
^I set query params to:$
^I set header "([^"]*)" with value "([^"]*)"$
^I set headers to:$
^I send "([^"]*)" request to "([^"]*)" with form body::$
^I send "([^"]*)" request to "([^"]*)"$
^I send "([^"]*)" request to "([^"]*)" with body:$
^The response code should be (\d+)$
^The response should be a valid json$
^The response should match json:$
The response header "([^"]*)" should have value ([^"]*)$
^The response should match json schema "([^"]*)"$
^The json path "([^"]*)" should have value "([^"]*)"$
^wait for (\d+) seconds$
^Store data in scope variable "([^"]*)" with value ([^"]*)
^I store the value of response header "([^"]*)" as ([^"]*) in scenario scope$
^I store the value of body path "([^"]*)" as "([^"]*)" in scenario scope$
^The scenario variable "([^"]*)" should have value "([^"]*)"$
This can also store the values from http response body and header and then use in subsequent requests.
To use the value of scope variable, use this pattern: `##(keyname)` without parenthesis
Example:
I store the value of response header "X-AUTH-TOKEN" as token in scenario scope
I set header "X-AUTH-TOKEN" with value "`##token`"
This can be used for Authentication headers.
Sample Feature files in examples/scope folder.
- Add steps for setting Cookies
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
If this project was useful to you in some form, I would be glad to have your support. It will help to keep the project alive and to have more time to work on Open Source.
The sinplest form of support is to give a ⭐️ to this repo.
You can also contribute with GitHub Sponsors.
Or if you prefer a one time donation to the project, you can simple:
👤 Bruno Paz
- Website: brunopaz.dev
- Github: @brpaz
- Twitter: @brunopaz88
Copyright Bruno Paz.
This project is MIT licensed.