https://github.com/godogx/clocksteps
Cucumber Clock steps for Golang
https://github.com/godogx/clocksteps
bdd clock cucumber functional-testing gherkin go go-clock godog godog-extension golang integration-testing testing time
Last synced: 7 months ago
JSON representation
Cucumber Clock steps for Golang
- Host: GitHub
- URL: https://github.com/godogx/clocksteps
- Owner: godogx
- License: mit
- Created: 2021-09-08T08:16:51.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-09-04T06:03:16.000Z (7 months ago)
- Last Synced: 2025-09-04T08:24:29.848Z (7 months ago)
- Topics: bdd, clock, cucumber, functional-testing, gherkin, go, go-clock, godog, godog-extension, golang, integration-testing, testing, time
- Language: Go
- Homepage:
- Size: 70.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cucumber Clock steps for Golang
[](https://github.com/godogx/clocksteps/releases/latest)
[](https://github.com/godogx/clocksteps/actions/workflows/test.yaml)
[](https://codecov.io/gh/godogx/clocksteps)
[](https://goreportcard.com/report/github.com/godogx/clocksteps)
[](https://pkg.go.dev/github.com/godogx/clocksteps)
`clocksteps` uses [`go.nhat.io/clock`](https://go.nhat.io/clock) to provide steps for `cucumber/godog` and
makes it easy to run tests with `time`.
## Prerequisites
- `Go >= 1.17`
## Usage
Initiate the clock and register it to the scenario.
```go
package mypackage
import (
"testing"
"github.com/cucumber/godog"
"github.com/godogx/clocksteps"
)
func TestIntegration(t *testing.T) {
clock := clocksteps.New()
suite := godog.TestSuite{
Name: "Integration",
TestSuiteInitializer: nil,
ScenarioInitializer: func(ctx *godog.ScenarioContext) {
clock.RegisterContext(ctx)
},
Options: &godog.Options{
Strict: true,
Output: out,
Randomize: rand.Int63(),
},
}
// Inject the clock to your application then run the suite.
status := suite.Run()
}
```
Read more about [`go.nhat.io/clock`](https://go.nhat.io/clock)
## Steps
### Set the time
By default, the clock always returns `time.Now()` unless you freeze or set it. For setting, you can use one of
these:
- `(?:the )?clock is at "([^"]*)"`
- `(?:the )?clock is set to "([^"]*)"`
- `sets? (?:the )?clock to "([^"]*)"`
- `now is "([^"]*)"`
They have the same effect, the clock will be set at a specific `time.Time`. The given can be in `RFC3339`
(`2006-01-02T15:04:05Z07:00`) or `YMD` (`2006-01-02`)
For example:
```gherkin
Scenario: Set time
Given the clock is at "2020-01-02T03:04:05Z"
Then the time is "2020-01-02T03:04:05Z"
Given the clock is set to "2020-02-03T04:05:06Z"
Then the time is "2020-02-03T04:05:06Z"
Given Someone sets the clock to "2020-03-04T05:06:07Z"
Then the time is "2020-03-04T05:06:07Z"
Given now is "2020-04-05T06:07:08Z"
Then the time is "2020-04-05T06:07:08Z"
```
### Adjust the time
After setting the clock, you can adjust the time by adding a `time.Duration`, some days, months or years with these:
- `adds? ([^\s]*) to (?:the )?clock`
- `adds? ([0-9]+) days? to (?:the )?clock`
- `adds? ([0-9]+) months? to (?:the )?clock`
- `adds? ([0-9]+) years? to (?:the )?clock`
- `adds? ([0-9]+) months?,? ([0-9]+) days? to (?:the )?clock`
- `adds? ([0-9]+) years?,? ([0-9]+) days? to (?:the )?clock`
- `adds? ([0-9]+) years?,? ([0-9]+) months? to (?:the )?clock`
- `adds? ([0-9]+) years?,? ([0-9]+) months?,? ([0-9]+) days? to (?:the )?clock`
**Important**: You have to set the clock before adjusting it. Otherwise you will get `clocksteps.ErrClockIsNotSet`
For example:
```gherkin
Scenario: Add time
Given the clock is at "2020-01-02T03:04:05Z"
And someone adds 1h5s to the clock
Then the time is "2020-01-02T04:04:10Z"
Given someone adds 2 days to the clock
Then the time is "2020-01-04T04:04:10Z"
```
### Freeze and release the clock
```gherkin
Scenario: Freeze and Release
Given the time is now
When I freeze the clock
And I wait for 50ms
Then the time is not now
When I wait for 50ms
And I release the clock
Then the time is now
```