⚠️ This package is deprecated, please use go.nhat.io/consolesteps instead.
consoledog
provides a new Console
for each cucumber/godog
Scenario.
Go >= 1.16
go get github.com/nhatthm/consoledog
Initialize a consoledog.Manager
with consoledog.New()
then add it into the ScenarioInitializer
. If you wish to add listeners to Manager.NewConsole
and
Manager.CloseConsole
event, use consoledog.WithStarter
and consoledog.WithCloser
option in the constructor.
For example:
package mypackage
import (
"math/rand"
"testing"
expect "github.com/Netflix/go-expect"
"github.com/cucumber/godog"
"github.com/nhatthm/consoledog"
)
type writer struct {
console *expect.Console
}
func (w *writer) registerContext(ctx *godog.ScenarioContext) {
ctx.Step(`write to console:`, func(s *godog.DocString) error {
_, err := w.console.Write([]byte(s.Content))
return err
})
}
func (w *writer) start(_ *godog.Scenario, console *expect.Console) {
w.console = console
}
func (w *writer) close(_ *godog.Scenario) {
w.console = nil
}
func TestIntegration(t *testing.T) {
t.Parallel()
w := &writer{}
m := consoledog.New(t,
consoledog.WithStarter(w.start),
consoledog.WithCloser(w.close),
)
suite := godog.TestSuite{
Name: "Integration",
ScenarioInitializer: func(ctx *godog.ScenarioContext) {
m.RegisterContext(ctx)
},
Options: &godog.Options{
Strict: true,
Output: out,
Randomize: rand.Int63(),
},
}
// Run the suite.
}
See more: #Examples
In case you want to resize the terminal (default is 80x100
) to avoid text wrapping, for example:
Then console output is:
"""
panic: could not build credentials provider option: unsupported credentials prov
ider
"""
Use consoledog.WithTermSize(cols, rows)
while initiating with consoledog.New()
, for example:
package mypackage
import (
"testing"
"github.com/nhatthm/consoledog"
)
func TestIntegration(t *testing.T) {
// ...
m := consoledog.New(t, consoledog.WithTermSize(100, 100))
// ...
}
Then your step will become:
Then console output is:
"""
panic: could not build credentials provider option: unsupported credentials provider
"""
Asserts the output of the console.
For example:
Scenario: Find all transaction in range with invalid format
When I run command "transactions -d --format invalid"
Then console output is:
"""
panic: unknown output format
"""
Full suite: https://github.com/nhatthm/consoledog/tree/master/features
If this project help you reduce time to develop, you can give me a cup of coffee :)
or scan this