Сравнить коммиты

..

Нет общих коммитов. «main» и «v0.7.7» имеют совершенно разные истории.
main ... v0.7.7

272 изменённых файлов: 12264 добавлений и 20123 удалений

6
.github/renovate.json предоставленный
Просмотреть файл

@ -1,6 +0,0 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"github>cucumber/renovate-config"
]
}

47
.github/workflows/gorelease.yml предоставленный
Просмотреть файл

@ -1,47 +0,0 @@
# Gorelease comments public API changes to pull request.
name: gorelease
on:
pull_request:
# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
env:
GO_VERSION: stable
jobs:
gorelease:
runs-on: ubuntu-latest
steps:
- name: Install Go stable
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v4
- name: Gorelease cache
uses: actions/cache@v4
with:
path: |
~/go/bin/gorelease
key: ${{ runner.os }}-gorelease-generic
- name: Gorelease
id: gorelease
run: |
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
OUTPUT=$(gorelease 2>&1 || exit 0)
echo "${OUTPUT}"
OUTPUT="${OUTPUT//$'\n'/%0A}"
echo "report=$OUTPUT" >> $GITHUB_OUTPUT
- name: Comment Report
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
with:
header: gorelease
message: |
### Go API Changes
<pre>
${{ steps.gorelease.outputs.report }}
</pre>

44
.github/workflows/release-assets.yml предоставленный
Просмотреть файл

@ -1,44 +0,0 @@
# This script uploads application binaries as GitHub release assets.
name: release-assets
on:
release:
types:
- created
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
name: Upload Release Assets
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: stable
- name: Checkout code
uses: actions/checkout@v4
- name: Build artifacts
run: |
make artifacts
- name: Upload linux amd64 binary
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./_artifacts/godog-${{ github.event.release.tag_name }}-linux-amd64.tar.gz
asset_name: godog-${{ github.event.release.tag_name }}-linux-amd64.tar.gz
asset_content_type: application/tar+gzip
- name: Upload linux arm64 binary
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./_artifacts/godog-${{ github.event.release.tag_name }}-linux-arm64.tar.gz
asset_name: godog-${{ github.event.release.tag_name }}-linux-arm64.tar.gz
asset_content_type: application/tar+gzip
- name: Upload darwin amd64 binary
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./_artifacts/godog-${{ github.event.release.tag_name }}-darwin-amd64.tar.gz
asset_name: godog-${{ github.event.release.tag_name }}-darwin-amd64.tar.gz
asset_content_type: application/tar+gzip

55
.github/workflows/test.yml предоставленный
Просмотреть файл

@ -1,55 +0,0 @@
name: test
on:
push:
branches:
- main
pull_request:
jobs:
test:
strategy:
matrix:
go-version: [ 1.16.x, 1.17.x, oldstable, stable ] # Lowest supported and current stable versions.
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v4
- name: Go cache
uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-cache
- name: Run gofmt
run: gofmt -d -e . 2>&1 | tee outfile && test -z "$(cat outfile)" && rm outfile
- name: Run staticcheck
if: matrix.go-version == 'stable'
uses: dominikh/staticcheck-action@v1.3.1
with:
version: "latest"
install-go: false
cache-key: ${{ matrix.go }}
- name: Run go vet
run: |
go vet ./...
cd _examples && go vet ./... && cd ..
- name: Run go test
run: |
go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
cd _examples && go test -v -race ./... && cd ..
- name: Run godog
run: |
go install ./cmd/godog
godog -f progress --strict
- name: Report on code coverage
if: matrix.go-version == 'stable'
uses: codecov/codecov-action@v4
with:
file: ./coverage.txt

11
.gitignore предоставленный
Просмотреть файл

@ -1,13 +1,2 @@
/cmd/godog/godog
/example/example
**/vendor/*
Gopkg.lock
Gopkg.toml
.DS_Store
.idea
.vscode
_artifacts
vendor

24
.travis.yml Обычный файл
Просмотреть файл

@ -0,0 +1,24 @@
language: go
go:
- 1.5.x
- 1.6.x
- 1.7.x
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x
go_import_path: github.com/DATA-DOG/godog
install: go install github.com/DATA-DOG/godog/cmd/godog
script:
- go vet github.com/DATA-DOG/godog
- go vet github.com/DATA-DOG/godog/gherkin
- go vet github.com/DATA-DOG/godog/colors
- test -z "$(go fmt ./...)" # fail if not formatted properly
- godog -f progress
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash)

Просмотреть файл

@ -1,269 +1,85 @@
# Changelog
# Change LOG
All notable changes to this project will be documented in this file.
**2018-03-04**
- support go1.10 new compiler and linker changes for **godog** command.
This project adheres to [Semantic Versioning](http://semver.org).
**2017-08-31**
- added **BeforeFeature** and **AfterFeature** hooks.
- failed multistep error is now prepended with a parent step text in order
to determine failed nested step.
- pretty format now removes the step definition location package name in
comment next to step if the step definition matches tested package. If
step definition is imported from other package, full package name will
be printed.
This document is formatted according to the principles of [Keep A CHANGELOG](http://keepachangelog.com).
**2017-05-04**
- added **--strict** option in order to fail suite when there are pending
or undefined steps. By default, suite passes and treats pending or
undefined steps as TODOs.
## Unreleased
**2017-04-29** - **v0.7.0**
- added support for nested steps. From now on, it is possible to return
**godog.Steps** instead of an **error** in the step definition func.
This change introduced few minor changes in **Formatter** interface. Be
sure to adapt the changes if you have custom formatters.
### Added
- Step text is added to "step is undefined" error - ([669](https://github.com/cucumber/godog/pull/669) - [vearutop](https://github.com/vearutop))
**2017-04-27**
- added an option to randomize scenario execution order, so we could
ensure that scenarios do not depend on global state.
- godog was manually sorting feature files by name. Now it just runs them
in given order, you may sort them anyway you like. For example `godog
$(find . -name '*.feature' | sort)`
### Changed
- Replace deprecated `::set-output` - ([681](https://github.com/cucumber/godog/pull/681) - [nodeg](https://github.com/nodeg))
**2016-10-30** - **v0.6.0**
- added experimental **events** format, this might be used for unified
cucumber formats. But should be not adapted widely, since it is highly
possible that specification will change.
- added **RunWithOptions** method which allows to easily run godog from
**TestMain** without needing to simulate flag arguments. These options
now allows to configure output writer.
- added flag **-o, --output=runner.binary** which only compiles the test
runner executable, but does not execute it.
- **FlagSet** initialization now takes io.Writer as output for help text
output. It was not showing nice colors on windows before.
**--no-colors** option only applies to test run output.
### Fixed
- fix(errors): fix(errors): Fix expected Step argument count for steps with `context.Context` ([679](https://github.com/cucumber/godog/pull/679) - [tigh-latte](https://github.com/tigh-latte))
- fix(formatter): On concurrent execution, execute formatter at end of Scenario - ([645](https://github.com/cucumber/godog/pull/645) - [tigh-latte](https://github.com/tigh-latte))
**2016-06-14** - **v0.5.0**
- godog now uses **go tool compile** and **go tool link** to support
vendor directory dependencies. It also compiles test executable the same
way as standard **go test** utility. With this change, only go
versions from **1.5** are now supported.
## [v0.15.0]
**2016-06-01**
- parse flags in main command, to show version and help without needing
to compile test package and buildable go sources.
### Added
- Improved the type checking of step return types and improved the error messages - ([647](https://github.com/cucumber/godog/pull/647) - [johnlon](https://github.com/johnlon))
- Ambiguous step definitions will now be detected when strict mode is activated - ([636](https://github.com/cucumber/godog/pull/636)/([648](https://github.com/cucumber/godog/pull/648) - [johnlon](https://github.com/johnlon))
- Provide support for attachments / embeddings including a new example in the examples dir - ([623](https://github.com/cucumber/godog/pull/623) - [johnlon](https://github.com/johnlon))
**2016-05-28**
- show nicely formatted called step func name and file path
### Changed
- Formatters now have a `Close` method and associated `io.Writer` changed to `io.WriteCloser`.
**2016-05-26**
- pack gherkin dependency in a subpackage to prevent compatibility
conflicts in the future. If recently upgraded, probably you will need to
reference gherkin as `github.com/DATA-DOG/godog/gherkin` instead.
## [v0.14.1]
**2016-05-25**
- refactored test suite build tooling in order to use standard **go test**
tool. Which allows to compile package with godog runner script in **go**
idiomatic way. It also supports all build environment options as usual.
- **godog.Run** now returns an **int** exit status. It was not returning
anything before, so there is no compatibility breaks.
### Added
- Provide testing.T-compatible interface on test context, allowing usage of assertion libraries such as testify's assert/require - ([571](https://github.com/cucumber/godog/pull/571) - [mrsheepuk](https://github.com/mrsheepuk))
- Created releasing guidelines - ([608](https://github.com/cucumber/godog/pull/608) - [glibas](https://github.com/glibas))
**2016-03-04**
- added **junit** compatible output formatter, which prints **xml**
results to **os.Stdout**
- fixed #14 which skipped printing background steps when there was
scenario outline in feature.
### Fixed
- Step duration calculation - ([616](https://github.com/cucumber/godog/pull/616) - [iaroslav-ciupin](https://github.com/iaroslav-ciupin))
- Invalid memory address or nil pointer dereference in RetrieveFeatures - ([566](https://github.com/cucumber/godog/pull/566) - [corneldamian](https://github.com/corneldamian))
**2015-07-03**
- changed **godog.Suite** from interface to struct. Context registration should be updated accordingly. The reason
for change: since it exports the same methods and there is no need to mock a function in tests, there is no
obvious reason to keep an interface.
- in order to support running suite concurrently, needed to refactor an entry point of application. The **Run** method
now is a func of godog package which initializes and run the suite (or more suites). Method **New** is removed. This
change made godog a little cleaner.
- renamed **RegisterFormatter** func to **Format** to be more consistent.
## [v0.14.0]
### Added
- Improve ErrSkip handling, add test for Summary and operations order ([584](https://github.com/cucumber/godog/pull/584) - [vearutop](https://github.com/vearutop))
### Fixed
- Remove line overwriting for scenario outlines in cucumber formatter ([605](https://github.com/cucumber/godog/pull/605) - [glibas](https://github.com/glibas))
- Remove duplicate warning message ([590](https://github.com/cucumber/godog/pull/590) - [vearutop](https://github.com/vearutop))
- updated base formatter to set a scenario as passed unless there exist ([582](https://github.com/cucumber/godog/pull/582) - [roskee](https://github.com/roskee))
### Changed
- Update test.yml ([583](https://github.com/cucumber/godog/pull/583) - [vearutop](https://github.com/vearutop))
## [v0.13.0]
### Added
- Support for reading feature files from an `fs.FS` ([550](https://github.com/cucumber/godog/pull/550) - [tigh-latte](https://github.com/tigh-latte))
- Added keyword functions. ([509](https://github.com/cucumber/godog/pull/509) - [otrava7](https://github.com/otrava7))
- Prefer go test to use of godog cli in README ([548](https://github.com/cucumber/godog/pull/548) - [danielhelfand](https://github.com/danielhelfand))
- Use `fs.FS` abstraction for filesystem ([550](https://github.com/cucumber/godog/pull/550) - [tigh-latte](https://github.com/tigh-latte))
- Cancel context for each scenario ([514](https://github.com/cucumber/godog/pull/514) - [draganm](https://github.com/draganm))
### Fixed
- Improve hooks invocation flow ([568](https://github.com/cucumber/godog/pull/568) - [vearutop](https://github.com/vearutop))
- Result of testing.T respect strict option ([539](https://github.com/cucumber/godog/pull/539) - [eiel](https://github.com/eiel))
### Changed
- BREAKING CHANGE, upgraded cucumber and messages dependencies = ([515](https://github.com/cucumber/godog/pull/515) - [otrava7](https://github.com/otrava7))
## [v0.12.6]
### Changed
- Each scenario is run with a cancellable `context.Context` which is cancelled at the end of the scenario. ([514](https://github.com/cucumber/godog/pull/514) - [draganm](https://github.com/draganm))
- README example is updated with `context.Context` and `go test` usage. ([477](https://github.com/cucumber/godog/pull/477) - [vearutop](https://github.com/vearutop))
- Removed deprecation of `godog.BindFlags`. ([498](https://github.com/cucumber/godog/pull/498) - [vearutop](https://github.com/vearutop))
- Pretty Print when using rules. ([480](https://github.com/cucumber/godog/pull/480) - [dumpsterfireproject](https://github.com/dumpsterfireproject))
### Fixed
- Fixed a bug which would ignore the context returned from a substep.([488](https://github.com/cucumber/godog/pull/488) - [wichert](https://github.com/wichert))
- Fixed a bug which would cause a panic when using the pretty formatter with a feature that contained a rule. ([480](https://github.com/cucumber/godog/pull/480) - [dumpsterfireproject](https://github.com/dumpsterfireproject))
- Multiple invocations of AfterScenario hooks in case of undefined steps. ([494](https://github.com/cucumber/godog/pull/494) - [vearutop](https://github.com/vearutop))
- Add a check for missing test files and raise a more helpful error. ([468](https://github.com/cucumber/godog/pull/468) - [ALCooper12](https://github.com/ALCooper12))
- Fix version subcommand. Do not print usage if run subcommand fails. ([475](https://github.com/cucumber/godog/pull/475) - [coopernurse](https://github.com/coopernurse))
### Added
- Add new option for created features with parsing from byte slices. ([476](https://github.com/cucumber/godog/pull/476) - [akaswenwilk](https://github.com/akaswenwilk))
### Deprecated
- `godog` CLI tool prints deprecation warning. ([489](https://github.com/cucumber/godog/pull/489) - [vearutop](https://github.com/vearutop))
## [v0.12.5]
### Changed
- Changed underlying cobra command setup to return errors instead of calling `os.Exit` directly to enable simpler testing. ([454](https://github.com/cucumber/godog/pull/454) - [mxygem](https://github.com/mxygem))
- Remove use of deprecated methods from `_examples`. ([460](https://github.com/cucumber/godog/pull/460) - [ricardogarfe](https://github.com/ricardogarfe))
### Fixed
- Support for go1.18 in `godog` cli mode ([466](https://github.com/cucumber/godog/pull/466) - [vearutop](https://github.com/vearutop))
## [v0.12.4]
### Added
- Allow suite-level configuration of steps and hooks ([453](https://github.com/cucumber/godog/pull/453) - [vearutop](https://github.com/vearutop))
## [v0.12.3]
### Added
- Automated binary releases with GitHub Actions ([437](https://github.com/cucumber/godog/pull/437) - [vearutop](https://github.com/vearutop))
- Automated binary versioning with `go install` ([437](https://github.com/cucumber/godog/pull/437) - [vearutop](https://github.com/vearutop))
- Module with local replace in examples ([437](https://github.com/cucumber/godog/pull/437) - [vearutop](https://github.com/vearutop))
### Changed
- suggest to use `go install` instead of the deprecated `go get` to install the `godog` binary ([449](https://github.com/cucumber/godog/pull/449) - [dmitris](https://github.com/dmitris))
### Fixed
- After Scenario hook is called before After Step ([444](https://github.com/cucumber/godog/pull/444) - [vearutop](https://github.com/vearutop))
- `check-go-version` in Makefile to run on WSL. ([443](https://github.com/cucumber/godog/pull/443) - [mxygem](https://github.com/mxygem))
## [v0.12.2]
### Fixed
- Error in `go mod tidy` with `GO111MODULE=off` ([436](https://github.com/cucumber/godog/pull/436) - [vearutop](https://github.com/vearutop))
## [v0.12.1]
### Fixed
- Unintended change of behavior in before step hook ([424](https://github.com/cucumber/godog/pull/424) - [nhatthm](https://github.com/nhatthm))
## [v0.12.0]
### Added
- Support for step definitions without return ([364](https://github.com/cucumber/godog/pull/364) - [titouanfreville](https://github.com/titouanfreville))
- Contextualized hooks for scenarios and steps ([409](https://github.com/cucumber/godog/pull/409) - [vearutop](https://github.com/vearutop))
- Step result status in After hook ([409](https://github.com/cucumber/godog/pull/409) - [vearutop](https://github.com/vearutop))
- Support auto converting doc strings to plain strings ([380](https://github.com/cucumber/godog/pull/380) - [chirino](https://github.com/chirino))
- Use multiple formatters in the same test run ([392](https://github.com/cucumber/godog/pull/392) - [vearutop](https://github.com/vearutop))
- Added `RetrieveFeatures()` method to `godog.TestSuite` ([276](https://github.com/cucumber/godog/pull/276) - [radtriste](https://github.com/radtriste))
- Added support to create custom formatters ([372](https://github.com/cucumber/godog/pull/372) - [leviable](https://github.com/leviable))
### Changed
- Upgraded gherkin-go to v19 and messages-go to v16 ([402](https://github.com/cucumber/godog/pull/402) - [mbow](https://github.com/mbow))
- Generate simpler snippets that use *godog.DocString and *godog.Table ([379](https://github.com/cucumber/godog/pull/379) - [chirino](https://github.com/chirino))
### Deprecated
- `ScenarioContext.BeforeScenario`, use `ScenarioContext.Before` ([409](https://github.com/cucumber/godog/pull/409)) - [vearutop](https://github.com/vearutop))
- `ScenarioContext.AfterScenario`, use `ScenarioContext.After` ([409](https://github.com/cucumber/godog/pull/409)) - [vearutop](https://github.com/vearutop))
- `ScenarioContext.BeforeStep`, use `ScenarioContext.StepContext().Before` ([409](https://github.com/cucumber/godog/pull/409)) - [vearutop](https://github.com/vearutop))
- `ScenarioContext.AfterStep`, use `ScenarioContext.StepContext().After` ([409](https://github.com/cucumber/godog/pull/409)) - [vearutop](https://github.com/vearutop))
### Fixed
- Incorrect step definition output for Data Tables ([411](https://github.com/cucumber/godog/pull/411) - [karfrank](https://github.com/karfrank))
- `ScenarioContext.AfterStep` not invoked after a failed case ([409](https://github.com/cucumber/godog/pull/409) - [vearutop](https://github.com/vearutop)))
- Can't execute multiple specific scenarios in the same feature file ([414](https://github.com/cucumber/godog/pull/414) - [vearutop](https://github.com/vearutop)))
## [v0.11.0]
### Added
- Created a simple example for a custom formatter ([330](https://github.com/cucumber/godog/pull/330) - [lonnblad](https://github.com/lonnblad))
- --format junit:result.xml will now write to result.xml ([331](https://github.com/cucumber/godog/pull/331) - [lonnblad](https://github.com/lonnblad))
- Added make commands to create artifacts and upload them to a github release ([333](https://github.com/cucumber/godog/pull/333) - [lonnblad](https://github.com/lonnblad))
- Created release notes and changelog for v0.11.0 ([355](https://github.com/cucumber/godog/pull/355) - [lonnblad](https://github.com/lonnblad))
- Created v0.11.0-rc2 ([362](https://github.com/cucumber/godog/pull/362) - [lonnblad](https://github.com/lonnblad))
### Changed
- Added Cobra for the Command Line Interface ([321](https://github.com/cucumber/godog/pull/321) - [lonnblad](https://github.com/lonnblad))
- Added internal packages for formatters, storage and models ([323](https://github.com/cucumber/godog/pull/323) - [lonnblad](https://github.com/lonnblad))
- Added an internal package for tags filtering ([326](https://github.com/cucumber/godog/pull/326) - [lonnblad](https://github.com/lonnblad))
- Added an internal pkg for the builder ([327](https://github.com/cucumber/godog/pull/327) - [lonnblad](https://github.com/lonnblad))
- Moved the parser code to a new internal pkg ([329](https://github.com/cucumber/godog/pull/329) - [lonnblad](https://github.com/lonnblad))
- Moved StepDefinition to the formatters pkg ([332](https://github.com/cucumber/godog/pull/332) - [lonnblad](https://github.com/lonnblad))
- Removed go1.12 and added go1.15 to CI config ([356](https://github.com/cucumber/godog/pull/356) - [lonnblad](https://github.com/lonnblad))
### Fixed
- Improved the help text of the formatter flag in the run command ([347](https://github.com/cucumber/godog/pull/347) - [lonnblad](https://github.com/lonnblad))
- Removed $GOPATH from the README.md and updated the example ([349](https://github.com/cucumber/godog/pull/349) - [lonnblad](https://github.com/lonnblad))
- Fixed the undefined step definitions help ([350](https://github.com/cucumber/godog/pull/350) - [lonnblad](https://github.com/lonnblad))
- Added a comment regarding running the examples within the $GOPATH ([352](https://github.com/cucumber/godog/pull/352) - [lonnblad](https://github.com/lonnblad))
- doc(FAQ/TestMain): `testing.M.Run()` is optional ([353](https://github.com/cucumber/godog/pull/353) - [hansbogert](https://github.com/hansbogert))
- Made a fix for the unstable Randomize Run tests ([354](https://github.com/cucumber/godog/pull/354) - [lonnblad](https://github.com/lonnblad))
- Fixed an issue when go test is parsing command-line flags ([359](https://github.com/cucumber/godog/pull/359) - [lonnblad](https://github.com/lonnblad))
- Make pickleStepIDs unique accross multiple paths ([366](https://github.com/cucumber/godog/pull/366) - [rickardenglund](https://github.com/rickardenglund))
### Removed
- Removed deprecated code ([322](https://github.com/cucumber/godog/pull/322) - [lonnblad](https://github.com/lonnblad))
## [v0.10.0]
### Added
- Added concurrency support to the pretty formatter ([275](https://github.com/cucumber/godog/pull/275) - [lonnblad](https://github.com/lonnblad))
- Added concurrency support to the events formatter ([274](https://github.com/cucumber/godog/pull/274) - [lonnblad](https://github.com/lonnblad))
- Added concurrency support to the cucumber formatter ([273](https://github.com/cucumber/godog/pull/273) - [lonnblad](https://github.com/lonnblad))
- Added an example for how to use assertion pkgs like testify with godog ([289](https://github.com/cucumber/godog/pull/289) - [lonnblad](https://github.com/lonnblad))
- Added the new TestSuiteInitializer and ScenarioInitializer ([294](https://github.com/cucumber/godog/pull/294) - [lonnblad](https://github.com/lonnblad))
- Added an in-mem storage for pickles ([304](https://github.com/cucumber/godog/pull/304) - [lonnblad](https://github.com/lonnblad))
- Added Pickle and PickleStep results to the in-mem storage ([305](https://github.com/cucumber/godog/pull/305) - [lonnblad](https://github.com/lonnblad))
- Added features to the in-mem storage ([306](https://github.com/cucumber/godog/pull/306) - [lonnblad](https://github.com/lonnblad))
- Broke out some code from massive files into new files ([307](https://github.com/cucumber/godog/pull/307) - [lonnblad](https://github.com/lonnblad))
- Added support for concurrent scenarios ([311](https://github.com/cucumber/godog/pull/311) - [lonnblad](https://github.com/lonnblad))
### Changed
- Broke out snippets gen and added sorting on method name ([271](https://github.com/cucumber/godog/pull/271) - [lonnblad](https://github.com/lonnblad))
- Updated so that we run all tests concurrent now ([278](https://github.com/cucumber/godog/pull/278) - [lonnblad](https://github.com/lonnblad))
- Moved fmt tests to a godog_test pkg and restructured the fmt output tests ([295](https://github.com/cucumber/godog/pull/295) - [lonnblad](https://github.com/lonnblad))
- Moved builder tests to a godog_test pkg ([296](https://github.com/cucumber/godog/pull/296) - [lonnblad](https://github.com/lonnblad))
- Made the builder tests run in parallel ([298](https://github.com/cucumber/godog/pull/298) - [lonnblad](https://github.com/lonnblad))
- Refactored suite_context.go ([300](https://github.com/cucumber/godog/pull/300) - [lonnblad](https://github.com/lonnblad))
- Added better testing of the Context Initializers and TestSuite{}.Run() ([301](https://github.com/cucumber/godog/pull/301) - [lonnblad](https://github.com/lonnblad))
- Updated the README.md ([302](https://github.com/cucumber/godog/pull/302) - [lonnblad](https://github.com/lonnblad))
- Unexported some exported properties in unexported structs ([303](https://github.com/cucumber/godog/pull/303) - [lonnblad](https://github.com/lonnblad))
- Refactored some states in the formatters and feature struct ([310](https://github.com/cucumber/godog/pull/310) - [lonnblad](https://github.com/lonnblad))
### Deprecated
- Deprecated SuiteContext and ConcurrentFormatter ([314](https://github.com/cucumber/godog/pull/314) - [lonnblad](https://github.com/lonnblad))
### Fixed
- Fixed failing builder tests due to the v0.9.0 change ([lonnblad](https://github.com/lonnblad))
- Update paths to screenshots for examples ([270](https://github.com/cucumber/godog/pull/270) - [leviable](https://github.com/leviable))
- Made progress formatter verification a bit more accurate ([lonnblad](https://github.com/lonnblad))
- Added comparison between single and multi threaded runs ([272](https://github.com/cucumber/godog/pull/272) - [lonnblad](https://github.com/lonnblad))
- Fixed issue with empty feature file causing nil pointer deref ([288](https://github.com/cucumber/godog/pull/288) - [lonnblad](https://github.com/lonnblad))
- Updated linting checks in circleci config and fixed linting issues ([290](https://github.com/cucumber/godog/pull/290) - [lonnblad](https://github.com/lonnblad))
- Readded some legacy doc for FeatureContext ([297](https://github.com/cucumber/godog/pull/297) - [lonnblad](https://github.com/lonnblad))
- Fixed an issue with calculating time for junit testsuite ([308](https://github.com/cucumber/godog/pull/308) - [lonnblad](https://github.com/lonnblad))
- Fixed so that we don't execute features with zero scenarios ([315](https://github.com/cucumber/godog/pull/315) - [lonnblad](https://github.com/lonnblad))
- Fixed the broken --random flag ([317](https://github.com/cucumber/godog/pull/317) - [lonnblad](https://github.com/lonnblad))
### Removed
- Removed pre go112 build code ([293](https://github.com/cucumber/godog/pull/293) - [lonnblad](https://github.com/lonnblad))
- Removed the deprecated feature hooks ([312](https://github.com/cucumber/godog/pull/312) - [lonnblad](https://github.com/lonnblad))
## [0.9.0]
### Changed
- Run godog features in CircleCI in strict mode ([mxygem](https://github.com/mxygem))
- Removed TestMain call in `suite_test.go` for CI. ([mxygem](https://github.com/mxygem))
- Migrated to [gherkin-go - v11.0.0](https://github.com/cucumber/gherkin-go/releases/tag/v11.0.0). ([240](https://github.com/cucumber/godog/pull/240) - [lonnblad](https://github.com/lonnblad))
### Fixed
- Fixed the time attributes in the JUnit formatter. ([232](https://github.com/cucumber/godog/pull/232) - [lonnblad](https://github.com/lonnblad))
- Re enable custom formatters. ([238](https://github.com/cucumber/godog/pull/238) - [ericmcbride](https://github.com/ericmcbride))
- Added back suite_test.go ([mxygem](https://github.com/mxygem))
- Normalise module paths for use on Windows ([242](https://github.com/cucumber/godog/pull/242) - [gjtaylor](https://github.com/gjtaylor))
- Fixed panic in indenting function `s` ([247](https://github.com/cucumber/godog/pull/247) - [titouanfreville](https://github.com/titouanfreville))
- Fixed wrong version in API example ([263](https://github.com/cucumber/godog/pull/263) - [denis-trofimov](https://github.com/denis-trofimov))
## [0.8.1]
### Added
- Link in Readme to the Slack community. ([210](https://github.com/cucumber/godog/pull/210) - [smikulcik](https://github.com/smikulcik))
- Added run tests for Cucumber formatting. ([214](https://github.com/cucumber/godog/pull/214), [216](https://github.com/cucumber/godog/pull/216) - [lonnblad](https://github.com/lonnblad))
### Changed
- Renamed the `examples` directory to `_examples`, removing dependencies from the Go module ([218](https://github.com/cucumber/godog/pull/218) - [axw](https://github.com/axw))
### Fixed
- Find/Replaced references to DATA-DOG/godog -> cucumber/godog for docs. ([209](https://github.com/cucumber/godog/pull/209) - [smikulcik](https://github.com/smikulcik))
- Fixed missing links in changelog to be correctly included! ([mxygem](https://github.com/mxygem))
## [0.8.0]
### Added
- Added initial CircleCI config. ([mxygem](https://github.com/mxygem))
- Added concurrency support for JUnit formatting ([lonnblad](https://github.com/lonnblad))
### Changed
- Changed code references to DATA-DOG/godog to cucumber/godog to help get things building correctly. ([mxygem](https://github.com/mxygem))
[v0.15.0]: https://github.com/cucumber/godog/compare/v0.14.1...v0.15.0
[v0.14.1]: https://github.com/cucumber/godog/compare/v0.14.0...v0.14.1
[v0.14.0]: https://github.com/cucumber/godog/compare/v0.13.0...v0.14.0
[v0.13.0]: https://github.com/cucumber/godog/compare/v0.12.6...v0.13.0
[v0.12.6]: https://github.com/cucumber/godog/compare/v0.12.5...v0.12.6
[v0.12.5]: https://github.com/cucumber/godog/compare/v0.12.4...v0.12.5
[v0.12.4]: https://github.com/cucumber/godog/compare/v0.12.3...v0.12.4
[v0.12.3]: https://github.com/cucumber/godog/compare/v0.12.2...v0.12.3
[v0.12.2]: https://github.com/cucumber/godog/compare/v0.12.1...v0.12.2
[v0.12.1]: https://github.com/cucumber/godog/compare/v0.12.0...v0.12.1
[v0.12.0]: https://github.com/cucumber/godog/compare/v0.11.0...v0.12.0
[v0.11.0]: https://github.com/cucumber/godog/compare/v0.10.0...v0.11.0
[v0.10.0]: https://github.com/cucumber/godog/compare/v0.9.0...v0.10.0
[0.9.0]: https://github.com/cucumber/godog/compare/v0.8.1...v0.9.0
[0.8.1]: https://github.com/cucumber/godog/compare/v0.8.0...v0.8.1
[0.8.0]: https://github.com/cucumber/godog/compare/v0.7.13...v0.8.0

Просмотреть файл

@ -1,113 +0,0 @@
# Change LOG
**2020-02-06**
- move to new [CHANGELOG.md](CHANGELOG.md)
**2020-01-31**
- change license to MIT and moving project repository to **cucumber**
organization.
**2018-11-16**
- added formatter output test suite, currently mainly pretty format
tested.
- these tests, helped to identify some output format issues.
**2018-11-12**
- proper go module support added for `godog` command build.
- added build tests.
**2018-10-27**
- support go1.11 new compiler and linker changes for **godog** command.
- support go1.11 modules and `go mod` builds.
- `BindFlags` now has a prefix option for flags, so that `go test` command
can avoid flag name collisions.
- `BindFlags` respect default options provided for binding, so that it
does not override predefined options when flags are bind, see #144.
- Minor patch to support tag filters on example tables for
ScenarioOutline.
- Minor patch for pretty printer, when scenario has no steps, comment
possition computation was in panic.
**2018-03-04**
- support go1.10 new compiler and linker changes for **godog** command.
**2017-08-31**
- added **BeforeFeature** and **AfterFeature** hooks.
- failed multistep error is now prepended with a parent step text in order
to determine failed nested step.
- pretty format now removes the step definition location package name in
comment next to step if the step definition matches tested package. If
step definition is imported from other package, full package name will
be printed.
**2017-05-04**
- added **--strict** option in order to fail suite when there are pending
or undefined steps. By default, suite passes and treats pending or
undefined steps as TODOs.
**2017-04-29** - **v0.7.0**
- added support for nested steps. From now on, it is possible to return
**godog.Steps** instead of an **error** in the step definition func.
This change introduced few minor changes in **Formatter** interface. Be
sure to adapt the changes if you have custom formatters.
**2017-04-27**
- added an option to randomize scenario execution order, so we could
ensure that scenarios do not depend on global state.
- godog was manually sorting feature files by name. Now it just runs them
in given order, you may sort them anyway you like. For example `godog
$(find . -name '*.feature' | sort)`
**2016-10-30** - **v0.6.0**
- added experimental **events** format, this might be used for unified
cucumber formats. But should be not adapted widely, since it is highly
possible that specification will change.
- added **RunWithOptions** method which allows to easily run godog from
**TestMain** without needing to simulate flag arguments. These options
now allows to configure output writer.
- added flag **-o, --output=runner.binary** which only compiles the test
runner executable, but does not execute it.
- **FlagSet** initialization now takes io.Writer as output for help text
output. It was not showing nice colors on windows before.
**--no-colors** option only applies to test run output.
**2016-06-14** - **v0.5.0**
- godog now uses **go tool compile** and **go tool link** to support
vendor directory dependencies. It also compiles test executable the same
way as standard **go test** utility. With this change, only go
versions from **1.5** are now supported.
**2016-06-01**
- parse flags in main command, to show version and help without needing
to compile test package and buildable go sources.
**2016-05-28**
- show nicely formatted called step func name and file path
**2016-05-26**
- pack gherkin dependency in a subpackage to prevent compatibility
conflicts in the future. If recently upgraded, probably you will need to
reference gherkin as `github.com/DATA-DOG/godog/gherkin` instead.
**2016-05-25**
- refactored test suite build tooling in order to use standard **go test**
tool. Which allows to compile package with godog runner script in **go**
idiomatic way. It also supports all build environment options as usual.
- **godog.Run** now returns an **int** exit status. It was not returning
anything before, so there is no compatibility breaks.
**2016-03-04**
- added **junit** compatible output formatter, which prints **xml**
results to **os.Stdout**
- fixed #14 which skipped printing background steps when there was
scenario outline in feature.
**2015-07-03**
- changed **godog.Suite** from interface to struct. Context registration should be updated accordingly. The reason
for change: since it exports the same methods and there is no need to mock a function in tests, there is no
obvious reason to keep an interface.
- in order to support running suite concurrently, needed to refactor an entry point of application. The **Run** method
now is a func of godog package which initializes and run the suite (or more suites). Method **New** is removed. This
change made godog a little cleaner.
- renamed **RegisterFormatter** func to **Format** to be more consistent.

Просмотреть файл

@ -1,28 +0,0 @@
# Welcome 💖
Before anything else, thank you for taking some of your precious time to help this project move forward. ❤️
If you're new to open source and feeling a bit nervous 😳, we understand! We recommend watching [this excellent guide](https://egghead.io/talks/git-how-to-make-your-first-open-source-contribution)
to give you a grounding in some of the basic concepts. You could also watch [this talk](https://www.youtube.com/watch?v=tuSk6dMoTIs) from our very own wonderful [Marit van Dijk](https://github.com/mlvandijk) on her experiences contributing to Cucumber.
We want you to feel safe to make mistakes, and ask questions. If anything in this guide or anywhere else in the codebase doesn't make sense to you, please let us know! It's through your feedback that we can make this codebase more welcoming, so we'll be glad to hear thoughts.
You can chat with us in the `#committers` channel in our [community Discord](https://cucumber.io/docs/community/get-in-touch/#discord), or feel free to [raise an issue] if you're experiencing any friction trying make your contribution.
## Setup
To get your development environment set up, you'll need to [install Go]. We're currently using version 1.17 for development.
Once that's done, try running the tests:
make test
If everything passes, you're ready to hack!
[install go]: https://golang.org/doc/install
[community Discord]: https://cucumber.io/community#discord
[raise an issue]: https://github.com/cucumber/godog/issues/new/choose
## Changing dependencies
If dependencies have changed, you will also need to update the _examples module. `go mod tidy` should be sufficient.

41
LICENSE
Просмотреть файл

@ -1,21 +1,28 @@
The MIT License (MIT)
The three clause BSD license (http://en.wikipedia.org/wiki/BSD_licenses)
Copyright (c) SmartBear
Copyright (c) 2015-2018, DATA-DOG team
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name DataDog.lt may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Просмотреть файл

@ -1,138 +0,0 @@
//go:build mage
// +build mage
package main
import (
// mage:import
. "magefile/docker"
)
var (
GolangVolume = "golang.upstream"
)
func init() {
AppName = "godog"
ImageName = "my/go"
}
func Test() {
TestNative()
}
func TestNative() {
Bash(`sudo docker run -ti --rm \
-h host \
--net=bridge \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + `\
go test -race -count=1 ./... \
'`)
}
func TestMakefile() {
Bash(`sudo docker run -ti --rm \
-h host \
--net=bridge \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + `set -x; \
make test \
'`)
}
func TestSnippets() {
Bash(`sudo docker run -ti --rm \
-h host \
--net=bridge \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + `set -x; \
godog run -f progress -c 4 \
features/snippets.feature \
'`)
}
func TestTags() {
Bash(`sudo docker run -ti --rm \
-h host \
--net=bridge \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + `set -x; \
godog run -f progress -c 4 \
features/tags.feature \
'`)
}
func Install() {
Bash(`sudo docker run -ti --rm \
-h host \
--net=bridge \
-v /etc/localtime:/etc/localtime:ro \
-v ` + GolangVolume + `:/usr/local/go:ro \
\
-v /gopath:/gopath:rw \
-v ${PWD}:/app \
\
-e GOPATH=/gopath \
-e GOCACHE=/gopath/gocache \
\
-w /app \
-u 1000 \
\
--entrypoint=/bin/bash \
\
` + ImageName + " -c '" + `set -x; \
go install ./cmd/godog \
'`)
}

Просмотреть файл

@ -1,33 +1,16 @@
.PHONY: test gherkin bump cover
VERS ?= $(shell git symbolic-ref -q --short HEAD || git describe --tags --exact-match)
VERS := $(shell grep 'const Version' -m 1 godog.go | awk -F\" '{print $$2}')
GO_MAJOR_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
GO_MINOR_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
MINIMUM_SUPPORTED_GO_MAJOR_VERSION = 1
MINIMUM_SUPPORTED_GO_MINOR_VERSION = 16
GO_VERSION_VALIDATION_ERR_MSG = Go version $(GO_MAJOR_VERSION).$(GO_MINOR_VERSION) is not supported, please update to at least $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION).$(MINIMUM_SUPPORTED_GO_MINOR_VERSION)
.PHONY: check-go-version
check-go-version:
@if [ $(GO_MAJOR_VERSION) -gt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
exit 0 ;\
elif [ $(GO_MAJOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\
exit 1; \
elif [ $(GO_MINOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MINOR_VERSION) ] ; then \
echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\
exit 1; \
fi
test: check-go-version
test:
@echo "running all tests"
@go install ./...
@go fmt ./...
@go run honnef.co/go/tools/cmd/staticcheck@v0.5.1 git.golang1.ru/softonik/godog
@go run honnef.co/go/tools/cmd/staticcheck@v0.5.1 git.golang1.ru/softonik/godog/cmd/godog
@golint github.com/DATA-DOG/godog
@golint github.com/DATA-DOG/godog/cmd/godog
go vet ./...
go test -race ./...
go run ./cmd/godog -f progress -c 4
go test -race
godog -f progress -c 4
gherkin:
@if [ -z "$(VERS)" ]; then echo "Provide gherkin version like: 'VERS=commit-hash'"; exit 1; fi
@ -40,38 +23,10 @@ bump:
@if [ -z "$(VERSION)" ]; then echo "Provide version like: 'VERSION=$(VERS) make bump'"; exit 1; fi
@echo "bumping version from: $(VERS) to $(VERSION)"
@sed -i.bak 's/$(VERS)/$(VERSION)/g' godog.go
@sed -i.bak 's/$(VERS)/$(VERSION)/g' _examples/api/features/version.feature
@sed -i.bak 's/$(VERS)/$(VERSION)/g' examples/api/version.feature
@find . -name '*.bak' | xargs rm
cover:
go test -race -coverprofile=coverage.txt
go tool cover -html=coverage.txt
rm coverage.txt
ARTIFACT_DIR := _artifacts
# To upload artifacts for the current version;
# execute: make upload
#
# Check https://github.com/tcnksm/ghr for usage of ghr
upload: artifacts
ghr -replace $(VERS) $(ARTIFACT_DIR)
# To build artifacts for the current version;
# execute: make artifacts
artifacts:
rm -rf $(ARTIFACT_DIR)
mkdir $(ARTIFACT_DIR)
$(call _build,darwin,amd64)
$(call _build,linux,amd64)
$(call _build,linux,arm64)
define _build
mkdir $(ARTIFACT_DIR)/godog-$(VERS)-$1-$2
env GOOS=$1 GOARCH=$2 go build -ldflags "-X git.golang1.ru/softonik/godog.Version=$(VERS)" -o $(ARTIFACT_DIR)/godog-$(VERS)-$1-$2/godog ./cmd/godog
cp README.md $(ARTIFACT_DIR)/godog-$(VERS)-$1-$2/README.md
cp LICENSE $(ARTIFACT_DIR)/godog-$(VERS)-$1-$2/LICENSE
cd $(ARTIFACT_DIR) && tar -c --use-compress-program="pigz --fast" -f godog-$(VERS)-$1-$2.tar.gz godog-$(VERS)-$1-$2 && cd ..
rm -rf $(ARTIFACT_DIR)/godog-$(VERS)-$1-$2
endef

651
README.md
Просмотреть файл

@ -1,81 +1,85 @@
[![Build Status](https://github.com/cucumber/godog/workflows/test/badge.svg)](https://github.com/cucumber/godog/actions?query=branch%main+workflow%3Atest)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/cucumber/godog)](https://pkg.go.dev/github.com/cucumber/godog)
[![codecov](https://codecov.io/gh/cucumber/godog/branch/master/graph/badge.svg)](https://codecov.io/gh/cucumber/godog)
[![pull requests](https://oselvar.com/api/badge?label=pull%20requests&csvUrl=https%3A%2F%2Fraw.githubusercontent.com%2Fcucumber%2Foselvar-github-metrics%2Fmain%2Fdata%2Fcucumber%2Fgodog%2FpullRequests.csv)](https://oselvar.com/github/cucumber/oselvar-github-metrics/main/cucumber/godog)
[![issues](https://oselvar.com/api/badge?label=issues&csvUrl=https%3A%2F%2Fraw.githubusercontent.com%2Fcucumber%2Foselvar-github-metrics%2Fmain%2Fdata%2Fcucumber%2Fgodog%2Fissues.csv)](https://oselvar.com/github/cucumber/oselvar-github-metrics/main/cucumber/godog)
[![Build Status](https://travis-ci.org/DATA-DOG/godog.svg?branch=master)](https://travis-ci.org/DATA-DOG/godog)
[![GoDoc](https://godoc.org/github.com/DATA-DOG/godog?status.svg)](https://godoc.org/github.com/DATA-DOG/godog)
[![codecov.io](https://codecov.io/github/DATA-DOG/godog/branch/master/graph/badge.svg)](https://codecov.io/github/DATA-DOG/godog)
# Godog
<p align="center"><img src="logo.png" alt="Godog logo" style="width:250px;" /></p>
<p align="center"><img src="/logo.png" alt="Godog logo" style="width:250px;" /></p>
**The API is likely to change a few times before we reach 1.0.0**
Please read the full README, you may find it very useful. And do not forget to peek into the [Release Notes](https://github.com/cucumber/godog/blob/master/release-notes) and the [CHANGELOG](https://github.com/cucumber/godog/blob/master/CHANGELOG.md) from time to time.
Please read all the README, you may find it very useful. And do not forget
to peek into the
[CHANGELOG](https://github.com/DATA-DOG/godog/blob/master/CHANGELOG.md)
from time to time.
Package godog is the official Cucumber BDD framework for Golang, it merges specification and test documentation into one cohesive whole, using Gherkin formatted scenarios in the format of Given, When, Then.
Package godog is the official Cucumber BDD framework for Golang, it merges
specification and test documentation into one cohesive whole. The author
is a member of [cucumber team](https://github.com/cucumber).
The project was inspired by [behat][behat] and [cucumber][cucumber].
The project is inspired by [behat][behat] and [cucumber][cucumber] and is
based on cucumber [gherkin3 parser][gherkin].
## Why Godog/Cucumber
**Godog** does not intervene with the standard **go test** command
behavior. You can leverage both frameworks to functionally test your
application while maintaining all test related source code in **_test.go**
files.
### A single source of truth
**Godog** acts similar compared to **go test** command, by using go
compiler and linker tool in order to produce test executable. Godog
contexts need to be exported the same way as **Test** functions for go
tests. Note, that if you use **godog** command tool, it will use `go`
executable to determine compiler and linker.
Godog merges specification and test documentation into one cohesive whole.
**Godog** ships gherkin parser dependency as a subpackage. This will
ensure that it is always compatible with the installed version of godog.
So in general there are no vendor dependencies needed for installation.
### Living documentation
The following about section was taken from
[cucumber](https://cucumber.io/) homepage.
Because they're automatically tested by Godog, your specifications are
## About
#### A single source of truth
Cucumber merges specification and test documentation into one cohesive whole.
#### Living documentation
Because they're automatically tested by Cucumber, your specifications are
always bang up-to-date.
### Focus on the customer
#### Focus on the customer
Business and IT don't always understand each other. Godog's executable specifications encourage closer collaboration, helping teams keep the business goal in mind at all times.
Business and IT don't always understand each other. Cucumber's executable
specifications encourage closer collaboration, helping teams keep the
business goal in mind at all times.
### Less rework
#### Less rework
When automated testing is this much fun, teams can easily protect themselves from costly regressions.
When automated testing is this much fun, teams can easily protect
themselves from costly regressions.
### Read more
- [Behaviour-Driven Development](https://cucumber.io/docs/bdd/)
- [Gherkin Reference](https://cucumber.io/docs/gherkin/reference/)
## Install
## Contributions
go get github.com/DATA-DOG/godog/cmd/godog
Godog is a community driven Open Source Project within the Cucumber organization. We [welcome contributions from everyone](https://cucumber.io/blog/open-source/tackling-structural-racism-(and-sexism)-in-open-so/), and we're ready to support you if you have the enthusiasm to contribute.
## Example
See the [contributing guide] for more detail on how to get started.
The following example can be [found
here](/examples/godogs).
See the [releasing guide] for release flow details.
### Step 1
## Getting help
Given we create a new go package **$GOPATH/src/godogs**. From now on, this
is our work directory `cd $GOPATH/src/godogs`.
We have a [community Discord](https://cucumber.io/docs/community/get-in-touch/#discord) where you can chat with other users, developers, and BDD practitioners.
## Examples
You can find a few examples [here](/_examples).
**Note** that if you want to execute any of the examples and have the Git repository checked out in the `$GOPATH`, you need to use: `GO111MODULE=off`. [Issue](https://github.com/cucumber/godog/issues/344) for reference.
### Godogs
The following example can be [found here](/_examples/godogs).
#### Step 1 - Setup a go module
Create a new go module named **godogs** in your go workspace by running `mkdir godogs`
From now on, use **godogs** as your working directory by running `cd godogs`
Initiate the go module inside the **godogs** directory by running `go mod init godogs`
#### Step 2 - Create gherkin feature
Imagine we have a **godog cart** to serve godogs for lunch.
First of all, we describe our feature in plain text:
Imagine we have a **godog cart** to serve godogs for lunch. First of all,
we describe our feature in plain text - `vim
$GOPATH/src/godogs/features/godogs.feature`:
``` gherkin
# file: $GOPATH/src/godogs/features/godogs.feature
Feature: eat godogs
In order to be happy
As a hungry gopher
@ -87,72 +91,46 @@ Feature: eat godogs
Then there should be 7 remaining
```
Run `vim features/godogs.feature` and add the text above into the vim editor and save the file.
**NOTE:** same as **go test** godog respects package level isolation. All
your step definitions should be in your tested package root directory. In
this case - `$GOPATH/src/godogs`
#### Step 3 - Create godog step definitions
### Step 2
**NOTE:** Same as **go test**, godog respects package level isolation. All your step definitions should be in your tested package root directory. In this case: **godogs**.
If godog is installed in your GOPATH. We can run `godog` inside the
**$GOPATH/src/godogs** directory. You should see that the steps are
undefined:
![Undefined step snippets](/screenshots/undefined.png?raw=true)
If we wish to vendor godog dependency, we can do it as usual, using tools
you prefer:
git clone https://github.com/DATA-DOG/godog.git $GOPATH/src/godogs/vendor/github.com/DATA-DOG/godog
It gives you undefined step snippets to implement in your test context.
You may copy these snippets into your `godogs_test.go` file.
Our directory structure should now look like:
![Directory layout](/screenshots/dir-tree.png?raw=true)
If you copy the snippets into our test file and run godog again. We should
see the step definition is now pending:
![Pending step definition](/screenshots/pending.png?raw=true)
You may change **ErrPending** to **nil** and the scenario will
pass successfully.
Since we need a working implementation, we may start by implementing only what is necessary.
### Step 3
We only need a number of **godogs** for now. Lets keep it simple.
Create and copy the step definitions below into a new file by running `vim godogs_test.go`:
``` go
package main
import "github.com/cucumber/godog"
func iEat(arg1 int) error {
return godog.ErrPending
}
func thereAreGodogs(arg1 int) error {
return godog.ErrPending
}
func thereShouldBeRemaining(arg1 int) error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs)
ctx.Step(`^I eat (\d+)$`, iEat)
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
}
```
Alternatively, you can also specify the keyword (Given, When, Then...) when creating the step definitions:
``` go
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Given(`^there are (\d+) godogs$`, thereAreGodogs)
ctx.When(`^I eat (\d+)$`, iEat)
ctx.Then(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
}
```
Our module should now look like this:
```
godogs
- features
- godogs.feature
- go.mod
- go.sum
- godogs_test.go
```
Run `go test` in the **godogs** directory to run the steps you have defined. You should now see that the scenario runs
with a warning stating there are no tests to run.
```
testing: warning: no tests to run
PASS
ok godogs 0.225s
```
By adding some logic to these steps, you will be able to thoroughly test the feature you just defined.
#### Step 4 - Create the main program to test
Let's keep it simple by only requiring an amount of **godogs** for now.
Create and copy the code below into a new file by running `vim godogs.go`
```go
/* file: $GOPATH/src/godogs/godogs.go */
package main
// Godogs available to eat
@ -161,292 +139,162 @@ var Godogs int
func main() { /* usual main func */ }
```
Our module should now look like this:
```
godogs
- features
- godogs.feature
- go.mod
- go.sum
- godogs.go
- godogs_test.go
```
### Step 4
#### Step 5 - Add some logic to the step definitions
Now lets implement our step definitions, which we can copy from generated
console output snippets in order to test our feature requirements:
Now lets implement our step definitions to test our feature requirements.
Replace the contents of `godogs_test.go` with the code below by running `vim godogs_test.go`.
```go
``` go
/* file: $GOPATH/src/godogs/godogs_test.go */
package main
import (
"context"
"errors"
"fmt"
"testing"
"fmt"
"github.com/cucumber/godog"
"github.com/DATA-DOG/godog"
)
// godogsCtxKey is the key used to store the available godogs in the context.Context.
type godogsCtxKey struct{}
func thereAreGodogs(ctx context.Context, available int) (context.Context, error) {
return context.WithValue(ctx, godogsCtxKey{}, available), nil
func thereAreGodogs(available int) error {
Godogs = available
return nil
}
func iEat(ctx context.Context, num int) (context.Context, error) {
available, ok := ctx.Value(godogsCtxKey{}).(int)
if !ok {
return ctx, errors.New("there are no godogs available")
}
if available < num {
return ctx, fmt.Errorf("you cannot eat %d godogs, there are %d available", num, available)
}
available -= num
return context.WithValue(ctx, godogsCtxKey{}, available), nil
func iEat(num int) error {
if Godogs < num {
return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
}
Godogs -= num
return nil
}
func thereShouldBeRemaining(ctx context.Context, remaining int) error {
available, ok := ctx.Value(godogsCtxKey{}).(int)
if !ok {
return errors.New("there are no godogs available")
}
if available != remaining {
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, available)
}
return nil
func thereShouldBeRemaining(remaining int) error {
if Godogs != remaining {
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
}
return nil
}
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: InitializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
func FeatureContext(s *godog.Suite) {
s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
s.Step(`^I eat (\d+)$`, iEat)
s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func InitializeScenario(sc *godog.ScenarioContext) {
sc.Step(`^there are (\d+) godogs$`, thereAreGodogs)
sc.Step(`^I eat (\d+)$`, iEat)
sc.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
s.BeforeScenario(func(interface{}) {
Godogs = 0 // clean the state before every scenario
})
}
```
In this example, we are using `context.Context` to pass the state between the steps.
Every scenario starts with an empty context and then steps and hooks can add relevant information to it.
Instrumented context is chained through the steps and hooks and is safe to use when multiple scenarios are running concurrently.
Now when you run the `godog` again, you should see:
When you run godog again with `go test -v godogs_test.go`, you should see a passing run:
```
=== RUN TestFeatures
Feature: eat godogs
In order to be happy
As a hungry gopher
I need to be able to eat godogs
=== RUN TestFeatures/Eat_5_out_of_12
![Passed suite](/screenshots/passed.png?raw=true)
Scenario: Eat 5 out of 12 # features/godogs.feature:6
Given there are 12 godogs # godog_test.go:15 -> command-line-arguments.thereAreGodogs
When I eat 5 # godog_test.go:19 -> command-line-arguments.iEat
Then there should be 7 remaining # godog_test.go:34 -> command-line-arguments.thereShouldBeRemaining
We have hooked to **BeforeScenario** event in order to reset application
state before each scenario. You may hook into more events, like
**AfterStep** to print all state in case of an error. Or
**BeforeSuite** to prepare a database.
1 scenarios (1 passed)
3 steps (3 passed)
279.917µs
--- PASS: TestFeatures (0.00s)
--- PASS: TestFeatures/Eat_5_out_of_12 (0.00s)
PASS
ok command-line-arguments 0.164s
```
By now, you should have figured out, how to use **godog**. Another advice
is to make steps orthogonal, small and simple to read for an user. Whether
the user is a dumb website user or an API developer, who may understand
a little more technical context - it should target that user.
You may hook to `ScenarioContext` **Before** event in order to reset or pre-seed the application state before each scenario.
You may hook into more events, like `sc.StepContext()` **After** to print all state in case of an error.
Or **BeforeSuite** to prepare a database.
When steps are orthogonal and small, you can combine them just like you do
with Unix tools. Look how to simplify or remove ones, which can be
composed.
By now, you should have figured out, how to use **godog**. Another piece of advice is to make steps orthogonal, small and simple to read for a user.
Whether the user is a dumb website user or an API developer, who may understand a little more technical context - it should target that user.
### References and Tutorials
When steps are orthogonal and small, you can combine them just like you do with Unix tools. Look how to simplify or remove ones, which can be composed.
`TestFeatures` acts as a regular Go test, so you can leverage your IDE facilities to run and debug it.
### Attachments
An example showing how to make attachments (aka embeddings) to the results is shown in [_examples/attachments](/_examples/attachments/)
## Code of Conduct
Everyone interacting in this codebase and issue tracker is expected to follow the Cucumber [code of conduct](https://github.com/cucumber/cucumber/blob/master/CODE_OF_CONDUCT.md).
## References and Tutorials
- [cucumber-html-reporter](https://github.com/gkushang/cucumber-html-reporter),
may be used in order to generate **html** reports together with **cucumber** output formatter. See the [following docker image](https://github.com/myie/cucumber-html-reporter) for usage details.
- [cucumber-html-reporter](https://github.com/gkushang/cucumber-html-reporter)
may be used in order to generate **html** reports together with
**cucumber** output formatter. See the [following docker
image](https://github.com/myie/cucumber-html-reporter) for usage
details.
- [how to use godog by semaphoreci](https://semaphoreci.com/community/tutorials/how-to-use-godog-for-behavior-driven-development-in-go)
- see [examples](https://github.com/cucumber/godog/tree/master/_examples)
- see extension [AssistDog](https://github.com/hellomd/assistdog),
which may have useful **gherkin.DataTable** transformations or comparison methods for assertions.
- see [examples](https://github.com/DATA-DOG/godog/tree/master/examples)
- see extension [AssistDog](https://github.com/hellomd/assistdog), which
may have useful **gherkin.DataTable** transformations or comparison
methods for assertions.
## Documentation
### Documentation
See [pkg documentation][godoc] for general API details.
See **[Circle Config](/.circleci/config.yml)** for supported **go** versions.
See [godoc][godoc] for general API details.
See **.travis.yml** for supported **go** versions.
See `godog -h` for general command options.
See implementation examples:
- [rest API server](/_examples/api)
- [rest API with Database](/_examples/db)
- [godogs](/_examples/godogs)
- [rest API server](/examples/api)
- [rest API with Database](/examples/db)
- [godogs](/examples/godogs)
## FAQ
### Running Godog with go test
You may integrate running **godog** in your **go test** command.
You may integrate running **godog** in your **go test** command. You can
run it using go [TestMain](https://golang.org/pkg/testing/#hdr-Main) func
available since **go 1.4**. In this case it is not necessary to have
**godog** command installed. See the following examples.
#### Subtests of *testing.T
The following example binds **godog** flags with specified prefix `godog`
in order to prevent flag collisions.
You can run test suite using go [Subtests](https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks).
In this case it is not necessary to have **godog** command installed. See the following example.
```go
package main_test
import (
"testing"
"github.com/cucumber/godog"
)
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: func(s *godog.ScenarioContext) {
// Add step definitions here.
},
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
```
Then you can run suite.
```
go test -test.v -test.run ^TestFeatures$
```
Or a particular scenario.
```
go test -test.v -test.run ^TestFeatures$/^my_scenario$
```
#### TestMain
You can run test suite using go [TestMain](https://golang.org/pkg/testing/#hdr-Main) func available since **go 1.4**.
In this case it is not necessary to have **godog** command installed. See the following examples.
The following example binds **godog** flags with specified prefix `godog` in order to prevent flag collisions.
```go
package main
import (
"os"
"testing"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
"github.com/spf13/pflag" // godog v0.11.0 and later
)
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
}
``` go
var opt = godog.Options{Output: colors.Colored(os.Stdout)}
func init() {
godog.BindFlags("godog.", pflag.CommandLine, &opts) // godog v0.10.0 and earlier
godog.BindCommandLineFlags("godog.", &opts) // godog v0.11.0 and later
godog.BindFlags("godog.", flag.CommandLine, &opt)
}
func TestMain(m *testing.M) {
pflag.Parse()
opts.Paths = pflag.Args()
flag.Parse()
opt.Paths = flag.Args()
status := godog.TestSuite{
Name: "godogs",
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
Options: &opts,
}.Run()
status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
// Optional: Run `testing` package's logic besides godog.
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
```
Then you may run tests with by specifying flags in order to filter features.
Then you may run tests with by specifying flags in order to filter
features.
```
go test -v --godog.random --godog.tags=wip
go test -v --godog.format=progress --godog.random --godog.tags=wip
go test -v --godog.format=pretty --godog.random -race -coverprofile=coverage.txt -covermode=atomic
```
The following example does not bind godog flags, instead manually configuring needed options.
The following example does not bind godog flags, instead manually
configuring needed options.
```go
``` go
func TestMain(m *testing.M) {
opts := godog.Options{
status := godog.RunWithOptions("godog", func(s *godog.Suite) {
FeatureContext(s)
}, godog.Options{
Format: "progress",
Paths: []string{"features"},
Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order
}
})
status := godog.TestSuite{
Name: "godogs",
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
Options: &opts,
}.Run()
// Optional: Run `testing` package's logic besides godog.
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
```
You can even go one step further and reuse **go test** flags, like **verbose** mode in order to switch godog **format**. See the following example:
You can even go one step further and reuse **go test** flags, like
**verbose** mode in order to switch godog **format**. See the following
example:
```go
``` go
func TestMain(m *testing.M) {
format := "progress"
for _, arg := range os.Args[1:] {
@ -455,128 +303,75 @@ func TestMain(m *testing.M) {
break
}
}
opts := godog.Options{
status := godog.RunWithOptions("godog", func(s *godog.Suite) {
godog.SuiteContext(s)
}, godog.Options{
Format: format,
Paths: []string{"features"},
}
})
status := godog.TestSuite{
Name: "godogs",
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
Options: &opts,
}.Run()
// Optional: Run `testing` package's logic besides godog.
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
```
Now when running `go test -v` it will use **pretty** format.
### Tags
If you want to filter scenarios by tags, you can use the `-t=<expression>` or `--tags=<expression>` where `<expression>` is one of the following:
- `@wip` - run all scenarios with wip tag
- `~@wip` - exclude all scenarios with wip tag
- `@wip && ~@new` - run wip scenarios, but exclude new
- `@wip,@undone` - run wip or undone scenarios
### Using assertion packages like testify with Godog
A more extensive example can be [found here](/_examples/assert-godogs).
```go
func thereShouldBeRemaining(ctx context.Context, remaining int) error {
assert.Equal(
godog.T(ctx), Godogs, remaining,
"Expected %d godogs to be remaining, but there is %d", remaining, Godogs,
)
return nil
}
```
### Embeds
If you're looking to compile your test binary in advance of running, you can compile the feature files into the binary via `go:embed`:
```go
//go:embed features/*
var features embed.FS
var opts = godog.Options{
Paths: []string{"features"},
FS: features,
}
```
Now, the test binary can be compiled with all feature files embedded, and can be ran independently from the feature files:
```sh
> go test -c ./test/integration/integration_test.go
> mv integration.test /some/random/dir
> cd /some/random/dir
> ./integration.test
```
**NOTE:** `godog.Options.FS` is as `fs.FS`, so custom filesystem loaders can be used.
## CLI Mode
**NOTE:** The [`godog` CLI has been deprecated](https://github.com/cucumber/godog/discussions/478). It is recommended to use `go test` instead.
Another way to use `godog` is to run it in CLI mode.
In this mode `godog` CLI will use `go` under the hood to compile and run your test suite.
**Godog** does not intervene with the standard **go test** command behavior. You can leverage both frameworks to functionally test your application while maintaining all test related source code in **_test.go** files.
**Godog** acts similar compared to **go test** command, by using go compiler and linker tool in order to produce test executable. Godog contexts need to be exported the same way as **Test** functions for go tests. Note, that if you use **godog** command tool, it will use `go` executable to determine compiler and linker.
### Install
```
go install github.com/cucumber/godog/cmd/godog@latest
```
Adding `@v0.12.0` will install v0.12.0 specifically instead of master.
With `go` version prior to 1.17, use `go get github.com/cucumber/godog/cmd/godog@v0.12.0`.
Running `within the $GOPATH`, you would also need to set `GO111MODULE=on`, like this:
```
GO111MODULE=on go get github.com/cucumber/godog/cmd/godog@v0.12.0
```
### Configure common options for godog CLI
There are no global options or configuration files. Alias your common or project based commands: `alias godog-wip="godog --format=progress --tags=@wip"`
There are no global options or configuration files. Alias your common or
project based commands: `alias godog-wip="godog --format=progress
--tags=@wip"`
## Concurrency
### Testing browser interactions
When concurrency is configured in options, godog will execute the scenarios concurrently, which is supported by all supplied formatters.
**godog** does not come with builtin packages to connect to the browser.
You may want to look at [selenium](http://www.seleniumhq.org/) and
probably [phantomjs](http://phantomjs.org/). See also the following
components:
In order to support concurrency well, you should reset the state and isolate each scenario. They should not share any state. It is suggested to run the suite concurrently in order to make sure there is no state corruption or race conditions in the application.
1. [browsersteps](https://github.com/llonchj/browsersteps) - provides
basic context steps to start selenium and navigate browser content.
2. You may wish to have [goquery](https://github.com/PuerkitoBio/goquery)
in order to work with HTML responses like with JQuery.
It is also useful to randomize the order of scenario execution, which you can now do with `--random` command option or `godog.Options.Randomize` setting.
### Concurrency
### Building your own custom formatter
A simple example can be [found here](/_examples/custom-formatter).
In order to support concurrency well, you should reset the state and
isolate each scenario. They should not share any state. It is suggested to
run the suite concurrently in order to make sure there is no state
corruption or race conditions in the application.
It is also useful to randomize the order of scenario execution, which you
can now do with **--random** command option.
**NOTE:** if suite runs with concurrency option, it concurrently runs
every feature, not scenario per different features. This gives
a flexibility to isolate state per feature. For example using
**BeforeFeature** hook, it is possible to spin up costly service and shut
it down only in **AfterFeature** hook and share the service between all
scenarios in that feature. It is not advisable though, because you are
risking having a state dependency.
## Contributions
Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) -
please open an issue before to discuss whether these changes can be accepted. All backward incompatible changes are
and will be treated cautiously.
## License
**Godog** and **Gherkin** are licensed under the [MIT][license] and developed as a part of the [cucumber project][cucumber]
[godoc]: https://pkg.go.dev/github.com/cucumber/godog "Documentation on godog"
**Godog** is licensed under the [three clause BSD license][license]
**Gherkin** is licensed under the [MIT][gherkin-license] and developed as
a part of the [cucumber project][cucumber]
[godoc]: http://godoc.org/github.com/DATA-DOG/godog "Documentation on godoc"
[golang]: https://golang.org/ "GO programming language"
[behat]: http://docs.behat.org/ "Behavior driven development framework for PHP"
[cucumber]: https://cucumber.io/ "Behavior driven development framework"
[license]: https://en.wikipedia.org/wiki/MIT_License "The MIT license"
[contributing guide]: https://github.com/cucumber/godog/blob/main/CONTRIBUTING.md
[releasing guide]: https://github.com/cucumber/godog/blob/main/RELEASING.md
[community Discord]: https://cucumber.io/community#discord
[gherkin]: https://github.com/cucumber/gherkin-go "Gherkin3 parser for GO"
[gherkin-license]: https://en.wikipedia.org/wiki/MIT_License "The MIT license"
[license]: http://en.wikipedia.org/wiki/BSD_licenses "The three clause BSD license"

Просмотреть файл

@ -1,67 +0,0 @@
# Releasing Guidelines for Cucumber Godog
This document provides guidelines for releasing new versions of Cucumber Godog. Follow these steps to ensure a smooth and consistent release process.
## Versioning
Cucumber Godog follows [Semantic Versioning]. Version numbers are in the format `MAJOR.MINOR.PATCH`.
### Current (for v0.MINOR.PATCH)
- **MINOR**: Incompatible API changes.
- **PATCH**: Backward-compatible new features and bug fixes.
### After v1.X.X release
- **MAJOR**: Incompatible API changes.
- **MINOR**: Backward-compatible new features.
- **PATCH**: Backward-compatible bug fixes.
## Release Process
1. **Update Changelog:**
- Open `CHANGELOG.md` and add an entry for the upcoming release formatting according to the principles of [Keep A CHANGELOG].
- Include details about new features, enhancements, and bug fixes.
2. **Run Tests:**
- Run the test suite to ensure all existing features are working as expected.
3. **Manual Testing for Backwards Compatibility:**
- Manually test the new release with external libraries that depend on Cucumber Godog.
- Look for any potential backwards compatibility issues, especially with widely-used libraries.
- Address any identified issues before proceeding.
4. **Create Release on GitHub:**
- Go to the [Releases] page on GitHub.
- Click on "Draft a new release."
- Tag version should be set to the new tag vMAJOR.MINOR.PATCH
- Title the release using the version number (e.g., "vMAJOR.MINOR.PATCH").
- Click 'Generate release notes'
5. **Publish Release:**
- Click "Publish release" to make the release public.
6. **Announce the Release:**
- Make an announcement on relevant communication channels (e.g., [community Discord]) about the new release.
## Additional Considerations
- **Documentation:**
- Update the project documentation on the [website], if applicable.
- **Deprecation Notices:**
- If any features are deprecated, clearly document them in the release notes and provide guidance on migration.
- **Compatibility:**
- Clearly state any compatibility requirements or changes in the release notes.
- **Feedback:**
- Encourage users to provide feedback and report any issues with the new release.
Following these guidelines, including manual testing with external libraries, will help ensure a thorough release process for Cucumber Godog, allowing detection and resolution of potential backwards compatibility issues before tagging the release.
[community Discord]: https://cucumber.io/community#discord
[website]: https://cucumber.github.io/godog/
[Releases]: https://github.com/cucumber/godog/releases
[Semantic Versioning]: http://semver.org
[Keep A CHANGELOG]: http://keepachangelog.com

Просмотреть файл

@ -1,100 +0,0 @@
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"git.golang1.ru/softonik/godog"
)
type apiFeature struct {
resp *httptest.ResponseRecorder
}
func (a *apiFeature) resetResponse(*godog.Scenario) {
a.resp = httptest.NewRecorder()
}
func (a *apiFeature) iSendrequestTo(method, endpoint string) (err error) {
req, err := http.NewRequest(method, endpoint, nil)
if err != nil {
return
}
// handle panic
defer func() {
switch t := recover().(type) {
case string:
err = fmt.Errorf(t)
case error:
err = t
}
}()
switch endpoint {
case "/version":
getVersion(a.resp, req)
default:
err = fmt.Errorf("unknown endpoint: %s", endpoint)
}
return
}
func (a *apiFeature) theResponseCodeShouldBe(code int) error {
if code != a.resp.Code {
return fmt.Errorf("expected response code to be: %d, but actual is: %d", code, a.resp.Code)
}
return nil
}
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err error) {
var expected, actual interface{}
// re-encode expected response
if err = json.Unmarshal([]byte(body.Content), &expected); err != nil {
return
}
// re-encode actual response too
if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil {
return
}
// the matching may be adapted per different requirements.
if !reflect.DeepEqual(expected, actual) {
return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual)
}
return nil
}
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: InitializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func InitializeScenario(ctx *godog.ScenarioContext) {
api := &apiFeature{}
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
api.resetResponse(sc)
return ctx, nil
})
ctx.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
ctx.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
ctx.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
}

Двоичные данные
_examples/api/screenshots/passed.png

Двоичный файл не отображается.

До

Ширина:  |  Высота:  |  Размер: 39 КиБ

Двоичные данные
_examples/api/screenshots/undefined.png

Двоичный файл не отображается.

До

Ширина:  |  Высота:  |  Размер: 58 КиБ

Просмотреть файл

@ -1,66 +0,0 @@
package main
import (
"context"
"os"
"testing"
"git.golang1.ru/softonik/godog"
"git.golang1.ru/softonik/godog/colors"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
var opts = godog.Options{Output: colors.Colored(os.Stdout)}
func init() {
godog.BindCommandLineFlags("godog.", &opts)
}
func TestMain(m *testing.M) {
pflag.Parse()
opts.Paths = pflag.Args()
status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: InitializeScenario,
Options: &opts,
}.Run()
os.Exit(status)
}
func thereAreGodogs(available int) error {
Godogs = available
return nil
}
func iEat(ctx context.Context, num int) error {
if !assert.GreaterOrEqual(godog.T(ctx), Godogs, num, "You cannot eat %d godogs, there are %d available", num, Godogs) {
return nil
}
Godogs -= num
return nil
}
func thereShouldBeRemaining(ctx context.Context, remaining int) error {
assert.Equal(godog.T(ctx), Godogs, remaining, "Expected %d godogs to be remaining, but there is %d", remaining, Godogs)
return nil
}
func thereShouldBeNoneRemaining(ctx context.Context) error {
assert.Empty(godog.T(ctx), Godogs, "Expected none godogs to be remaining, but there is %d", Godogs)
return nil
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
Godogs = 0 // clean the state before every scenario
return ctx, nil
})
ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs)
ctx.Step(`^I eat (\d+)$`, iEat)
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
ctx.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining)
}

Просмотреть файл

@ -1,16 +0,0 @@
# An example of Making attachments to the reports
The JSON (and in future NDJSON) report formats allow the inclusion of data attachments.
These attachments could be console logs or file data or images for instance.
The example in this directory shows how the godog API is used to add attachments to the JSON report.
## Run the example
You must use the '-v' flag or you will not see the cucumber JSON output.
go test -v attachments_test.go

Просмотреть файл

@ -1,89 +0,0 @@
package attachments_test
// This "demo" doesn't actually get run as a test by the build.
// This "example" shows how to attach data to the cucumber reports
// Run the sample with : go test -v attachments_test.go
// Then review the "embeddings" within the JSON emitted on the console.
import (
"context"
"os"
"testing"
"git.golang1.ru/softonik/godog"
"git.golang1.ru/softonik/godog/colors"
)
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "cucumber", // cucumber json format
}
func TestFeatures(t *testing.T) {
o := opts
o.TestingT = t
status := godog.TestSuite{
Name: "attachments",
Options: &o,
ScenarioInitializer: InitializeScenario,
}.Run()
if status == 2 {
t.SkipNow()
}
if status != 0 {
t.Fatalf("zero status code expected, %d received", status)
}
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("BeforeScenarioAttachment"), FileName: "Step Attachment 1", MediaType: "text/plain"},
)
return ctx, nil
})
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("AfterScenarioAttachment"), FileName: "Step Attachment 2", MediaType: "text/plain"},
)
return ctx, nil
})
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Step Attachment 3", MediaType: "text/plain"},
)
return ctx, nil
})
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Step Attachment 4", MediaType: "text/plain"},
)
return ctx, nil
})
ctx.Step(`^I have attached two documents in sequence$`, func(ctx context.Context) (context.Context, error) {
// the attached bytes will be base64 encoded by the framework and placed in the embeddings section of the cuke report
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("TheData1"), FileName: "Step Attachment 5", MediaType: "text/plain"},
)
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("{ \"a\" : 1 }"), FileName: "Step Attachment 6", MediaType: "application/json"},
)
return ctx, nil
})
ctx.Step(`^I have attached two documents at once$`, func(ctx context.Context) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("TheData1"), FileName: "Step Attachment 7", MediaType: "text/plain"},
godog.Attachment{Body: []byte("TheData2"), FileName: "Step Attachment 8", MediaType: "text/plain"},
)
return ctx, nil
})
}

Просмотреть файл

@ -1,7 +0,0 @@
Feature: Attaching content to the cucumber report
The cucumber JSON and NDJSON support the inclusion of attachments.
These can be text or images or any data really.
Scenario: Attaching files to the report
Given I have attached two documents in sequence
And I have attached two documents at once

Просмотреть файл

@ -1,19 +0,0 @@
# Custom Formatter Example
This example custom formatter demonstrates some ways to build and use custom formatters with godog
## Emoji Progress
The first example is the Emoji formatter, built on top of the Progress formatter that is included with godog.
To run it:
```
$ godog -f emoji
```
Which would output step progress as emojis rather than text:
![](imgs/emoji-output-example.png)

Просмотреть файл

@ -1,122 +0,0 @@
package main
import (
"fmt"
"io"
"math"
"git.golang1.ru/softonik/godog"
)
const (
passedEmoji = "✅"
skippedEmoji = ""
failedEmoji = "❌"
undefinedEmoji = "❓"
pendingEmoji = "🚧"
)
func init() {
godog.Format("emoji", "Progress formatter with emojis", emojiFormatterFunc)
}
func emojiFormatterFunc(suite string, out io.Writer) godog.Formatter {
return newEmojiFmt(suite, out)
}
func newEmojiFmt(suite string, out io.Writer) *emojiFmt {
return &emojiFmt{
ProgressFmt: godog.NewProgressFmt(suite, out),
out: out,
}
}
type emojiFmt struct {
*godog.ProgressFmt
out io.Writer
}
func (f *emojiFmt) TestRunStarted() {}
func (f *emojiFmt) Passed(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) {
f.ProgressFmt.Base.Passed(scenario, step, match)
f.ProgressFmt.Base.Lock.Lock()
defer f.ProgressFmt.Base.Lock.Unlock()
f.step(step.Id)
}
func (f *emojiFmt) Skipped(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) {
f.ProgressFmt.Base.Skipped(scenario, step, match)
f.ProgressFmt.Base.Lock.Lock()
defer f.ProgressFmt.Base.Lock.Unlock()
f.step(step.Id)
}
func (f *emojiFmt) Undefined(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) {
f.ProgressFmt.Base.Undefined(scenario, step, match)
f.ProgressFmt.Base.Lock.Lock()
defer f.ProgressFmt.Base.Lock.Unlock()
f.step(step.Id)
}
func (f *emojiFmt) Failed(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition, err error) {
f.ProgressFmt.Base.Failed(scenario, step, match, err)
f.ProgressFmt.Base.Lock.Lock()
defer f.ProgressFmt.Base.Lock.Unlock()
f.step(step.Id)
}
func (f *emojiFmt) Pending(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) {
f.ProgressFmt.Base.Pending(scenario, step, match)
f.ProgressFmt.Base.Lock.Lock()
defer f.ProgressFmt.Base.Lock.Unlock()
f.step(step.Id)
}
func (f *emojiFmt) Summary() {
f.printSummaryLegend()
f.ProgressFmt.Summary()
}
func (f *emojiFmt) printSummaryLegend() {
fmt.Fprint(f.out, "\n\nOutput Legend:\n")
fmt.Fprint(f.out, fmt.Sprintf("\t%s Passed\n", passedEmoji))
fmt.Fprint(f.out, fmt.Sprintf("\t%s Failed\n", failedEmoji))
fmt.Fprint(f.out, fmt.Sprintf("\t%s Skipped\n", skippedEmoji))
fmt.Fprint(f.out, fmt.Sprintf("\t%s Undefined\n", undefinedEmoji))
fmt.Fprint(f.out, fmt.Sprintf("\t%s Pending\n", pendingEmoji))
}
func (f *emojiFmt) step(pickleStepID string) {
pickleStepResult := f.Storage.MustGetPickleStepResult(pickleStepID)
switch pickleStepResult.Status {
case godog.StepPassed:
fmt.Fprint(f.out, fmt.Sprintf(" %s", passedEmoji))
case godog.StepSkipped:
fmt.Fprint(f.out, fmt.Sprintf(" %s", skippedEmoji))
case godog.StepFailed:
fmt.Fprint(f.out, fmt.Sprintf(" %s", failedEmoji))
case godog.StepUndefined:
fmt.Fprint(f.out, fmt.Sprintf(" %s", undefinedEmoji))
case godog.StepPending:
fmt.Fprint(f.out, fmt.Sprintf(" %s", pendingEmoji))
}
*f.Steps++
if math.Mod(float64(*f.Steps), float64(f.StepsPerRow)) == 0 {
fmt.Fprintf(f.out, " %d\n", *f.Steps)
}
}

Просмотреть файл

@ -1,26 +0,0 @@
# file: $GOPATH/godogs/features/godogs.feature
Feature: Custom emoji formatter examples
In order to be happy
As a hungry gopher
I need to be able to eat godogs
Scenario: Passing test
Given there are 12 godogs
When I eat 5
Then there should be 7 remaining
Scenario: Failing and Skipped test
Given there are 12 godogs
When I eat 5
Then there should be 6 remaining
And there should be 4 remaining
Scenario: Undefined steps
Given there are 12 godogs
When I eat 5
Then this step is not defined
Scenario: Pending step
Given there are 12 godogs
When I eat 5
Then this step is pending

Просмотреть файл

@ -1,6 +0,0 @@
package main
// Godogs available to eat
var Godogs int
func main() { /* usual main func */ }

Просмотреть файл

@ -1,78 +0,0 @@
package main
import (
"context"
"fmt"
"os"
"testing"
"git.golang1.ru/softonik/godog"
"git.golang1.ru/softonik/godog/colors"
flag "github.com/spf13/pflag"
)
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "emoji",
}
func init() {
godog.BindCommandLineFlags("godog.", &opts)
}
func TestMain(m *testing.M) {
flag.Parse()
opts.Paths = flag.Args()
status := godog.TestSuite{
Name: "godogs",
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
Options: &opts,
}.Run()
// This example test is expected to fail to showcase custom formatting, suppressing status.
if status != 1 {
os.Exit(1)
}
}
func thereAreGodogs(available int) error {
Godogs = available
return nil
}
func iEat(num int) error {
if Godogs < num {
return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
}
Godogs -= num
return nil
}
func thereShouldBeRemaining(remaining int) error {
if Godogs != remaining {
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
}
return nil
}
func thisStepIsPending() error {
return godog.ErrPending
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() { Godogs = 0 })
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
Godogs = 0 // clean the state before every scenario
return ctx, nil
})
ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs)
ctx.Step(`^I eat (\d+)$`, iEat)
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
ctx.Step(`^this step is pending$`, thisStepIsPending)
}

Двоичный файл не отображается.

До

Ширина:  |  Высота:  |  Размер: 87 КиБ

Просмотреть файл

@ -1,18 +0,0 @@
module git.golang1.ru/softonik/godog/_examples
go 1.16
replace git.golang1.ru/softonik/godog => ../
require (
github.com/DATA-DOG/go-txdb v0.1.6
git.golang1.ru/softonik/godog v0.15.0
github.com/go-sql-driver/mysql v1.7.1
github.com/spf13/pflag v1.0.6
github.com/stretchr/testify v1.8.2
)
require (
github.com/kr/pretty v0.3.0 // indirect
github.com/lib/pq v1.10.3 // indirect
)

Просмотреть файл

@ -1,65 +0,0 @@
github.com/DATA-DOG/go-txdb v0.1.6 h1:D1Ob/L79mCW6UCFL6vwM/9TWs/rshZujxTsvy7+gicw=
github.com/DATA-DOG/go-txdb v0.1.6/go.mod h1:DhAhxMXZpUJVGnT+p9IbzJoRKvlArO2pkHjnGX7o0n0=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI=
github.com/cucumber/gherkin/go/v26 v26.2.0/go.mod h1:t2GAPnB8maCT4lkHL99BDCVNzCh1d7dBhCLt150Nr/0=
github.com/cucumber/messages/go/v21 v21.0.1 h1:wzA0LxwjlWQYZd32VTlAVDTkW6inOFmSM+RuOwHZiMI=
github.com/cucumber/messages/go/v21 v21.0.1/go.mod h1:zheH/2HS9JLVFukdrsPWoPdmUtmYQAQPLk7w5vWsk5s=
github.com/cucumber/messages/go/v22 v22.0.0/go.mod h1:aZipXTKc0JnjCsXrJnuZpWhtay93k7Rn3Dee7iyPJjs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI=
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c=
github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Просмотреть файл

@ -1,14 +0,0 @@
Feature: eat godogs
In order to be happy
As a hungry gopher
I need to be able to eat godogs
Scenario: Eat 5 out of 12
Given there are 12 godogs
When I eat 5
Then there should be 7 remaining
Scenario: Eat 12 out of 12
Given there are 12 godogs
When I eat 12
Then there should be none remaining

Просмотреть файл

@ -1,14 +0,0 @@
Feature: do not eat godogs
In order to be fit
As a well-fed gopher
I need to be able to avoid godogs
Scenario: Eat 0 out of 12
Given there are 12 godogs
When I eat 0
Then there should be 12 remaining
Scenario: Eat 0 out of 0
Given there are 0 godogs
When I eat 0
Then there should be none remaining

Просмотреть файл

@ -1,37 +0,0 @@
package godogs
import (
"fmt"
)
// Godogs is an example behavior holder.
type Godogs int
// Add increments Godogs count.
func (g *Godogs) Add(n int) {
*g = *g + Godogs(n)
}
// Eat decrements Godogs count or fails if there is not enough available.
func (g *Godogs) Eat(n int) error {
ng := Godogs(n)
if (g == nil && ng > 0) || ng > *g {
return fmt.Errorf("you cannot eat %d godogs, there are %d available", n, g.Available())
}
if ng > 0 {
*g = *g - ng
}
return nil
}
// Available returns the number of currently available Godogs.
func (g *Godogs) Available() int {
if g == nil {
return 0
}
return int(*g)
}

Просмотреть файл

@ -1,109 +0,0 @@
package godogs_test
// This example shows how to set up test suite runner with Go subtests and godog command line parameters.
// Sample commands:
// * run all scenarios from default directory (features): go test -test.run "^TestFeatures/"
// * run all scenarios and list subtest names: go test -test.v -test.run "^TestFeatures/"
// * run all scenarios from one feature file: go test -test.v -godog.paths features/nodogs.feature -test.run "^TestFeatures/"
// * run all scenarios from multiple feature files: go test -test.v -godog.paths features/nodogs.feature,features/godogs.feature -test.run "^TestFeatures/"
// * run single scenario as a subtest: go test -test.v -test.run "^TestFeatures/Eat_5_out_of_12$"
// * show usage help: go test -godog.help
// * show usage help if there were other test files in directory: go test -godog.help godogs_test.go
// * run scenarios with multiple formatters: go test -test.v -godog.format cucumber:cuc.json,pretty -test.run "^TestFeatures/"
import (
"context"
"flag"
"fmt"
"git.golang1.ru/softonik/godog/_examples/godogs"
"os"
"testing"
"git.golang1.ru/softonik/godog"
"git.golang1.ru/softonik/godog/colors"
)
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Concurrency: 4,
}
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opts)
}
func TestFeatures(t *testing.T) {
o := opts
o.TestingT = t
status := godog.TestSuite{
Name: "godogs",
Options: &o,
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
}.Run()
if status == 2 {
t.SkipNow()
}
if status != 0 {
t.Fatalf("zero status code expected, %d received", status)
}
}
type godogsCtxKey struct{}
func godogsToContext(ctx context.Context, g godogs.Godogs) context.Context {
return context.WithValue(ctx, godogsCtxKey{}, &g)
}
func godogsFromContext(ctx context.Context) *godogs.Godogs {
g, _ := ctx.Value(godogsCtxKey{}).(*godogs.Godogs)
return g
}
// Concurrent execution of scenarios may lead to race conditions on shared resources.
// Use context to maintain data separation and avoid data races.
// Step definition can optionally receive context as a first argument.
func thereAreGodogs(ctx context.Context, available int) {
godogsFromContext(ctx).Add(available)
}
// Step definition can return error, context, context and error, or nothing.
func iEat(ctx context.Context, num int) error {
return godogsFromContext(ctx).Eat(num)
}
func thereShouldBeRemaining(ctx context.Context, remaining int) error {
available := godogsFromContext(ctx).Available()
if available != remaining {
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, available)
}
return nil
}
func thereShouldBeNoneRemaining(ctx context.Context) error {
return thereShouldBeRemaining(ctx, 0)
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() { fmt.Println("Get the party started!") })
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
// Add initial godogs to context.
return godogsToContext(ctx, 0), nil
})
ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs)
ctx.Step(`^I eat (\d+)$`, iEat)
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
ctx.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining)
}

Просмотреть файл

@ -1,6 +0,0 @@
This example is to help reproduce issue [#383](https://git.golang1.ru/softonik/godog/issues/383)
To run the example:
cd _examples/incorrect-project-structure
go run ../../cmd/godog

Просмотреть файл

@ -1,7 +0,0 @@
module incorrect-project-structure
go 1.13
require git.golang1.ru/softonik/godog v0.15.0
replace git.golang1.ru/softonik/godog => ../../

Просмотреть файл

@ -1,71 +0,0 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cucumber/gherkin/go/v26 v26.0.2 h1:DjNKtTIv5VG0F1XaJ2xYNk+ck8pJWRNFzyajkc/Y4l4=
github.com/cucumber/gherkin/go/v26 v26.0.2/go.mod h1:Xf+SrSuFbivEDZvmHjTShord3zlEkqsj7QB4sxl1SuU=
github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI=
github.com/cucumber/gherkin/go/v26 v26.2.0/go.mod h1:t2GAPnB8maCT4lkHL99BDCVNzCh1d7dBhCLt150Nr/0=
github.com/cucumber/messages/go/v21 v21.0.1 h1:wzA0LxwjlWQYZd32VTlAVDTkW6inOFmSM+RuOwHZiMI=
github.com/cucumber/messages/go/v21 v21.0.1/go.mod h1:zheH/2HS9JLVFukdrsPWoPdmUtmYQAQPLk7w5vWsk5s=
github.com/cucumber/messages/go/v22 v22.0.0/go.mod h1:aZipXTKc0JnjCsXrJnuZpWhtay93k7Rn3Dee7iyPJjs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI=
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-memdb v1.3.2 h1:RBKHOsnSszpU6vxq80LzC2BaQjuuvoyaQbkLTf7V7g8=
github.com/hashicorp/go-memdb v1.3.2/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g=
github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c=
github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Просмотреть файл

@ -1,7 +0,0 @@
package main
import "git.golang1.ru/softonik/godog"
func InitializeScenario(ctx *godog.ScenarioContext) {
}

Просмотреть файл

@ -1,8 +1,8 @@
package builder
package godog
import "go/ast"
func astContexts(f *ast.File, selectName string) []string {
func astContexts(f *ast.File) []string {
var contexts []string
for _, d := range f.Decls {
switch fun := d.(type) {
@ -12,13 +12,13 @@ func astContexts(f *ast.File, selectName string) []string {
case *ast.StarExpr:
switch x := expr.X.(type) {
case *ast.Ident:
if x.Name == selectName {
if x.Name == "Suite" {
contexts = append(contexts, fun.Name.Name)
}
case *ast.SelectorExpr:
switch t := x.X.(type) {
case *ast.Ident:
if t.Name == "godog" && x.Sel.Name == selectName {
if t.Name == "godog" && x.Sel.Name == "Suite" {
contexts = append(contexts, fun.Name.Name)
}
}

Просмотреть файл

@ -1,4 +1,4 @@
package builder
package godog
import (
"go/parser"
@ -9,7 +9,7 @@ import (
var astContextSrc = `package main
import (
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
)
func MyContext(s *godog.Suite) {
@ -18,7 +18,7 @@ func MyContext(s *godog.Suite) {
var astTwoContextSrc = `package lib
import (
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
)
func ApiContext(s *godog.Suite) {
@ -34,7 +34,7 @@ func astContextParse(src string, t *testing.T) []string {
t.Fatalf("unexpected error while parsing ast: %v", err)
}
return astContexts(f, "Suite")
return astContexts(f)
}
func TestShouldGetSingleContextFromSource(t *testing.T) {

Просмотреть файл

@ -1,28 +0,0 @@
package godog
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAttach(t *testing.T) {
ctx := context.Background()
ctx = Attach(ctx, Attachment{Body: []byte("body1"), FileName: "fileName1", MediaType: "mediaType1"})
ctx = Attach(ctx, Attachment{Body: []byte("body2"), FileName: "fileName2", MediaType: "mediaType2"})
attachments := Attachments(ctx)
assert.Equal(t, 2, len(attachments))
assert.Equal(t, []byte("body1"), attachments[0].Body)
assert.Equal(t, "fileName1", attachments[0].FileName)
assert.Equal(t, "mediaType1", attachments[0].MediaType)
assert.Equal(t, []byte("body2"), attachments[1].Body)
assert.Equal(t, "fileName2", attachments[1].FileName)
assert.Equal(t, "mediaType2", attachments[1].MediaType)
}

337
builder.go Обычный файл
Просмотреть файл

@ -0,0 +1,337 @@
// +build !go1.10
package godog
import (
"bytes"
"fmt"
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"text/template"
"time"
"unicode"
)
var tooldir = findToolDir()
var compiler = filepath.Join(tooldir, "compile")
var linker = filepath.Join(tooldir, "link")
var gopaths = filepath.SplitList(build.Default.GOPATH)
var goarch = build.Default.GOARCH
var goos = build.Default.GOOS
var godogImportPath = "github.com/DATA-DOG/godog"
var runnerTemplate = template.Must(template.New("testmain").Parse(`package main
import (
"github.com/DATA-DOG/godog"
{{if .Contexts}}_test "{{.ImportPath}}"{{end}}
"os"
)
func main() {
status := godog.Run("{{ .Name }}", func (suite *godog.Suite) {
os.Setenv("GODOG_TESTED_PACKAGE", "{{.ImportPath}}")
{{range .Contexts}}
_test.{{ . }}(suite)
{{end}}
})
os.Exit(status)
}`))
// Build creates a test package like go test command at given target path.
// If there are no go files in tested directory, then
// it simply builds a godog executable to scan features.
//
// If there are go test files, it first builds a test
// package with standard go test command.
//
// Finally it generates godog suite executable which
// registers exported godog contexts from the test files
// of tested package.
//
// Returns the path to generated executable
func Build(bin string) error {
abs, err := filepath.Abs(".")
if err != nil {
return err
}
// we allow package to be nil, if godog is run only when
// there is a feature file in empty directory
pkg := importPackage(abs)
src, anyContexts, err := buildTestMain(pkg)
if err != nil {
return err
}
workdir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
testdir := workdir
// if none of test files exist, or there are no contexts found
// we will skip test package compilation, since it is useless
if anyContexts {
// first of all compile test package dependencies
// that will save was many compilations for dependencies
// go does it better
out, err := exec.Command("go", "test", "-i").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to compile package: %s, reason: %v, output: %s", pkg.Name, err, string(out))
}
// let go do the dirty work and compile test
// package with it's dependencies. Older go
// versions does not accept existing file output
// so we create a temporary executable which will
// removed.
temp := fmt.Sprintf(filepath.Join("%s", "temp-%d.test"), os.TempDir(), time.Now().UnixNano())
// builds and compile the tested package.
// generated test executable will be removed
// since we do not need it for godog suite.
// we also print back the temp WORK directory
// go has built. We will reuse it for our suite workdir.
out, err = exec.Command("go", "test", "-c", "-work", "-o", temp).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to compile tested package: %s, reason: %v, output: %s", pkg.Name, err, string(out))
}
defer os.Remove(temp)
// extract go-build temporary directory as our workdir
workdir = strings.TrimSpace(string(out))
if !strings.HasPrefix(workdir, "WORK=") {
return fmt.Errorf("expected WORK dir path, but got: %s", workdir)
}
workdir = strings.Replace(workdir, "WORK=", "", 1)
testdir = filepath.Join(workdir, pkg.ImportPath, "_test")
} else {
// still need to create temporary workdir
if err = os.MkdirAll(testdir, 0755); err != nil {
return err
}
}
defer os.RemoveAll(workdir)
// replace _testmain.go file with our own
testmain := filepath.Join(testdir, "_testmain.go")
err = ioutil.WriteFile(testmain, src, 0644)
if err != nil {
return err
}
// godog library may not be imported in tested package
// but we need it for our testmain package.
// So we look it up in available source paths
// including vendor directory, supported since 1.5.
try := maybeVendorPaths(abs)
for _, d := range build.Default.SrcDirs() {
try = append(try, filepath.Join(d, godogImportPath))
}
godogPkg, err := locatePackage(try)
if err != nil {
return err
}
// make sure godog package archive is installed, gherkin
// will be installed as dependency of godog
cmd := exec.Command("go", "install", godogPkg.ImportPath)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to install godog package: %s, reason: %v", string(out), err)
}
// collect all possible package dirs, will be
// used for includes and linker
pkgDirs := []string{workdir, testdir}
for _, gopath := range gopaths {
pkgDirs = append(pkgDirs, filepath.Join(gopath, "pkg", goos+"_"+goarch))
}
pkgDirs = uniqStringList(pkgDirs)
// compile godog testmain package archive
// we do not depend on CGO so a lot of checks are not necessary
testMainPkgOut := filepath.Join(testdir, "main.a")
args := []string{
"-o", testMainPkgOut,
// "-trimpath", workdir,
"-p", "main",
"-complete",
}
// if godog library is in vendor directory
// link it with import map
if i := strings.LastIndex(godogPkg.ImportPath, "vendor/"); i != -1 {
args = append(args, "-importmap", godogImportPath+"="+godogPkg.ImportPath)
}
for _, inc := range pkgDirs {
args = append(args, "-I", inc)
}
args = append(args, "-pack", testmain)
cmd = exec.Command(compiler, args...)
cmd.Env = os.Environ()
out, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to compile testmain package: %v - output: %s", err, string(out))
}
// link test suite executable
args = []string{
"-o", bin,
"-buildmode=exe",
}
for _, link := range pkgDirs {
args = append(args, "-L", link)
}
args = append(args, testMainPkgOut)
cmd = exec.Command(linker, args...)
cmd.Env = os.Environ()
out, err = cmd.CombinedOutput()
if err != nil {
msg := `failed to link test executable:
reason: %s
command: %s`
return fmt.Errorf(msg, string(out), linker+" '"+strings.Join(args, "' '")+"'")
}
return nil
}
func locatePackage(try []string) (*build.Package, error) {
for _, p := range try {
abs, err := filepath.Abs(p)
if err != nil {
continue
}
pkg, err := build.ImportDir(abs, 0)
if err != nil {
continue
}
return pkg, nil
}
return nil, fmt.Errorf("failed to find godog package in any of:\n%s", strings.Join(try, "\n"))
}
func importPackage(dir string) *build.Package {
pkg, _ := build.ImportDir(dir, 0)
// normalize import path for local import packages
// taken from go source code
// see: https://github.com/golang/go/blob/go1.7rc5/src/cmd/go/pkg.go#L279
if pkg != nil && pkg.ImportPath == "." {
pkg.ImportPath = path.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
}
return pkg
}
// from go src
func makeImportValid(r rune) rune {
// Should match Go spec, compilers, and ../../go/parser/parser.go:/isValidImport.
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return '_'
}
return r
}
func uniqStringList(strs []string) (unique []string) {
uniq := make(map[string]void, len(strs))
for _, s := range strs {
if _, ok := uniq[s]; !ok {
uniq[s] = void{}
unique = append(unique, s)
}
}
return
}
// buildTestMain if given package is valid
// it scans test files for contexts
// and produces a testmain source code.
func buildTestMain(pkg *build.Package) ([]byte, bool, error) {
var contexts []string
var importPath string
name := "main"
if nil != pkg {
ctxs, err := processPackageTestFiles(
pkg.TestGoFiles,
pkg.XTestGoFiles,
)
if err != nil {
return nil, false, err
}
contexts = ctxs
importPath = pkg.ImportPath
name = pkg.Name
}
data := struct {
Name string
Contexts []string
ImportPath string
}{name, contexts, importPath}
var buf bytes.Buffer
if err := runnerTemplate.Execute(&buf, data); err != nil {
return nil, len(contexts) > 0, err
}
return buf.Bytes(), len(contexts) > 0, nil
}
// maybeVendorPaths determines possible vendor paths
// which goes levels down from given directory
// until it reaches GOPATH source dir
func maybeVendorPaths(dir string) (paths []string) {
for _, gopath := range gopaths {
gopath = filepath.Join(gopath, "src")
for strings.HasPrefix(dir, gopath) && dir != gopath {
paths = append(paths, filepath.Join(dir, "vendor", godogImportPath))
dir = filepath.Dir(dir)
}
}
return
}
// processPackageTestFiles runs through ast of each test
// file pack and looks for godog suite contexts to register
// on run
func processPackageTestFiles(packs ...[]string) ([]string, error) {
var ctxs []string
fset := token.NewFileSet()
for _, pack := range packs {
for _, testFile := range pack {
node, err := parser.ParseFile(fset, testFile, nil, 0)
if err != nil {
return ctxs, err
}
ctxs = append(ctxs, astContexts(node)...)
}
}
var failed []string
for _, ctx := range ctxs {
runes := []rune(ctx)
if unicode.IsLower(runes[0]) {
expected := append([]rune{unicode.ToUpper(runes[0])}, runes[1:]...)
failed = append(failed, fmt.Sprintf("%s - should be: %s", ctx, string(expected)))
}
}
if len(failed) > 0 {
return ctxs, fmt.Errorf("godog contexts must be exported:\n\t%s", strings.Join(failed, "\n\t"))
}
return ctxs, nil
}
func findToolDir() string {
if out, err := exec.Command("go", "env", "GOTOOLDIR").Output(); err != nil {
return filepath.Clean(strings.TrimSpace(string(out)))
}
return filepath.Clean(build.ToolDir)
}

419
builder_go110.go Обычный файл
Просмотреть файл

@ -0,0 +1,419 @@
// +build go1.10
package godog
import (
"bytes"
"fmt"
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"text/template"
"time"
"unicode"
)
var tooldir = findToolDir()
var compiler = filepath.Join(tooldir, "compile")
var linker = filepath.Join(tooldir, "link")
var gopaths = filepath.SplitList(build.Default.GOPATH)
var goarch = build.Default.GOARCH
var goroot = build.Default.GOROOT
var goos = build.Default.GOOS
var godogImportPath = "github.com/DATA-DOG/godog"
var runnerTemplate = template.Must(template.New("testmain").Parse(`package main
import (
"github.com/DATA-DOG/godog"
{{if .Contexts}}_test "{{.ImportPath}}"{{end}}
"os"
)
func main() {
status := godog.Run("{{ .Name }}", func (suite *godog.Suite) {
os.Setenv("GODOG_TESTED_PACKAGE", "{{.ImportPath}}")
{{range .Contexts}}
_test.{{ . }}(suite)
{{end}}
})
os.Exit(status)
}`))
// Build creates a test package like go test command at given target path.
// If there are no go files in tested directory, then
// it simply builds a godog executable to scan features.
//
// If there are go test files, it first builds a test
// package with standard go test command.
//
// Finally it generates godog suite executable which
// registers exported godog contexts from the test files
// of tested package.
//
// Returns the path to generated executable
func Build(bin string) error {
abs, err := filepath.Abs(".")
if err != nil {
return err
}
// we allow package to be nil, if godog is run only when
// there is a feature file in empty directory
pkg := importPackage(abs)
src, anyContexts, err := buildTestMain(pkg)
if err != nil {
return err
}
workdir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
testdir := workdir
// if none of test files exist, or there are no contexts found
// we will skip test package compilation, since it is useless
if anyContexts {
// first of all compile test package dependencies
// that will save us many compilations for dependencies
// go does it better
out, err := exec.Command("go", "test", "-i").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to compile package: %s, reason: %v, output: %s", pkg.Name, err, string(out))
}
// builds and compile the tested package.
// generated test executable will be removed
// since we do not need it for godog suite.
// we also print back the temp WORK directory
// go has built. We will reuse it for our suite workdir.
out, err = exec.Command("go", "test", "-c", "-work", "-o", os.DevNull).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to compile tested package: %s, reason: %v, output: %s", pkg.Name, err, string(out))
}
// extract go-build temporary directory as our workdir
workdir = strings.TrimSpace(string(out))
if !strings.HasPrefix(workdir, "WORK=") {
return fmt.Errorf("expected WORK dir path, but got: %s", workdir)
}
workdir = strings.Replace(workdir, "WORK=", "", 1)
testdir = filepath.Join(workdir, "b001")
} else {
// still need to create temporary workdir
if err = os.MkdirAll(testdir, 0755); err != nil {
return err
}
}
defer os.RemoveAll(workdir)
// replace _testmain.go file with our own
testmain := filepath.Join(testdir, "_testmain.go")
err = ioutil.WriteFile(testmain, src, 0644)
if err != nil {
return err
}
// godog library may not be imported in tested package
// but we need it for our testmain package.
// So we look it up in available source paths
// including vendor directory, supported since 1.5.
godogPkg, err := locatePackage(godogImportPath)
if err != nil {
return err
}
// make sure godog package archive is installed, gherkin
// will be installed as dependency of godog
cmd := exec.Command("go", "install", "-i", godogPkg.ImportPath)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to install godog package: %s, reason: %v", string(out), err)
}
// compile godog testmain package archive
// we do not depend on CGO so a lot of checks are not necessary
testMainPkgOut := filepath.Join(testdir, "main.a")
args := []string{
"-o", testMainPkgOut,
"-p", "main",
"-complete",
}
cfg := filepath.Join(testdir, "importcfg.link")
args = append(args, "-importcfg", cfg)
if _, err := os.Stat(cfg); err != nil {
// there were no go sources in the directory
// so we need to build all dependency tree ourselves
in, err := os.Create(cfg)
if err != nil {
return err
}
fmt.Fprintln(in, "# import config")
deps := make(map[string]string)
if err := dependencies(godogPkg, deps, false); err != nil {
in.Close()
return err
}
for pkgName, pkgObj := range deps {
if i := strings.LastIndex(pkgName, "vendor/"); i != -1 {
name := pkgName[i+7:]
fmt.Fprintf(in, "importmap %s=%s\n", name, pkgName)
}
fmt.Fprintf(in, "packagefile %s=%s\n", pkgName, pkgObj)
}
in.Close()
} else {
// need to make sure that vendor dependencies are mapped
in, err := os.OpenFile(cfg, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
deps := make(map[string]string)
if err := dependencies(pkg, deps, true); err != nil {
in.Close()
return err
}
if err := dependencies(godogPkg, deps, false); err != nil {
in.Close()
return err
}
for pkgName := range deps {
if i := strings.LastIndex(pkgName, "vendor/"); i != -1 {
name := pkgName[i+7:]
fmt.Fprintf(in, "importmap %s=%s\n", name, pkgName)
}
}
in.Close()
}
args = append(args, "-pack", testmain)
cmd = exec.Command(compiler, args...)
cmd.Env = os.Environ()
out, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to compile testmain package: %v - output: %s", err, string(out))
}
// link test suite executable
args = []string{
"-o", bin,
"-importcfg", cfg,
"-buildmode=exe",
}
args = append(args, testMainPkgOut)
cmd = exec.Command(linker, args...)
cmd.Env = os.Environ()
// in case if build is without contexts, need to remove import maps
data, err := ioutil.ReadFile(cfg)
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
var fixed []string
for _, line := range lines {
if strings.Index(line, "importmap") == 0 {
continue
}
fixed = append(fixed, line)
}
if err := ioutil.WriteFile(cfg, []byte(strings.Join(fixed, "\n")), 0600); err != nil {
return err
}
out, err = cmd.CombinedOutput()
if err != nil {
msg := `failed to link test executable:
reason: %s
command: %s`
return fmt.Errorf(msg, string(out), linker+" '"+strings.Join(args, "' '")+"'")
}
return nil
}
func locatePackage(name string) (*build.Package, error) {
// search vendor paths first since that takes priority
dir, err := filepath.Abs(".")
if err != nil {
return nil, err
}
for _, gopath := range gopaths {
gopath = filepath.Join(gopath, "src")
for strings.HasPrefix(dir, gopath) && dir != gopath {
pkg, err := build.ImportDir(filepath.Join(dir, "vendor", name), 0)
if err != nil {
dir = filepath.Dir(dir)
continue
}
return pkg, nil
}
}
// search source paths otherwise
for _, p := range build.Default.SrcDirs() {
abs, err := filepath.Abs(filepath.Join(p, name))
if err != nil {
continue
}
pkg, err := build.ImportDir(abs, 0)
if err != nil {
continue
}
return pkg, nil
}
return nil, fmt.Errorf("failed to find %s package in any of:\n%s", name, strings.Join(build.Default.SrcDirs(), "\n"))
}
func importPackage(dir string) *build.Package {
pkg, _ := build.ImportDir(dir, 0)
// normalize import path for local import packages
// taken from go source code
// see: https://github.com/golang/go/blob/go1.7rc5/src/cmd/go/pkg.go#L279
if pkg != nil && pkg.ImportPath == "." {
pkg.ImportPath = path.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
}
return pkg
}
// from go src
func makeImportValid(r rune) rune {
// Should match Go spec, compilers, and ../../go/parser/parser.go:/isValidImport.
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return '_'
}
return r
}
func uniqStringList(strs []string) (unique []string) {
uniq := make(map[string]void, len(strs))
for _, s := range strs {
if _, ok := uniq[s]; !ok {
uniq[s] = void{}
unique = append(unique, s)
}
}
return
}
// buildTestMain if given package is valid
// it scans test files for contexts
// and produces a testmain source code.
func buildTestMain(pkg *build.Package) ([]byte, bool, error) {
var contexts []string
var importPath string
name := "main"
if nil != pkg {
ctxs, err := processPackageTestFiles(
pkg.TestGoFiles,
pkg.XTestGoFiles,
)
if err != nil {
return nil, false, err
}
contexts = ctxs
// for module support, query the module import path
// @TODO: maybe there is a better way to read it
out, err := exec.Command("go", "list", "-m").CombinedOutput()
if err != nil {
// is not using modules or older go version
importPath = pkg.ImportPath
} else {
// otherwise read the module name from command output
importPath = strings.TrimSpace(string(out))
}
name = pkg.Name
}
data := struct {
Name string
Contexts []string
ImportPath string
}{name, contexts, importPath}
var buf bytes.Buffer
if err := runnerTemplate.Execute(&buf, data); err != nil {
return nil, len(contexts) > 0, err
}
return buf.Bytes(), len(contexts) > 0, nil
}
// processPackageTestFiles runs through ast of each test
// file pack and looks for godog suite contexts to register
// on run
func processPackageTestFiles(packs ...[]string) ([]string, error) {
var ctxs []string
fset := token.NewFileSet()
for _, pack := range packs {
for _, testFile := range pack {
node, err := parser.ParseFile(fset, testFile, nil, 0)
if err != nil {
return ctxs, err
}
ctxs = append(ctxs, astContexts(node)...)
}
}
var failed []string
for _, ctx := range ctxs {
runes := []rune(ctx)
if unicode.IsLower(runes[0]) {
expected := append([]rune{unicode.ToUpper(runes[0])}, runes[1:]...)
failed = append(failed, fmt.Sprintf("%s - should be: %s", ctx, string(expected)))
}
}
if len(failed) > 0 {
return ctxs, fmt.Errorf("godog contexts must be exported:\n\t%s", strings.Join(failed, "\n\t"))
}
return ctxs, nil
}
func findToolDir() string {
if out, err := exec.Command("go", "env", "GOTOOLDIR").Output(); err != nil {
return filepath.Clean(strings.TrimSpace(string(out)))
}
return filepath.Clean(build.ToolDir)
}
func dependencies(pkg *build.Package, visited map[string]string, vendor bool) error {
visited[pkg.ImportPath] = pkg.PkgObj
imports := pkg.Imports
if vendor {
imports = append(imports, pkg.TestImports...)
}
for _, name := range imports {
if i := strings.LastIndex(name, "vendor/"); vendor && i == -1 {
continue // only interested in vendor packages
}
if _, ok := visited[name]; ok {
continue
}
next, err := locatePackage(name)
if err != nil {
return err
}
visited[name] = pkg.PkgObj
if err := dependencies(next, visited, vendor); err != nil {
return err
}
}
return nil
}

37
builder_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,37 @@
package godog
import (
"os"
"path/filepath"
"testing"
)
func TestBuildTestRunner(t *testing.T) {
bin := filepath.Join(os.TempDir(), "godog.test")
if err := Build(bin); err != nil {
t.Fatalf("failed to build godog test binary: %v", err)
}
os.Remove(bin)
}
func TestBuildTestRunnerWithoutGoFiles(t *testing.T) {
bin := filepath.Join(os.TempDir(), "godog.test")
pwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
wd := filepath.Join(pwd, "features")
if err := os.Chdir(wd); err != nil {
t.Fatalf("failed to change working directory: %v", err)
}
defer func() {
os.Chdir(pwd) // get back to current dir
}()
if err := Build(bin); err != nil {
t.Fatalf("failed to build godog test binary: %v", err)
}
os.Remove(bin)
}

Просмотреть файл

@ -1,55 +0,0 @@
package internal
import (
"fmt"
"go/build"
"path/filepath"
"git.golang1.ru/softonik/godog/colors"
"git.golang1.ru/softonik/godog/internal/builder"
"github.com/spf13/cobra"
)
var buildOutput string
var buildOutputDefault = "godog.test"
// CreateBuildCmd creates the build subcommand.
func CreateBuildCmd() cobra.Command {
if build.Default.GOOS == "windows" {
buildOutputDefault += ".exe"
}
buildCmd := cobra.Command{
Use: "build",
Short: "Compiles a test runner",
Long: `Compiles a test runner. Command should be run from the directory of tested
package and contain buildable go source.
The test runner can be executed with the same flags as when using godog run.`,
Example: ` godog build
godog build -o ` + buildOutputDefault,
RunE: buildCmdRunFunc,
}
buildCmd.Flags().StringVarP(&buildOutput, "output", "o", buildOutputDefault, `compiles the test runner to the named file
`)
return buildCmd
}
func buildCmdRunFunc(cmd *cobra.Command, args []string) error {
fmt.Println(colors.Yellow("Use of godog CLI is deprecated, please use *testing.T instead."))
fmt.Println(colors.Yellow("See https://git.golang1.ru/softonik/godog/discussions/478 for details."))
bin, err := filepath.Abs(buildOutput)
if err != nil {
return fmt.Errorf("could not locate absolute path for: %q. reason: %v", buildOutput, err)
}
if err = builder.Build(bin); err != nil {
return fmt.Errorf("could not build binary at: %q. reason: %v", buildOutput, err)
}
return nil
}

Просмотреть файл

@ -1,65 +0,0 @@
package internal
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"git.golang1.ru/softonik/godog/internal/flags"
)
var version bool
var output string
// CreateRootCmd creates the root command.
func CreateRootCmd() cobra.Command {
rootCmd := cobra.Command{
Use: "godog",
Long: `Creates and runs test runner for the given feature files.
Command should be run from the directory of tested package
and contain buildable go source.`,
Args: cobra.ArbitraryArgs,
// Deprecated: Use godog build, godog run or godog version.
// This is to support the legacy direct usage of the root command.
RunE: runRootCmd,
}
bindRootCmdFlags(rootCmd.Flags())
return rootCmd
}
func runRootCmd(cmd *cobra.Command, args []string) error {
if version {
versionCmdRunFunc(cmd, args)
return nil
}
if len(output) > 0 {
buildOutput = output
if err := buildCmdRunFunc(cmd, args); err != nil {
return err
}
}
return runCmdRunFunc(cmd, args)
}
func bindRootCmdFlags(flagSet *pflag.FlagSet) {
flagSet.StringVarP(&output, "output", "o", "", "compiles the test runner to the named file")
flagSet.BoolVar(&version, "version", false, "show current version")
flags.BindRunCmdFlags("", flagSet, &opts)
// Since using the root command directly is deprecated.
// All flags will be hidden
flagSet.MarkHidden("output")
flagSet.MarkHidden("version")
flagSet.MarkHidden("no-colors")
flagSet.MarkHidden("concurrency")
flagSet.MarkHidden("tags")
flagSet.MarkHidden("format")
flagSet.MarkHidden("definitions")
flagSet.MarkHidden("stop-on-failure")
flagSet.MarkHidden("strict")
flagSet.MarkHidden("random")
}

Просмотреть файл

@ -1,111 +0,0 @@
package internal
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/spf13/cobra"
"git.golang1.ru/softonik/godog/colors"
"git.golang1.ru/softonik/godog/internal/builder"
"git.golang1.ru/softonik/godog/internal/flags"
)
var opts flags.Options
// CreateRunCmd creates the run subcommand.
func CreateRunCmd() cobra.Command {
runCmd := cobra.Command{
Use: "run [features]",
Short: "Compiles and runs a test runner",
Long: `Compiles and runs test runner for the given feature files.
Command should be run from the directory of tested package and contain
buildable go source.`,
Example: ` godog run
godog run <feature>
godog run <feature> <feature>
Optional feature(s) to run:
dir (features/)
feature (*.feature)
scenario at specific line (*.feature:10)
If no feature arguments are supplied, godog will use "features/" by default.`,
RunE: runCmdRunFunc,
SilenceUsage: true,
}
flags.BindRunCmdFlags("", runCmd.Flags(), &opts)
return runCmd
}
func runCmdRunFunc(cmd *cobra.Command, args []string) error {
fmt.Println(colors.Yellow("Use of godog CLI is deprecated, please use *testing.T instead."))
fmt.Println(colors.Yellow("See https://git.golang1.ru/softonik/godog/discussions/478 for details."))
osArgs := os.Args[1:]
if len(osArgs) > 0 && osArgs[0] == "run" {
osArgs = osArgs[1:]
}
if err := buildAndRunGodog(osArgs); err != nil {
return err
}
return nil
}
func buildAndRunGodog(args []string) (err error) {
bin, err := filepath.Abs(buildOutputDefault)
if err != nil {
return err
}
if err = builder.Build(bin); err != nil {
return err
}
defer os.Remove(bin)
return runGodog(bin, args)
}
func runGodog(bin string, args []string) (err error) {
cmd := exec.Command(bin, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
if err = cmd.Start(); err != nil {
return err
}
if err = cmd.Wait(); err == nil {
return nil
}
exiterr, ok := err.(*exec.ExitError)
if !ok {
return err
}
st, ok := exiterr.Sys().(syscall.WaitStatus)
if !ok {
return fmt.Errorf("failed to convert error to syscall wait status. original error: %w", exiterr)
}
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if st.ExitStatus() > 0 {
return err
}
return nil
}

Просмотреть файл

@ -1,26 +0,0 @@
package internal
import (
"fmt"
"os"
"github.com/spf13/cobra"
"git.golang1.ru/softonik/godog"
)
// CreateVersionCmd creates the version subcommand.
func CreateVersionCmd() cobra.Command {
versionCmd := cobra.Command{
Use: "version",
Short: "Show current version",
Run: versionCmdRunFunc,
Version: godog.Version,
}
return versionCmd
}
func versionCmdRunFunc(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stdout, "Godog version is:", godog.Version)
}

Просмотреть файл

@ -2,21 +2,125 @@ package main
import (
"fmt"
"go/build"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"syscall"
"git.golang1.ru/softonik/godog/cmd/godog/internal"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/colors"
)
var statusMatch = regexp.MustCompile("^exit status (\\d+)")
var parsedStatus int
func buildAndRun() (int, error) {
var status int
bin, err := filepath.Abs("godog.test")
if err != nil {
return 1, err
}
if build.Default.GOOS == "windows" {
bin += ".exe"
}
if err = godog.Build(bin); err != nil {
return 1, err
}
defer os.Remove(bin)
cmd := exec.Command(bin, os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
if err = cmd.Start(); err != nil {
return status, err
}
if err = cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
status = 1
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if st, ok := exiterr.Sys().(syscall.WaitStatus); ok {
status = st.ExitStatus()
}
return status, nil
}
return status, err
}
return status, nil
}
func main() {
rootCmd := internal.CreateRootCmd()
buildCmd := internal.CreateBuildCmd()
runCmd := internal.CreateRunCmd()
versionCmd := internal.CreateVersionCmd()
var vers bool
var output string
rootCmd.AddCommand(&buildCmd, &runCmd, &versionCmd)
opt := godog.Options{Output: colors.Colored(os.Stdout)}
flagSet := godog.FlagSet(&opt)
flagSet.BoolVar(&vers, "version", false, "Show current version.")
flagSet.StringVar(&output, "o", "", "Build and output test runner executable to given target path.")
flagSet.StringVar(&output, "output", "", "Build and output test runner executable to given target path.")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
if err := flagSet.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(output) > 0 {
bin, err := filepath.Abs(output)
if err != nil {
fmt.Fprintln(os.Stderr, "could not locate absolute path for:", output, err)
os.Exit(1)
}
if err = godog.Build(bin); err != nil {
fmt.Fprintln(os.Stderr, "could not build binary at:", output, err)
os.Exit(1)
}
os.Exit(0)
}
if vers {
fmt.Fprintln(os.Stdout, "Godog version is:", godog.Version)
os.Exit(0) // should it be 0?
}
status, err := buildAndRun()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// it might be a case, that status might not be resolved
// in some OSes. this is attempt to parse it from stderr
if parsedStatus > status {
status = parsedStatus
}
os.Exit(status)
}
func statusOutputFilter(w io.Writer) io.Writer {
return writerFunc(func(b []byte) (int, error) {
if m := statusMatch.FindStringSubmatch(string(b)); len(m) > 1 {
parsedStatus, _ = strconv.Atoi(m[1])
// skip status stderr output
return len(b), nil
}
return w.Write(b)
})
}
type writerFunc func([]byte) (int, error)
func (w writerFunc) Write(b []byte) (int, error) {
return w(b)
}

Просмотреть файл

@ -1,8 +0,0 @@
coverage:
status:
project:
default:
threshold: 0.5%
patch:
default:
threshold: 0.5%

Просмотреть файл

@ -2,7 +2,6 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build !windows
// +build !windows
package colors

Просмотреть файл

@ -2,7 +2,6 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build windows
// +build windows
package colors
@ -410,7 +409,7 @@ func (cw *ansiColorWriter) Write(p []byte) (int, error) {
}
if cw.mode != discardNonColorEscSeq || cw.state == outsideCsiCode {
nw, err = cw.w.Write(p[first:])
nw, err = cw.w.Write(p[first:len(p)])
r += nw
}

Просмотреть файл

@ -16,8 +16,8 @@ const (
red
green
yellow
blue // unused
magenta // unused
blue
magenta
cyan
white
)
@ -26,43 +26,34 @@ func colorize(s interface{}, c color) string {
return fmt.Sprintf("%s[%dm%v%s[0m", ansiEscape, c, s, ansiEscape)
}
// ColorFunc is a helper type to create colorized strings.
type ColorFunc func(interface{}) string
// Bold will accept a ColorFunc and return a new ColorFunc
// that will make the string bold.
func Bold(fn ColorFunc) ColorFunc {
return ColorFunc(func(input interface{}) string {
return strings.Replace(fn(input), ansiEscape+"[", ansiEscape+"[1;", 1)
})
}
// Green will accept an interface and return a colorized green string.
func Green(s interface{}) string {
return colorize(s, green)
}
// Red will accept an interface and return a colorized red string.
func Red(s interface{}) string {
return colorize(s, red)
}
// Cyan will accept an interface and return a colorized cyan string.
func Cyan(s interface{}) string {
return colorize(s, cyan)
}
// Black will accept an interface and return a colorized black string.
func Black(s interface{}) string {
return colorize(s, black)
}
// Yellow will accept an interface and return a colorized yellow string.
func Yellow(s interface{}) string {
return colorize(s, yellow)
}
// White will accept an interface and return a colorized white string.
func White(s interface{}) string {
return colorize(s, white)
}

Просмотреть файл

@ -11,8 +11,6 @@ type noColors struct {
lastbuf bytes.Buffer
}
// Uncolored will accept and io.Writer and return a
// new io.Writer that won't include colors.
func Uncolored(w io.Writer) io.Writer {
return &noColors{out: w}
}

Просмотреть файл

@ -16,7 +16,7 @@ type outputMode int
const (
_ outputMode = iota
discardNonColorEscSeq
outputNonColorEscSeq // unused
outputNonColorEscSeq
)
// Colored creates and initializes a new ansiColorWriter

Просмотреть файл

@ -1,45 +0,0 @@
package godog_test
import (
"testing"
"git.golang1.ru/softonik/godog"
)
func ExampleTestSuite_Run_subtests() {
var t *testing.T // Comes from your test function, e.g. func TestFeatures(t *testing.T).
suite := godog.TestSuite{
ScenarioInitializer: func(s *godog.ScenarioContext) {
// Add step definitions here.
},
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: func(s *godog.ScenarioContext) {
godog.InitializeScenario(s)
// Add step definitions here.
},
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}

Просмотреть файл

@ -4,8 +4,8 @@ The following example demonstrates steps how we describe and test our API using
### Step 1
Describe our feature. Imagine we need a REST API with `json` format. Lets from the point, that
we need to have a `/version` endpoint, which responds with a version number. We also need to manage
Describe our feature. Imagine we need a REST API with **json** format. Lets from the point, that
we need to have a **/version** endpoint, which responds with a version number. We also need to manage
error responses.
``` gherkin
@ -31,24 +31,24 @@ Feature: get version
And the response should match json:
"""
{
"version": "v0.0.0-dev"
"version": "v0.5.3"
}
"""
```
Save it as `features/version.feature`.
Save it as **version.feature**.
Now we have described a success case and an error when the request method is not allowed.
### Step 2
Execute `godog run`. You should see the following result, which says that all of our
Run **godog version.feature**. You should see the following result, which says that all of our
steps are yet undefined and provide us with the snippets to implement them.
![Screenshot](https://raw.git.golang1.ru/softonik/godog/master/_examples/api/screenshots/undefined.png)
![Screenshot](https://raw.github.com/DATA-DOG/godog/master/examples/api/screenshots/undefined.png)
### Step 3
Lets copy the snippets to `api_test.go` and modify it for our use case. Since we know that we will
Lets copy the snippets to **api_test.go** and modify it for our use case. Since we know that we will
need to store state within steps (a response), we should introduce a structure with some variables.
``` go
@ -56,7 +56,8 @@ need to store state within steps (a response), we should introduce a structure w
package main
import (
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
)
type apiFeature struct {
@ -70,26 +71,11 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return godog.ErrPending
}
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) error {
func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) error {
return godog.ErrPending
}
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: InitializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func InitializeScenario(s *godog.ScenarioContext) {
func FeatureContext(s *godog.Suite) {
api := &apiFeature{}
s.Step(`^I send "([^"]*)" request to "([^"]*)"$`, api.iSendrequestTo)
s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
@ -99,29 +85,28 @@ func InitializeScenario(s *godog.ScenarioContext) {
### Step 4
Now we can implement steps, since we know what behavior we expect:
Now we can implemented steps, since we know what behavior we expect:
``` go
// file: api_test.go
package main
import (
"context"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
)
type apiFeature struct {
resp *httptest.ResponseRecorder
}
func (a *apiFeature) resetResponse(*godog.Scenario) {
func (a *apiFeature) resetResponse(interface{}) {
a.resp = httptest.NewRecorder()
}
@ -157,59 +142,37 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return nil
}
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err error) {
var expected, actual interface{}
// re-encode expected response
if err = json.Unmarshal([]byte(body.Content), &expected); err != nil {
func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) {
var expected, actual []byte
var data interface{}
if err = json.Unmarshal([]byte(body.Content), &data); err != nil {
return
}
// re-encode actual response too
if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil {
if expected, err = json.Marshal(data); err != nil {
return
}
// the matching may be adapted per different requirements.
if !reflect.DeepEqual(expected, actual) {
return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual)
actual = a.resp.Body.Bytes()
if !bytes.Equal(actual, expected) {
err = fmt.Errorf("expected json, does not match actual: %s", string(actual))
}
return nil
return
}
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: InitializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t, // Testing instance that will run subtests.
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func InitializeScenario(ctx *godog.ScenarioContext) {
func FeatureContext(s *godog.Suite) {
api := &apiFeature{}
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
api.resetResponse(sc)
return ctx, nil
})
ctx.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
ctx.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
ctx.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
s.BeforeScenario(api.resetResponse)
s.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
s.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
}
```
**NOTE:** the `getVersion` handler is called on `/version` endpoint.
Executing `godog run` or `go test -v` will provide `undefined: getVersion` error, so we actually need to implement it now.
**NOTE:** the `getVersion` handler call on **/version** endpoint. We actually need to implement it now.
If we made some mistakes in step implementations, we will know about it when we run the tests.
Though, we could also improve our `JSON` comparison function to range through the interfaces and
Though, we could also improve our **JSON** comparison function to range through the interfaces and
match their types and values.
In case if some router is used, you may search the handler based on the endpoint. Current example
@ -217,7 +180,7 @@ uses a standard http package.
### Step 5
Finally, lets implement the `API` server:
Finally, lets implement the **api** server:
``` go
// file: api.go
@ -226,17 +189,17 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
)
func getVersion(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
if r.Method != "GET" {
fail(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
data := struct {
Version string `json:"version"`
}{Version: godog.Version}
@ -244,34 +207,42 @@ func getVersion(w http.ResponseWriter, r *http.Request) {
ok(w, data)
}
func main() {
http.HandleFunc("/version", getVersion)
http.ListenAndServe(":8080", nil)
}
// fail writes a json response with error msg and status header
func fail(w http.ResponseWriter, msg string, status int) {
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
data := struct {
Error string `json:"error"`
}{Error: msg}
resp, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
resp, _ := json.Marshal(data)
w.WriteHeader(status)
fmt.Fprintf(w, string(resp))
}
// ok writes data to response with 200 status
func ok(w http.ResponseWriter, data interface{}) {
resp, err := json.Marshal(data)
if err != nil {
fail(w, "Oops something evil has happened", http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
if s, ok := data.(string); ok {
fmt.Fprintf(w, s)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}
resp, err := json.Marshal(data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fail(w, "oops something evil has happened", 500)
return
}
func main() {
http.HandleFunc("/version", getVersion)
http.ListenAndServe(":8080", nil)
fmt.Fprintf(w, string(resp))
}
```
@ -280,9 +251,9 @@ used to respond with the correct constant version number.
### Step 6
Run our tests to see whether everything is happening as we have expected: `go test -v`
Run our tests to see whether everything is happening as we have expected: `godog version.feature`
![Screenshot](https://raw.git.golang1.ru/softonik/godog/master/_examples/api/screenshots/passed.png)
![Screenshot](https://raw.github.com/DATA-DOG/godog/master/examples/api/screenshots/passed.png)
### Conclusions

Просмотреть файл

@ -3,17 +3,17 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
)
func getVersion(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
if r.Method != "GET" {
fail(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
data := struct {
Version string `json:"version"`
}{Version: godog.Version}
@ -21,32 +21,40 @@ func getVersion(w http.ResponseWriter, r *http.Request) {
ok(w, data)
}
// fail writes a json response with error msg and status header
func fail(w http.ResponseWriter, msg string, status int) {
w.WriteHeader(status)
data := struct {
Error string `json:"error"`
}{Error: msg}
resp, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}
// ok writes data to response with 200 status
func ok(w http.ResponseWriter, data interface{}) {
resp, err := json.Marshal(data)
if err != nil {
fail(w, "Oops something evil has happened", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}
func main() {
http.HandleFunc("/version", getVersion)
http.ListenAndServe(":8080", nil)
}
// fail writes a json response with error msg and status header
func fail(w http.ResponseWriter, msg string, status int) {
w.Header().Set("Content-Type", "application/json")
data := struct {
Error string `json:"error"`
}{Error: msg}
resp, _ := json.Marshal(data)
w.WriteHeader(status)
fmt.Fprintf(w, string(resp))
}
// ok writes data to response with 200 status
func ok(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
if s, ok := data.(string); ok {
fmt.Fprintf(w, s)
return
}
resp, err := json.Marshal(data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fail(w, "oops something evil has happened", 500)
return
}
fmt.Fprintf(w, string(resp))
}

102
examples/api/api_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,102 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
)
type apiFeature struct {
resp *httptest.ResponseRecorder
}
func (a *apiFeature) resetResponse(interface{}) {
a.resp = httptest.NewRecorder()
}
func (a *apiFeature) iSendrequestTo(method, endpoint string) (err error) {
req, err := http.NewRequest(method, endpoint, nil)
if err != nil {
return
}
// handle panic
defer func() {
switch t := recover().(type) {
case string:
err = fmt.Errorf(t)
case error:
err = t
}
}()
switch endpoint {
case "/version":
getVersion(a.resp, req)
default:
err = fmt.Errorf("unknown endpoint: %s", endpoint)
}
return
}
func (a *apiFeature) theResponseCodeShouldBe(code int) error {
if code != a.resp.Code {
return fmt.Errorf("expected response code to be: %d, but actual is: %d", code, a.resp.Code)
}
return nil
}
func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) {
var expected, actual []byte
var exp, act interface{}
// re-encode expected response
if err = json.Unmarshal([]byte(body.Content), &exp); err != nil {
return
}
if expected, err = json.MarshalIndent(exp, "", " "); err != nil {
return
}
// re-encode actual response too
if err = json.Unmarshal(a.resp.Body.Bytes(), &act); err != nil {
return
}
if actual, err = json.MarshalIndent(act, "", " "); err != nil {
return
}
// the matching may be adapted per different requirements.
if len(actual) != len(expected) {
return fmt.Errorf(
"expected json length: %d does not match actual: %d:\n%s",
len(expected),
len(actual),
string(actual),
)
}
for i, b := range actual {
if b != expected[i] {
return fmt.Errorf(
"expected JSON does not match actual, showing up to last matched character:\n%s",
string(actual[:i+1]),
)
}
}
return
}
func FeatureContext(s *godog.Suite) {
api := &apiFeature{}
s.BeforeScenario(api.resetResponse)
s.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
s.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
}

Двоичные данные
examples/api/screenshots/passed.png Обычный файл

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 75 КиБ

Двоичные данные
examples/api/screenshots/undefined.png Обычный файл

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 99 КиБ

Просмотреть файл

@ -20,6 +20,6 @@ Feature: get version
And the response should match json:
"""
{
"version": "v0.0.0-dev"
"version": "v0.7.5"
}
"""

Просмотреть файл

Просмотреть файл

@ -1,7 +1,7 @@
# An example of API with DB
The following example demonstrates steps how we describe and test our API with DB using **godog**.
To start with, see [API example](https://git.golang1.ru/softonik/godog/tree/master/_examples/api) before.
To start with, see [API example](https://github.com/DATA-DOG/godog/tree/master/examples/api) before.
We have extended it to be used with database.
The interesting point is, that we have [go-txdb](https://github.com/DATA-DOG/go-txdb) library,

Просмотреть файл

Просмотреть файл

@ -1,17 +1,16 @@
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
txdb "github.com/DATA-DOG/go-txdb"
"git.golang1.ru/softonik/godog"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
)
func init() {
@ -24,7 +23,7 @@ type apiFeature struct {
resp *httptest.ResponseRecorder
}
func (a *apiFeature) resetResponse(*godog.Scenario) {
func (a *apiFeature) resetResponse(interface{}) {
a.resp = httptest.NewRecorder()
if a.db != nil {
a.db.Close()
@ -71,27 +70,23 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return nil
}
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err error) {
var expected, actual interface{}
// re-encode expected response
if err = json.Unmarshal([]byte(body.Content), &expected); err != nil {
func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) {
var expected, actual []byte
var data interface{}
if err = json.Unmarshal([]byte(body.Content), &data); err != nil {
return
}
// re-encode actual response too
if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil {
if expected, err = json.Marshal(data); err != nil {
return
}
// the matching may be adapted per different requirements.
if !reflect.DeepEqual(expected, actual) {
return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual)
actual = a.resp.Body.Bytes()
if string(actual) != string(expected) {
err = fmt.Errorf("expected json %s, does not match actual: %s", string(expected), string(actual))
}
return nil
return
}
func (a *apiFeature) thereAreUsers(users *godog.Table) error {
func (a *apiFeature) thereAreUsers(users *gherkin.DataTable) error {
var fields []string
var marks []string
head := users.Rows[0].Cells
@ -123,16 +118,13 @@ func (a *apiFeature) thereAreUsers(users *godog.Table) error {
return nil
}
func InitializeScenario(ctx *godog.ScenarioContext) {
func FeatureContext(s *godog.Suite) {
api := &apiFeature{}
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
api.resetResponse(sc)
return ctx, nil
})
s.BeforeScenario(api.resetResponse)
ctx.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
ctx.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
ctx.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
ctx.Step(`^there are users:$`, api.thereAreUsers)
s.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
s.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
s.Step(`^there are users:$`, api.thereAreUsers)
}

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -8,8 +8,3 @@ Feature: eat godogs
Given there are 12 godogs
When I eat 5
Then there should be 7 remaining
Scenario: Eat 12 out of 12
Given there are 12 godogs
When I eat 12
Then there should be none remaining

Просмотреть файл

@ -1,3 +1,4 @@
/* file: $GOPATH/src/godogs/godogs.go */
package main
// Godogs available to eat

62
examples/godogs/godogs_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,62 @@
/* file: $GOPATH/src/godogs/godogs_test.go */
package main
import (
"flag"
"fmt"
"os"
"testing"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/colors"
)
var opt = godog.Options{Output: colors.Colored(os.Stdout)}
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
}
func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()
status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
func thereAreGodogs(available int) error {
Godogs = available
return nil
}
func iEat(num int) error {
if Godogs < num {
return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
}
Godogs -= num
return nil
}
func thereShouldBeRemaining(remaining int) error {
if Godogs != remaining {
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
}
return nil
}
func FeatureContext(s *godog.Suite) {
s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
s.Step(`^I eat (\d+)$`, iEat)
s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
s.BeforeScenario(func(interface{}) {
Godogs = 0 // clean the state before every scenario
})
}

Просмотреть файл

Просмотреть файл

@ -16,10 +16,12 @@ Feature: suite events
When I run feature suite
Then these events had to be fired for a number of times:
| BeforeSuite | 1 |
| BeforeFeature | 1 |
| BeforeScenario | 1 |
| BeforeStep | 3 |
| AfterStep | 3 |
| AfterScenario | 1 |
| AfterFeature | 1 |
| AfterSuite | 1 |
Scenario: triggers appropriate events whole feature
@ -27,10 +29,12 @@ Feature: suite events
When I run feature suite
Then these events had to be fired for a number of times:
| BeforeSuite | 1 |
| BeforeFeature | 1 |
| BeforeScenario | 6 |
| BeforeStep | 19 |
| AfterStep | 19 |
| AfterScenario | 6 |
| AfterFeature | 1 |
| AfterSuite | 1 |
Scenario: triggers appropriate events for two feature files
@ -39,10 +43,12 @@ Feature: suite events
When I run feature suite
Then these events had to be fired for a number of times:
| BeforeSuite | 1 |
| BeforeFeature | 2 |
| BeforeScenario | 2 |
| BeforeStep | 7 |
| AfterStep | 7 |
| AfterScenario | 2 |
| AfterFeature | 2 |
| AfterSuite | 1 |
Scenario: should not trigger events on empty feature
@ -57,10 +63,12 @@ Feature: suite events
When I run feature suite
Then these events had to be fired for a number of times:
| BeforeSuite | 1 |
| BeforeFeature | 0 |
| BeforeScenario | 0 |
| BeforeStep | 0 |
| AfterStep | 0 |
| AfterScenario | 0 |
| AfterFeature | 0 |
| AfterSuite | 1 |
Scenario: should not trigger events on empty scenarios
@ -72,9 +80,6 @@ Feature: suite events
Scenario: two
Then passing step
And adding step state to context
And having correct context
And failing step
Scenario Outline: three
Then passing step
@ -86,71 +91,10 @@ Feature: suite events
When I run feature suite
Then these events had to be fired for a number of times:
| BeforeSuite | 1 |
| BeforeFeature | 1 |
| BeforeScenario | 2 |
| BeforeStep | 5 |
| AfterStep | 5 |
| BeforeStep | 2 |
| AfterStep | 2 |
| AfterScenario | 2 |
| AfterFeature | 1 |
| AfterSuite | 1 |
And the suite should have failed
Scenario: should add scenario hook errors to steps
Given a feature "normal.feature" file:
"""
Feature: scenario hook errors
Scenario: failing before and after scenario
Then adding step state to context
And passing step
Scenario: failing before scenario
Then adding step state to context
And passing step
Scenario: failing after scenario
Then adding step state to context
And passing step
"""
When I run feature suite with formatter "pretty"
Then the suite should have failed
And the rendered output will be as follows:
"""
Feature: scenario hook errors
Scenario: failing before and after scenario # normal.feature:3
Then adding step state to context # suite_context_test.go:0 -> InitializeScenario.func17
after scenario hook failed: failed in after scenario hook, step error: before scenario hook failed: failed in before scenario hook
And passing step # suite_context_test.go:0 -> InitializeScenario.func2
Scenario: failing before scenario # normal.feature:7
Then adding step state to context # suite_context_test.go:0 -> InitializeScenario.func17
before scenario hook failed: failed in before scenario hook
And passing step # suite_context_test.go:0 -> InitializeScenario.func2
Scenario: failing after scenario # normal.feature:11
Then adding step state to context # suite_context_test.go:0 -> InitializeScenario.func17
And passing step # suite_context_test.go:0 -> InitializeScenario.func2
after scenario hook failed: failed in after scenario hook
--- Failed steps:
Scenario: failing before and after scenario # normal.feature:3
Then adding step state to context # normal.feature:4
Error: after scenario hook failed: failed in after scenario hook, step error: before scenario hook failed: failed in before scenario hook
Scenario: failing before scenario # normal.feature:7
Then adding step state to context # normal.feature:8
Error: before scenario hook failed: failed in before scenario hook
Scenario: failing after scenario # normal.feature:11
And passing step # normal.feature:13
Error: after scenario hook failed: failed in after scenario hook
3 scenarios (3 failed)
6 steps (1 passed, 3 failed, 2 skipped)
0s
"""

Просмотреть файл

@ -323,7 +323,7 @@ Feature: cucumber json formatter
{
"keyword": "Given ",
"name": "passing step",
"line": 7,
"line": 11,
"match": {
"location": "suite_context.go:64"
},
@ -345,7 +345,7 @@ Feature: cucumber json formatter
{
"keyword": "Given ",
"name": "failing step",
"line": 7,
"line": 12,
"match": {
"location": "suite_context.go:47"
},

Просмотреть файл

@ -9,11 +9,12 @@ Feature: event stream formatter
Then the following events should be fired:
"""
TestRunStarted
TestSource
TestRunFinished
"""
Scenario: should process simple scenario
Given a feature path "features/load.feature:27"
Given a feature path "features/load.feature:24"
When I run feature suite with formatter "events"
Then the following events should be fired:
"""
@ -34,7 +35,7 @@ Feature: event stream formatter
"""
Scenario: should process outline scenario
Given a feature path "features/load.feature:35"
Given a feature path "features/load.feature:32"
When I run feature suite with formatter "events"
Then the following events should be fired:
"""

Просмотреть файл

@ -1,228 +0,0 @@
Feature: JUnit XML formatter
In order to support tools that import JUnit XML output
I need to be able to support junit formatted output
Scenario: Support of Feature Plus Scenario Node
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario
simple scenario description
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="1" skipped="0" failures="0" errors="0" time="0">
<testsuite name="simple feature" tests="1" skipped="0" failures="0" errors="0" time="0">
<testcase name="simple scenario" status="" time="0"></testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Feature Plus Scenario Node With Tags
Given a feature "features/simple.feature" file:
"""
@TAG1
Feature: simple feature
simple feature description
@TAG2 @TAG3
Scenario: simple scenario
simple scenario description
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="1" skipped="0" failures="0" errors="0" time="0">
<testsuite name="simple feature" tests="1" skipped="0" failures="0" errors="0" time="0">
<testcase name="simple scenario" status="" time="0"></testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Feature Plus Scenario Outline
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario
simple scenario description
Examples: simple examples
| status |
| pass |
| fail |
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="2" skipped="0" failures="0" errors="0" time="0">
<testsuite name="simple feature" tests="2" skipped="0" failures="0" errors="0" time="0">
<testcase name="simple scenario #1" status="" time="0"></testcase>
<testcase name="simple scenario #2" status="" time="0"></testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Feature Plus Scenario Outline With Tags
Given a feature "features/simple.feature" file:
"""
@TAG1
Feature: simple feature
simple feature description
@TAG2
Scenario Outline: simple scenario
simple scenario description
@TAG3
Examples: simple examples
| status |
| pass |
| fail |
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="2" skipped="0" failures="0" errors="0" time="0">
<testsuite name="simple feature" tests="2" skipped="0" failures="0" errors="0" time="0">
<testcase name="simple scenario #1" status="" time="0"></testcase>
<testcase name="simple scenario #2" status="" time="0"></testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Feature Plus Scenario With Steps
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario
simple scenario description
Given passing step
Then a failing step
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="1" skipped="0" failures="1" errors="0" time="0">
<testsuite name="simple feature" tests="1" skipped="0" failures="1" errors="0" time="0">
<testcase name="simple scenario" status="failed" time="0">
<failure message="Step a failing step: intentional failure"></failure>
</testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Feature Plus Scenario Outline With Steps
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario
simple scenario description
Given <status> step
Examples: simple examples
| status |
| passing |
| failing |
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="2" skipped="0" failures="1" errors="0" time="0">
<testsuite name="simple feature" tests="2" skipped="0" failures="1" errors="0" time="0">
<testcase name="simple scenario #1" status="passed" time="0"></testcase>
<testcase name="simple scenario #2" status="failed" time="0">
<failure message="Step failing step: intentional failure"></failure>
</testcase>
</testsuite>
</testsuites>
"""
# Currently godog only supports comments on Feature and not
# scenario and steps.
Scenario: Support of Comments
Given a feature "features/simple.feature" file:
"""
#Feature comment
Feature: simple feature
simple description
Scenario: simple scenario
simple feature description
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="1" skipped="0" failures="0" errors="0" time="0">
<testsuite name="simple feature" tests="1" skipped="0" failures="0" errors="0" time="0">
<testcase name="simple scenario" status="" time="0"></testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Docstrings
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple description
Scenario: simple scenario
simple feature description
Given passing step
\"\"\" content type
step doc string
\"\"\"
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="1" skipped="0" failures="0" errors="0" time="0">
<testsuite name="simple feature" tests="1" skipped="0" failures="0" errors="0" time="0">
<testcase name="simple scenario" status="passed" time="0"></testcase>
</testsuite>
</testsuites>
"""
Scenario: Support of Undefined, Pending and Skipped status
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario
simple scenario description
Given passing step
And pending step
And undefined
And passing step
"""
When I run feature suite with formatter "junit"
Then the rendered xml will be as follows:
""" application/xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="godog" tests="1" skipped="0" failures="0" errors="1" time="0">
<testsuite name="simple feature" tests="1" skipped="0" failures="0" errors="1" time="0">
<testcase name="simple scenario" status="undefined" time="0">
<error message="Step pending step: TODO: write pending definition" type="pending"></error>
<error message="Step undefined" type="undefined"></error>
<error message="Step passing step" type="skipped"></error>
</testcase>
</testsuite>
</testsuites>
"""

Просмотреть файл

@ -1,687 +0,0 @@
Feature: pretty formatter
In order to support tools that import pretty output
I need to be able to support pretty formatted output
Scenario: Support of Feature Plus Scenario Node
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario
simple scenario description
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario # features/simple.feature:3
1 scenarios (1 undefined)
No steps
0s
"""
Scenario: Support of Feature Plus Scenario Node With Tags
Given a feature "features/simple.feature" file:
"""
@TAG1
Feature: simple feature
simple feature description
@TAG2 @TAG3
Scenario: simple scenario
simple scenario description
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario # features/simple.feature:5
1 scenarios (1 undefined)
No steps
0s
"""
Scenario: Support of Feature Plus Scenario Outline
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario
simple scenario description
Examples: simple examples
| status |
| pass |
| fail |
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario # features/simple.feature:4
Examples: simple examples
| status |
| pass |
| fail |
2 scenarios (2 undefined)
No steps
0s
"""
Scenario: Support of Feature Plus Scenario Outline With Tags
Given a feature "features/simple.feature" file:
"""
@TAG1
Feature: simple feature
simple feature description
@TAG2
Scenario Outline: simple scenario
simple scenario description
@TAG3
Examples: simple examples
| status |
| pass |
| fail |
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario # features/simple.feature:6
Examples: simple examples
| status |
| pass |
| fail |
2 scenarios (2 undefined)
No steps
0s
"""
Scenario: Support of Feature Plus Scenario With Steps
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario
simple scenario description
Given passing step
Then a failing step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario # features/simple.feature:4
Given passing step # suite_context.go:0 -> SuiteContext.func2
Then a failing step # suite_context.go:0 -> *suiteContext
intentional failure
--- Failed steps:
Scenario: simple scenario # features/simple.feature:4
Then a failing step # features/simple.feature:8
Error: intentional failure
1 scenarios (1 failed)
2 steps (1 passed, 1 failed)
0s
"""
Scenario: Support of Feature Plus Scenario Outline With Steps
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario
simple scenario description
Given <status> step
Examples: simple examples
| status |
| passing |
| failing |
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario Outline: simple scenario # features/simple.feature:4
Given <status> step # suite_context.go:0 -> SuiteContext.func2
Examples: simple examples
| status |
| passing |
| failing |
intentional failure
--- Failed steps:
Scenario Outline: simple scenario # features/simple.feature:4
Given failing step # features/simple.feature:7
Error: intentional failure
2 scenarios (1 passed, 1 failed)
2 steps (1 passed, 1 failed)
0s
"""
# Currently godog only supports comments on Feature and not
# scenario and steps.
Scenario: Support of Comments
Given a feature "features/simple.feature" file:
"""
#Feature comment
Feature: simple feature
simple description
Scenario: simple scenario
simple feature description
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple description
Scenario: simple scenario # features/simple.feature:5
1 scenarios (1 undefined)
No steps
0s
"""
Scenario: Support of Docstrings
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple description
Scenario: simple scenario
simple feature description
Given passing step
\"\"\" content type
step doc string
\"\"\"
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple description
Scenario: simple scenario # features/simple.feature:4
Given passing step # suite_context.go:0 -> SuiteContext.func2
\"\"\" content type
step doc string
\"\"\"
1 scenarios (1 passed)
1 steps (1 passed)
0s
"""
Scenario: Support of Undefined, Pending and Skipped status
Given a feature "features/simple.feature" file:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario
simple scenario description
Given passing step
And pending step
And undefined doc string
\"\"\"
abc
\"\"\"
And undefined table
| a | b | c |
| 1 | 2 | 3 |
And passing step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature
simple feature description
Scenario: simple scenario # features/simple.feature:4
Given passing step # suite_context.go:0 -> SuiteContext.func2
And pending step # suite_context.go:0 -> SuiteContext.func1
TODO: write pending definition
And undefined doc string
\"\"\"
abc
\"\"\"
And undefined table
| a | b | c |
| 1 | 2 | 3 |
And passing step # suite_context.go:0 -> SuiteContext.func2
1 scenarios (1 pending, 1 undefined)
5 steps (1 passed, 1 pending, 2 undefined, 1 skipped)
0s
You can implement step definitions for undefined steps with these snippets:
func undefinedDocString(arg1 *godog.DocString) error {
return godog.ErrPending
}
func undefinedTable(arg1 *godog.Table) error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^undefined doc string$`, undefinedDocString)
ctx.Step(`^undefined table$`, undefinedTable)
}
"""
# Ensure s will not break when injecting data from BeforeStep
Scenario: Support data injection in BeforeStep
Given a feature "features/inject.feature" file:
"""
Feature: inject long value
Scenario: test scenario
Given Ignore I save some value X under key Y
And I allow variable injection
When Ignore I use value {{Y}}
Then Ignore Godog rendering should not break
And Ignore test
| key | val |
| 1 | 2 |
| 3 | 4 |
And I disable variable injection
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: inject long value
Scenario: test scenario # features/inject.feature:3
Given Ignore I save some value X under key Y # suite_context.go:0 -> SuiteContext.func12
And I allow variable injection # suite_context.go:0 -> *suiteContext
When Ignore I use value someverylonginjectionsoweacanbesureitsurpasstheinitiallongeststeplenghtanditwillhelptestsmethodsafety # suite_context.go:0 -> SuiteContext.func12
Then Ignore Godog rendering should not break # suite_context.go:0 -> SuiteContext.func12
And Ignore test # suite_context.go:0 -> SuiteContext.func12
| key | val |
| 1 | 2 |
| 3 | 4 |
And I disable variable injection # suite_context.go:0 -> *suiteContext
1 scenarios (1 passed)
6 steps (6 passed)
0s
"""
Scenario: Should scenarios identified with path:line and preserve the order.
Given a feature path "features/load.feature:6"
And a feature path "features/multistep.feature:6"
And a feature path "features/load.feature:27"
And a feature path "features/multistep.feature:23"
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: load features
In order to run features
As a test suite
I need to be able to load features
Scenario: load features within path # features/load.feature:6
Given a feature path "features" # suite_context_test.go:0 -> *godogFeaturesScenario
When I parse features # suite_context_test.go:0 -> *godogFeaturesScenario
Then I should have 14 feature files: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
features/background.feature
features/events.feature
features/formatter/cucumber.feature
features/formatter/events.feature
features/formatter/junit.feature
features/formatter/pretty.feature
features/lang.feature
features/load.feature
features/multistep.feature
features/outline.feature
features/run.feature
features/snippets.feature
features/tags.feature
features/testingt.feature
\"\"\"
Feature: run features with nested steps
In order to test multisteps
As a test suite
I need to be able to execute multisteps
Scenario: should run passing multistep successfully # features/multistep.feature:6
Given a feature "normal.feature" file: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
Feature: normal feature
Scenario: run passing multistep
Given passing step
Then passing multistep
\"\"\"
When I run feature suite # suite_context_test.go:0 -> *godogFeaturesScenario
Then the suite should have passed # suite_context_test.go:0 -> *godogFeaturesScenario
And the following steps should be passed: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
passing step
passing multistep
\"\"\"
Feature: load features
In order to run features
As a test suite
I need to be able to load features
Scenario: load a specific feature file # features/load.feature:27
Given a feature path "features/load.feature" # suite_context_test.go:0 -> *godogFeaturesScenario
When I parse features # suite_context_test.go:0 -> *godogFeaturesScenario
Then I should have 1 feature file: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
features/load.feature
\"\"\"
Feature: run features with nested steps
In order to test multisteps
As a test suite
I need to be able to execute multisteps
Scenario: should fail multistep # features/multistep.feature:23
Given a feature "failed.feature" file: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
Feature: failed feature
Scenario: run failing multistep
Given passing step
When failing multistep
Then I should have 1 scenario registered
\"\"\"
When I run feature suite # suite_context_test.go:0 -> *godogFeaturesScenario
Then the suite should have failed # suite_context_test.go:0 -> *godogFeaturesScenario
And the following step should be failed: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
failing multistep
\"\"\"
And the following steps should be skipped: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
I should have 1 scenario registered
\"\"\"
And the following steps should be passed: # suite_context_test.go:0 -> *godogFeaturesScenario
\"\"\"
passing step
\"\"\"
4 scenarios (4 passed)
16 steps (16 passed)
0s
"""
Scenario: Support of Feature Plus Rule
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule
simple feature description
Rule: simple rule
simple rule description
Example: simple scenario
simple scenario description
Given passing step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule
simple feature description
Example: simple scenario # features/simple.feature:5
Given passing step # suite_context.go:0 -> SuiteContext.func2
1 scenarios (1 passed)
1 steps (1 passed)
0s
"""
Scenario: Support of Feature Plus Rule with Background
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule with Background
simple feature description
Rule: simple rule
simple rule description
Background:
Given passing step
Example: simple scenario
simple scenario description
Given passing step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule with Background
simple feature description
Background:
Given passing step # suite_context.go:0 -> SuiteContext.func2
Example: simple scenario # features/simple.feature:7
Given passing step # suite_context.go:0 -> SuiteContext.func2
1 scenarios (1 passed)
2 steps (2 passed)
0s
"""
Scenario: Support of Feature Plus Rule with Scenario Outline
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule with Scenario Outline
simple feature description
Rule: simple rule
simple rule description
Scenario Outline: simple scenario
simple scenario description
Given <status> step
Examples: simple examples
| status |
| passing |
| failing |
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule with Scenario Outline
simple feature description
Scenario Outline: simple scenario # features/simple.feature:5
Given <status> step # suite_context.go:0 -> SuiteContext.func2
Examples: simple examples
| status |
| passing |
| failing |
intentional failure
--- Failed steps:
Scenario Outline: simple scenario # features/simple.feature:5
Given failing step # features/simple.feature:8
Error: intentional failure
2 scenarios (1 passed, 1 failed)
2 steps (1 passed, 1 failed)
0s
"""
Scenario: Use 'given' keyword on a declared 'when' step
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule
simple feature description
Rule: simple rule
simple rule description
Example: simple scenario
simple scenario description
Given a when step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule
simple feature description
Example: simple scenario # features/simple.feature:5
Given a when step
1 scenarios (1 undefined)
1 steps (1 undefined)
0s
You can implement step definitions for undefined steps with these snippets:
func aWhenStep() error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^a when step$`, aWhenStep)
}
"""
Scenario: Use 'when' keyword on a declared 'then' step
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule
simple feature description
Rule: simple rule
simple rule description
Example: simple scenario
simple scenario description
When a then step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule
simple feature description
Example: simple scenario # features/simple.feature:5
When a then step
1 scenarios (1 undefined)
1 steps (1 undefined)
0s
You can implement step definitions for undefined steps with these snippets:
func aThenStep() error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^a then step$`, aThenStep)
}
"""
Scenario: Use 'then' keyword on a declared 'given' step
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule
simple feature description
Rule: simple rule
simple rule description
Example: simple scenario
simple scenario description
Then a given step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule
simple feature description
Example: simple scenario # features/simple.feature:5
Then a given step
1 scenarios (1 undefined)
1 steps (1 undefined)
0s
You can implement step definitions for undefined steps with these snippets:
func aGivenStep() error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^a given step$`, aGivenStep)
}
"""
Scenario: Match keyword functions correctly
Given a feature "features/simple.feature" file:
"""
Feature: simple feature with a rule
simple feature description
Rule: simple rule
simple rule description
Example: simple scenario
simple scenario description
Given a given step
When a when step
Then a then step
And a then step
"""
When I run feature suite with formatter "pretty"
Then the rendered output will be as follows:
"""
Feature: simple feature with a rule
simple feature description
Example: simple scenario # features/simple.feature:5
Given a given step # suite_context_test.go:0 -> InitializeScenario.func3
When a when step # suite_context_test.go:0 -> InitializeScenario.func4
Then a then step # suite_context_test.go:0 -> InitializeScenario.func5
And a then step # suite_context_test.go:0 -> InitializeScenario.func5
1 scenarios (1 passed)
4 steps (4 passed)
0s
"""

Просмотреть файл

@ -8,14 +8,12 @@ Savybė: užkrauti savybes
Scenarijus: savybių užkrovimas iš aplanko
Duota savybių aplankas "features"
Kai aš išskaitau savybes
Tada aš turėčiau turėti 14 savybių failus:
Tada aš turėčiau turėti 11 savybių failus:
"""
features/background.feature
features/events.feature
features/formatter/cucumber.feature
features/formatter/events.feature
features/formatter/junit.feature
features/formatter/pretty.feature
features/lang.feature
features/load.feature
features/multistep.feature
@ -23,5 +21,4 @@ Savybė: užkrauti savybes
features/run.feature
features/snippets.feature
features/tags.feature
features/testingt.feature
"""

Просмотреть файл

@ -6,14 +6,12 @@ Feature: load features
Scenario: load features within path
Given a feature path "features"
When I parse features
Then I should have 14 feature files:
Then I should have 11 feature files:
"""
features/background.feature
features/events.feature
features/formatter/cucumber.feature
features/formatter/events.feature
features/formatter/junit.feature
features/formatter/pretty.feature
features/lang.feature
features/load.feature
features/multistep.feature
@ -21,7 +19,6 @@ Feature: load features
features/run.feature
features/snippets.feature
features/tags.feature
features/testingt.feature
"""
Scenario: load a specific feature file
@ -41,7 +38,7 @@ Feature: load features
| feature | number |
| features/load.feature:3 | 0 |
| features/load.feature:6 | 1 |
| features/load.feature | 6 |
| features/load.feature | 4 |
Scenario: load a number of feature files
Given a feature path "features/load.feature"

Просмотреть файл

@ -138,63 +138,3 @@ Feature: run features with nested steps
"""
I should have 1 scenario registered
"""
Scenario: context passed between steps
Given a feature "normal.feature" file:
"""
Feature: normal feature
Scenario: run passing multistep
Given I return a context from a step
Then I should see the context in the next step
"""
When I run feature suite
Then the suite should have passed
Scenario: context passed between steps
Given a feature "normal.feature" file:
"""
Feature: normal feature
Scenario: run passing multistep
Given I can see contexts passed in multisteps
"""
When I run feature suite
Then the suite should have passed
Scenario: should run passing multistep using keyword function successfully
Given a feature "normal.feature" file:
"""
Feature: normal feature
Scenario: run passing multistep
Given passing step
Then passing multistep using 'then' function
"""
When I run feature suite
Then the suite should have passed
And the following steps should be passed:
"""
passing step
passing multistep using 'then' function
"""
Scenario: should identify undefined multistep using keyword function
Given a feature "normal.feature" file:
"""
Feature: normal feature
Scenario: run passing multistep
Given passing step
Then undefined multistep using 'then' function
"""
When I run feature suite
Then the suite should have passed
And the following steps should be passed:
"""
passing step
"""
And the following step should be undefined:
"""
undefined multistep using 'then' function
"""

Просмотреть файл

@ -262,16 +262,3 @@ Feature: run features
another undefined step
"""
And the suite should have failed
Scenario: should be able to convert a Doc String to a `*godog.DocString` argument
Given call func(*godog.DocString) with:
"""
text
"""
Scenario: should be able to convert a Doc String to a `string` argument
Given call func(string) with:
"""
text
"""

Просмотреть файл

@ -28,9 +28,9 @@ Feature: undefined step snippets
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I send "([^"]*)" request to "([^"]*)"$`, iSendRequestTo)
ctx.Step(`^the response code should be (\d+)$`, theResponseCodeShouldBe)
func FeatureContext(s *godog.Suite) {
s.Step(`^I send "([^"]*)" request to "([^"]*)"$`, iSendRequestTo)
s.Step(`^the response code should be (\d+)$`, theResponseCodeShouldBe)
}
"""
@ -44,19 +44,11 @@ Feature: undefined step snippets
| col1 | val1 |
| col2 | val2 |
Then the response code should be 200 and header "X-Powered-By" should be "godog"
And the response body should be:
\"\"\"
Hello World
\"\"\"
"""
When I run feature suite
Then the undefined step snippets should be:
"""
func iSendRequestToWith(arg1, arg2 string, arg3 *godog.Table) error {
return godog.ErrPending
}
func theResponseBodyShouldBe(arg1 *godog.DocString) error {
func iSendRequestToWith(arg1, arg2 string, arg3 *gherkin.DataTable) error {
return godog.ErrPending
}
@ -64,10 +56,9 @@ Feature: undefined step snippets
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I send "([^"]*)" request to "([^"]*)" with:$`, iSendRequestToWith)
ctx.Step(`^the response body should be:$`, theResponseBodyShouldBe)
ctx.Step(`^the response code should be (\d+) and header "([^"]*)" should be "([^"]*)"$`, theResponseCodeShouldBeAndHeaderShouldBe)
func FeatureContext(s *godog.Suite) {
s.Step(`^I send "([^"]*)" request to "([^"]*)" with:$`, iSendRequestToWith)
s.Step(`^the response code should be (\d+) and header "([^"]*)" should be "([^"]*)"$`, theResponseCodeShouldBeAndHeaderShouldBe)
}
"""
@ -96,9 +87,9 @@ Feature: undefined step snippets
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I pull from github\.com$`, iPullFromGithubcom)
ctx.Step(`^the project should be there$`, theProjectShouldBeThere)
func FeatureContext(s *godog.Suite) {
s.Step(`^I pull from github\.com$`, iPullFromGithubcom)
s.Step(`^the project should be there$`, theProjectShouldBeThere)
}
"""
@ -114,17 +105,17 @@ Feature: undefined step snippets
When I run feature suite
And the undefined step snippets should be:
"""
func iAddTheToTheBasket(arg1 string) error {
return godog.ErrPending
}
func thereIsAWhichCosts(arg1 string, arg2 int) error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I add the "([^"]*)" to the basket$`, iAddTheToTheBasket)
ctx.Step(`^there is a "([^"]*)", which costs £(\d+)$`, thereIsAWhichCosts)
func iAddTheToTheBasket(arg1 string) error {
return godog.ErrPending
}
func FeatureContext(s *godog.Suite) {
s.Step(`^there is a "([^"]*)", which costs £(\d+)$`, thereIsAWhichCosts)
s.Step(`^I add the "([^"]*)" to the basket$`, iAddTheToTheBasket)
}
"""
@ -140,56 +131,16 @@ Feature: undefined step snippets
When I run feature suite
And the undefined step snippets should be:
"""
func godogs(arg1 int) error {
return godog.ErrPending
}
func whichCosts(arg1 string, arg2 int) error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^(\d+) godogs$`, godogs)
ctx.Step(`^"([^"]*)", which costs £(\d+)$`, whichCosts)
}
"""
Scenario: Для русских сценариев генерируются русские функции
Given a feature "undefined.feature" file:
"""
# language: ru
Функционал: суперфича
Сценарий: делает что-то полезное
Дано что-то
Когда я делаю ещё что-то
То получается ещё более что-то
"""
When I run feature suite
Then the following steps should be undefined:
"""
получается ещё более что-то
что-то
я делаю ещё что-то
"""
And the undefined step snippets should be:
"""
func получаетсяЕщёБолееЧтото() error {
func godogs(arg1 int) error {
return godog.ErrPending
}
func чтото() error {
return godog.ErrPending
}
func яДелаюЕщёЧтото() error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^получается ещё более что-то$`, получаетсяЕщёБолееЧтото)
ctx.Step(`^что-то$`, чтото)
ctx.Step(`^я делаю ещё что-то$`, яДелаюЕщёЧтото)
func FeatureContext(s *godog.Suite) {
s.Step(`^"([^"]*)", which costs £(\d+)$`, whichCosts)
s.Step(`^(\d+) godogs$`, godogs)
}
"""

Просмотреть файл

@ -10,7 +10,6 @@ Feature: tag filters
Background:
Given passing step
And passing step without return
Scenario Outline: parse a scenario
Given a feature path "<path>"
@ -125,75 +124,3 @@ Feature: tag filters
"""
a feature path "four"
"""
Scenario: empty filter and scenarios with f-tag - are executed only
Given a feature "normal.feature" file:
"""
Feature: f-tagged
Scenario: one
Given a feature path "one"
Scenario: two
Given a feature path "two"
@f
Scenario: three
Given a feature path "three"
@f
Scenario: four
Given a feature path "four"
"""
When I run feature suite with tags ""
Then the suite should have passed
And I should have 2 scenario registered
And the following steps should be passed:
"""
a feature path "three"
a feature path "four"
"""
Scenario: two feature files and scenarios with f-tag - are executed only
Given a feature "normal.feature" file:
"""
Feature: f-tagged
Scenario: one
Given a feature path "one"
Scenario: two
Given a feature path "two"
@f
Scenario: three
Given a feature path "three"
@f
Scenario: four
Given a feature path "four"
"""
And a feature "another.feature" file:
"""
Feature: non-tagged
Scenario: five
Given a feature path "five"
Scenario: six
Given a feature path "six"
Scenario: seven
Given a feature path "seven"
Scenario: eight
Given a feature path "eight"
"""
When I run feature suite with tags ""
Then the suite should have passed
And I should have 2 scenario registered
And the following steps should be passed:
"""
a feature path "three"
a feature path "four"
"""

Просмотреть файл

@ -1,194 +0,0 @@
Feature: providing testingT compatibility
In order to test application behavior using standard go assertion techniques
As a test suite
I need to be able to provide a testing.T compatible interface
Scenario Outline: should fail test with no message if <op> called on testing T
Given a feature "failed.feature" file:
"""
Feature: failed feature
Scenario: fail a scenario
Given passing step
When my step fails the test by calling <op> on testing T
"""
When I run feature suite
Then the suite should have failed
And the following steps should be passed:
"""
passing step
"""
And the following step should be failed:
"""
my step fails the test by calling <op> on testing T
"""
Examples:
| op |
| Fail |
| FailNow |
Scenario Outline: should fail test with message if <op> called on T
Given a feature "failed.feature" file:
"""
Feature: failed feature
Scenario: fail a scenario
Given passing step
When my step fails the test by calling <op> on testing T with message "an unformatted message"
"""
When I run feature suite
Then the suite should have failed
And the following steps should be passed:
"""
passing step
"""
And the following step should be failed:
"""
my step fails the test by calling <op> on testing T with message "an unformatted message"
"""
Examples:
| op |
| Error |
| Fatal |
Scenario Outline: should fail test with formatted message if <op> called on T
Given a feature "failed.feature" file:
"""
Feature: failed feature
Scenario: fail a scenario
Given passing step
When my step fails the test by calling <op> on testing T with message "a formatted message %s" and argument "arg1"
"""
When I run feature suite
Then the suite should have failed
And the following steps should be passed:
"""
passing step
"""
And the following step should be failed:
"""
my step fails the test by calling <op> on testing T with message "a formatted message %s" and argument "arg1"
"""
Examples:
| op |
| Errorf |
| Fatalf |
Scenario: should pass test when testify assertions pass
Given a feature "testify.feature" file:
"""
Feature: passed feature
Scenario: pass a scenario
Given passing step
When my step calls testify's assert.Equal with expected "exp" and actual "exp"
When my step calls testify's require.Equal with expected "exp" and actual "exp"
"""
When I run feature suite
Then the suite should have passed
And the following steps should be passed:
"""
passing step
my step calls testify's assert.Equal with expected "exp" and actual "exp"
my step calls testify's require.Equal with expected "exp" and actual "exp"
"""
Scenario: should fail test when testify assertions do not pass
Given a feature "testify.feature" file:
"""
Feature: failed feature
Scenario: fail a scenario
Given passing step
When my step calls testify's assert.Equal with expected "exp" and actual "not"
And my step calls testify's assert.Equal with expected "exp2" and actual "not"
"""
When I run feature suite
Then the suite should have failed
And the following steps should be passed:
"""
passing step
"""
And the following steps should be failed:
"""
my step calls testify's assert.Equal with expected "exp" and actual "not"
"""
And the following steps should be skipped:
"""
my step calls testify's assert.Equal with expected "exp2" and actual "not"
"""
Scenario: should fail test when multiple testify assertions are used in a step
Given a feature "testify.feature" file:
"""
Feature: failed feature
Scenario: fail a scenario
Given passing step
When my step calls testify's assert.Equal 3 times
"""
When I run feature suite
Then the suite should have failed
And the following steps should be passed:
"""
passing step
"""
And the following steps should be failed:
"""
my step calls testify's assert.Equal 3 times
"""
Scenario: should pass test when multiple testify assertions are used successfully in a step
Given a feature "testify.feature" file:
"""
Feature: passed feature
Scenario: pass a scenario
Given passing step
When my step calls testify's assert.Equal 3 times with match
"""
When I run feature suite
Then the suite should have passed
And the following steps should be passed:
"""
passing step
my step calls testify's assert.Equal 3 times with match
"""
Scenario Outline: should skip test when <op> is called on the testing.T
Given a feature "testify.feature" file:
"""
Feature: skipped feature
Scenario: skip a scenario
Given passing step
When my step skips the test by calling <op> on testing T
"""
When I run feature suite
Then the suite should have passed
And the following steps should be passed:
"""
passing step
"""
And the following steps should be skipped:
"""
my step skips the test by calling <op> on testing T
"""
Examples:
| op |
| Skip |
| SkipNow |
Scenario: should log when Logf/Log called on testing.T
When my step calls Logf on testing T with message "format this %s" and argument "formatparam1"
And my step calls Log on testing T with message "log this message"
Then the logged messages should include "format this formatparam1"
And the logged messages should include "log this message"
Scenario: should log when godog.Logf/Log called
When my step calls godog.Logf with message "format this %s" and argument "formatparam1"
And my step calls godog.Log with message "log this message"
Then the logged messages should include "format this formatparam1"
And the logged messages should include "log this message"

121
flags.go
Просмотреть файл

@ -4,23 +4,19 @@ import (
"flag"
"fmt"
"io"
"sort"
"math/rand"
"strconv"
"strings"
"time"
"git.golang1.ru/softonik/godog/colors"
"git.golang1.ru/softonik/godog/internal/utils"
"github.com/DATA-DOG/godog/colors"
)
// repeats a space n times
var s = utils.S
var descFeaturesArgument = "Optional feature(s) to run. Can be:\n" +
s(4) + "- dir " + colors.Yellow("(features/)") + "\n" +
s(4) + "- feature " + colors.Yellow("(*.feature)") + "\n" +
s(4) + "- scenario at specific line " + colors.Yellow("(*.feature:10)") + "\n" +
"If no feature paths are listed, suite tries " + colors.Yellow("features") + " path by default.\n" +
"Multiple comma-separated values can be provided.\n"
"If no feature paths are listed, suite tries " + colors.Yellow("features") + " path by default.\n"
var descConcurrencyOption = "Run the test suite with concurrency level:\n" +
s(4) + "- " + colors.Yellow(`= 1`) + ": supports all types of formats.\n" +
@ -39,8 +35,6 @@ var descRandomOption = "Randomly shuffle the scenario execution order.\n" +
// FlagSet allows to manage flags by external suite runner
// builds flag.FlagSet with godog flags binded
//
// Deprecated:
func FlagSet(opt *Options) *flag.FlagSet {
set := flag.NewFlagSet("godog", flag.ExitOnError)
BindFlags("", set, opt)
@ -51,87 +45,25 @@ func FlagSet(opt *Options) *flag.FlagSet {
// BindFlags binds godog flags to given flag set prefixed
// by given prefix, without overriding usage
func BindFlags(prefix string, set *flag.FlagSet, opt *Options) {
set.Usage = usage(set, set.Output())
descFormatOption := "How to format tests output. Built-in formats:\n"
type fm struct {
name string
desc string
}
var fms []fm
// @TODO: sort by name
for name, desc := range AvailableFormatters() {
fms = append(fms, fm{
name: name,
desc: desc,
})
descFormatOption += s(4) + "- " + colors.Yellow(name) + ": " + desc + "\n"
}
sort.Slice(fms, func(i, j int) bool {
return fms[i].name < fms[j].name
})
for _, fm := range fms {
descFormatOption += s(4) + "- " + colors.Yellow(fm.name) + ": " + fm.desc + "\n"
}
descFormatOption = strings.TrimSpace(descFormatOption)
// override flag defaults if any corresponding properties were supplied on the incoming `opt`
defFormatOption := "pretty"
if opt.Format != "" {
defFormatOption = opt.Format
}
defTagsOption := ""
if opt.Tags != "" {
defTagsOption = opt.Tags
}
defConcurrencyOption := 1
if opt.Concurrency != 0 {
defConcurrencyOption = opt.Concurrency
}
defShowStepDefinitions := false
if opt.ShowStepDefinitions {
defShowStepDefinitions = opt.ShowStepDefinitions
}
defStopOnFailure := false
if opt.StopOnFailure {
defStopOnFailure = opt.StopOnFailure
}
defStrict := false
if opt.Strict {
defStrict = opt.Strict
}
defNoColors := false
if opt.NoColors {
defNoColors = opt.NoColors
}
set.StringVar(&opt.Format, prefix+"format", defFormatOption, descFormatOption)
set.StringVar(&opt.Format, prefix+"f", defFormatOption, descFormatOption)
set.StringVar(&opt.Tags, prefix+"tags", defTagsOption, descTagsOption)
set.StringVar(&opt.Tags, prefix+"t", defTagsOption, descTagsOption)
set.IntVar(&opt.Concurrency, prefix+"concurrency", defConcurrencyOption, descConcurrencyOption)
set.IntVar(&opt.Concurrency, prefix+"c", defConcurrencyOption, descConcurrencyOption)
set.BoolVar(&opt.ShowStepDefinitions, prefix+"definitions", defShowStepDefinitions, "Print all available step definitions.")
set.BoolVar(&opt.ShowStepDefinitions, prefix+"d", defShowStepDefinitions, "Print all available step definitions.")
set.BoolVar(&opt.StopOnFailure, prefix+"stop-on-failure", defStopOnFailure, "Stop processing on first failed scenario.")
set.BoolVar(&opt.Strict, prefix+"strict", defStrict, "Fail suite when there are pending or undefined or ambiguous steps.")
set.BoolVar(&opt.NoColors, prefix+"no-colors", defNoColors, "Disable ansi colors.")
set.StringVar(&opt.Format, prefix+"format", "pretty", descFormatOption)
set.StringVar(&opt.Format, prefix+"f", "pretty", descFormatOption)
set.StringVar(&opt.Tags, prefix+"tags", "", descTagsOption)
set.StringVar(&opt.Tags, prefix+"t", "", descTagsOption)
set.IntVar(&opt.Concurrency, prefix+"concurrency", 1, descConcurrencyOption)
set.IntVar(&opt.Concurrency, prefix+"c", 1, descConcurrencyOption)
set.BoolVar(&opt.ShowStepDefinitions, prefix+"definitions", false, "Print all available step definitions.")
set.BoolVar(&opt.ShowStepDefinitions, prefix+"d", false, "Print all available step definitions.")
set.BoolVar(&opt.StopOnFailure, prefix+"stop-on-failure", false, "Stop processing on first failed scenario.")
set.BoolVar(&opt.Strict, prefix+"strict", false, "Fail suite when there are pending or undefined steps.")
set.BoolVar(&opt.NoColors, prefix+"no-colors", false, "Disable ansi colors.")
set.Var(&randomSeed{&opt.Randomize}, prefix+"random", descRandomOption)
set.BoolVar(&opt.ShowHelp, "godog.help", false, "Show usage help.")
set.Func(prefix+"paths", descFeaturesArgument, func(paths string) error {
if paths != "" {
opt.Paths = strings.Split(paths, ",")
}
return nil
})
}
type flagged struct {
@ -208,7 +140,15 @@ func usage(set *flag.FlagSet, w io.Writer) func() {
// --- GENERAL ---
fmt.Fprintln(w, colors.Yellow("Usage:"))
fmt.Fprint(w, s(2)+"go test [options]\n\n")
fmt.Fprintf(w, s(2)+"godog [options] [<features>]\n\n")
// description
fmt.Fprintln(w, "Builds a test package and runs given feature files.")
fmt.Fprintf(w, "Command should be run from the directory of tested package and contain buildable go source.\n\n")
// --- ARGUMENTS ---
fmt.Fprintln(w, colors.Yellow("Arguments:"))
// --> features
fmt.Fprintln(w, opt("features", descFeaturesArgument))
// --- OPTIONS ---
fmt.Fprintln(w, colors.Yellow("Options:"))
@ -224,9 +164,16 @@ type randomSeed struct {
ref *int64
}
// Choose randomly assigns a convenient pseudo-random seed value.
// The resulting seed will be between `1-99999` for later ease of specification.
func (rs *randomSeed) choose() {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
*rs.ref = r.Int63n(99998) + 1
}
func (rs *randomSeed) Set(s string) error {
if s == "true" {
*rs.ref = makeRandomSeed()
rs.choose()
return nil
}

Просмотреть файл

@ -2,13 +2,11 @@ package godog
import (
"bytes"
"flag"
"fmt"
"strings"
"testing"
"git.golang1.ru/softonik/godog/colors"
"git.golang1.ru/softonik/godog/internal/formatters"
"github.com/DATA-DOG/godog/colors"
)
func TestFlagsShouldRandomizeAndGenerateSeed(t *testing.T) {
@ -62,7 +60,7 @@ func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
output := colors.Uncolored(&buf)
// register some custom formatter
Format("custom", "custom format description", formatters.JUnitFormatterFunc)
Format("custom", "custom format description", junitFunc)
var opt Options
flags := FlagSet(&opt)
@ -77,128 +75,3 @@ func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
}
}
}
func TestBindFlagsShouldRespectFlagDefaults(t *testing.T) {
opts := Options{}
BindFlags("flagDefaults.", flag.CommandLine, &opts)
if opts.Format != "pretty" {
t.Fatalf("expected Format: pretty, but it was: %s", opts.Format)
}
if opts.Tags != "" {
t.Fatalf("expected Tags: '', but it was: %s", opts.Tags)
}
if opts.Concurrency != 1 {
t.Fatalf("expected Concurrency: 1, but it was: %d", opts.Concurrency)
}
if opts.ShowStepDefinitions {
t.Fatalf("expected ShowStepDefinitions: false, but it was: %t", opts.ShowStepDefinitions)
}
if opts.StopOnFailure {
t.Fatalf("expected StopOnFailure: false, but it was: %t", opts.StopOnFailure)
}
if opts.Strict {
t.Fatalf("expected Strict: false, but it was: %t", opts.Strict)
}
if opts.NoColors {
t.Fatalf("expected NoColors: false, but it was: %t", opts.NoColors)
}
if opts.Randomize != 0 {
t.Fatalf("expected Randomize: 0, but it was: %d", opts.Randomize)
}
}
func TestBindFlagsShouldRespectOptDefaults(t *testing.T) {
opts := Options{
Format: "progress",
Tags: "test",
Concurrency: 2,
ShowStepDefinitions: true,
StopOnFailure: true,
Strict: true,
NoColors: true,
Randomize: int64(7),
}
flagSet := flag.FlagSet{}
BindFlags("optDefaults.", &flagSet, &opts)
if opts.Format != "progress" {
t.Fatalf("expected Format: progress, but it was: %s", opts.Format)
}
if opts.Tags != "test" {
t.Fatalf("expected Tags: 'test', but it was: %s", opts.Tags)
}
if opts.Concurrency != 2 {
t.Fatalf("expected Concurrency: 2, but it was: %d", opts.Concurrency)
}
if !opts.ShowStepDefinitions {
t.Fatalf("expected ShowStepDefinitions: true, but it was: %t", opts.ShowStepDefinitions)
}
if !opts.StopOnFailure {
t.Fatalf("expected StopOnFailure: true, but it was: %t", opts.StopOnFailure)
}
if !opts.Strict {
t.Fatalf("expected Strict: true, but it was: %t", opts.Strict)
}
if !opts.NoColors {
t.Fatalf("expected NoColors: true, but it was: %t", opts.NoColors)
}
if opts.Randomize != 7 {
t.Fatalf("expected Randomize: 7, but it was: %d", opts.Randomize)
}
}
func TestBindFlagsShouldRespectFlagOverrides(t *testing.T) {
opts := Options{
Format: "progress",
Tags: "test",
Concurrency: 2,
ShowStepDefinitions: true,
StopOnFailure: true,
Strict: true,
NoColors: true,
Randomize: 11,
}
flagSet := flag.FlagSet{}
BindFlags("optOverrides.", &flagSet, &opts)
flagSet.Parse([]string{
"--optOverrides.format=junit",
"--optOverrides.tags=test2",
"--optOverrides.concurrency=3",
"--optOverrides.definitions=false",
"--optOverrides.stop-on-failure=false",
"--optOverrides.strict=false",
"--optOverrides.no-colors=false",
"--optOverrides.random=2",
})
if opts.Format != "junit" {
t.Fatalf("expected Format: junit, but it was: %s", opts.Format)
}
if opts.Tags != "test2" {
t.Fatalf("expected Tags: 'test2', but it was: %s", opts.Tags)
}
if opts.Concurrency != 3 {
t.Fatalf("expected Concurrency: 3, but it was: %d", opts.Concurrency)
}
if opts.ShowStepDefinitions {
t.Fatalf("expected ShowStepDefinitions: true, but it was: %t", opts.ShowStepDefinitions)
}
if opts.StopOnFailure {
t.Fatalf("expected StopOnFailure: true, but it was: %t", opts.StopOnFailure)
}
if opts.Strict {
t.Fatalf("expected Strict: true, but it was: %t", opts.Strict)
}
if opts.NoColors {
t.Fatalf("expected NoColors: true, but it was: %t", opts.NoColors)
}
if opts.Randomize != 2 {
t.Fatalf("expected Randomize: 2, but it was: %d", opts.Randomize)
}
}

Просмотреть файл

@ -1,33 +0,0 @@
package godog
import (
"errors"
"flag"
"math/rand"
"time"
"github.com/spf13/pflag"
"git.golang1.ru/softonik/godog/internal/flags"
)
// Choose randomly assigns a convenient pseudo-random seed value.
// The resulting seed will be between `1-99999` for later ease of specification.
func makeRandomSeed() int64 {
return rand.New(rand.NewSource(time.Now().UTC().UnixNano())).Int63n(99998) + 1
}
func flagSet(opt *Options) *pflag.FlagSet {
set := pflag.NewFlagSet("godog", pflag.ExitOnError)
flags.BindRunCmdFlags("", set, opt)
pflag.ErrHelp = errors.New("godog: help requested")
return set
}
// BindCommandLineFlags binds godog flags to given flag set prefixed
// by given prefix, without overriding usage
func BindCommandLineFlags(prefix string, opts *Options) {
flagSet := pflag.CommandLine
flags.BindRunCmdFlags(prefix, flagSet, opts)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
}

Просмотреть файл

@ -1,23 +0,0 @@
package godog
import (
"testing"
"git.golang1.ru/softonik/godog/internal/flags"
"github.com/stretchr/testify/assert"
)
func Test_BindFlagsShouldRespectFlagDefaults(t *testing.T) {
opts := flags.Options{}
BindCommandLineFlags("flagDefaults.", &opts)
assert.Equal(t, "pretty", opts.Format)
assert.Equal(t, "", opts.Tags)
assert.Equal(t, 1, opts.Concurrency)
assert.False(t, opts.ShowStepDefinitions)
assert.False(t, opts.StopOnFailure)
assert.False(t, opts.Strict)
assert.False(t, opts.NoColors)
assert.Equal(t, int64(0), opts.Randomize)
}

534
fmt.go
Просмотреть файл

@ -1,23 +1,68 @@
package godog
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"text/template"
"time"
"unicode"
"git.golang1.ru/softonik/godog/colors"
"git.golang1.ru/softonik/godog/formatters"
internal_fmt "git.golang1.ru/softonik/godog/internal/formatters"
"git.golang1.ru/softonik/godog/internal/models"
"git.golang1.ru/softonik/godog/internal/storage"
"github.com/DATA-DOG/godog/colors"
"github.com/DATA-DOG/godog/gherkin"
)
// some snippet formatting regexps
var snippetExprCleanup = regexp.MustCompile("([\\/\\[\\]\\(\\)\\\\^\\$\\.\\|\\?\\*\\+\\'])")
var snippetExprQuoted = regexp.MustCompile("(\\W|^)\"(?:[^\"]*)\"(\\W|$)")
var snippetMethodName = regexp.MustCompile("[^a-zA-Z\\_\\ ]")
var snippetNumbers = regexp.MustCompile("(\\d+)")
var snippetHelperFuncs = template.FuncMap{
"backticked": func(s string) string {
return "`" + s + "`"
},
}
var undefinedSnippetsTpl = template.Must(template.New("snippets").Funcs(snippetHelperFuncs).Parse(`
{{ range . }}func {{ .Method }}({{ .Args }}) error {
return godog.ErrPending
}
{{end}}func FeatureContext(s *godog.Suite) { {{ range . }}
s.Step({{ backticked .Expr }}, {{ .Method }}){{end}}
}
`))
type undefinedSnippet struct {
Method string
Expr string
argument interface{} // gherkin step argument
}
type registeredFormatter struct {
name string
fmt FormatterFunc
description string
}
var formatters []*registeredFormatter
// FindFmt searches available formatters registered
// and returns FormaterFunc matched by given
// format name or nil otherwise
func FindFmt(name string) FormatterFunc {
return formatters.FindFmt(name)
for _, el := range formatters {
if el.name == name {
return el.fmt
}
}
return nil
}
// Format registers a feature suite output
@ -25,14 +70,22 @@ func FindFmt(name string) FormatterFunc {
// FormatterFunc constructor function, to initialize
// formatter with the output recorder.
func Format(name, description string, f FormatterFunc) {
formatters.Format(name, description, f)
formatters = append(formatters, &registeredFormatter{
name: name,
fmt: f,
description: description,
})
}
// AvailableFormatters gives a map of all
// formatters registered with their name as key
// and description as value
func AvailableFormatters() map[string]string {
return formatters.AvailableFormatters()
fmts := make(map[string]string, len(formatters))
for _, f := range formatters {
fmts[f.name] = f.description
}
return fmts
}
// Formatter is an interface for feature runner
@ -42,83 +95,422 @@ func AvailableFormatters() map[string]string {
// suite results in different ways. These new
// formatters needs to be registered with a
// godog.Format function call
type Formatter = formatters.Formatter
type storageFormatter interface {
SetStorage(*storage.Storage)
type Formatter interface {
Feature(*gherkin.Feature, string, []byte)
Node(interface{})
Defined(*gherkin.Step, *StepDef)
Failed(*gherkin.Step, *StepDef, error)
Passed(*gherkin.Step, *StepDef)
Skipped(*gherkin.Step, *StepDef)
Undefined(*gherkin.Step, *StepDef)
Pending(*gherkin.Step, *StepDef)
Summary()
}
// FormatterFunc builds a formatter with given
// suite name and io.Writer to record output
type FormatterFunc = formatters.FormatterFunc
type FormatterFunc func(string, io.Writer) Formatter
func printStepDefinitions(steps []*models.StepDefinition, w io.Writer) {
var longest int
for _, def := range steps {
n := utf8.RuneCountInString(def.Expr.String())
if longest < n {
longest = n
type stepType int
const (
passed stepType = iota
failed
skipped
undefined
pending
)
func (st stepType) clr() colors.ColorFunc {
switch st {
case passed:
return green
case failed:
return red
case skipped:
return cyan
default:
return yellow
}
}
func (st stepType) String() string {
switch st {
case passed:
return "passed"
case failed:
return "failed"
case skipped:
return "skipped"
case undefined:
return "undefined"
case pending:
return "pending"
default:
return "unknown"
}
}
type stepResult struct {
typ stepType
feature *feature
owner interface{}
step *gherkin.Step
def *StepDef
err error
}
func (f stepResult) line() string {
return fmt.Sprintf("%s:%d", f.feature.Path, f.step.Location.Line)
}
func (f stepResult) scenarioDesc() string {
if sc, ok := f.owner.(*gherkin.Scenario); ok {
return fmt.Sprintf("%s: %s", sc.Keyword, sc.Name)
}
if row, ok := f.owner.(*gherkin.TableRow); ok {
for _, def := range f.feature.Feature.ScenarioDefinitions {
out, ok := def.(*gherkin.ScenarioOutline)
if !ok {
continue
}
for _, ex := range out.Examples {
for _, rw := range ex.TableBody {
if rw.Location.Line == row.Location.Line {
return fmt.Sprintf("%s: %s", out.Keyword, out.Name)
}
}
}
}
}
return f.line() // was not expecting different owner
}
func (f stepResult) scenarioLine() string {
if sc, ok := f.owner.(*gherkin.Scenario); ok {
return fmt.Sprintf("%s:%d", f.feature.Path, sc.Location.Line)
}
if row, ok := f.owner.(*gherkin.TableRow); ok {
for _, def := range f.feature.Feature.ScenarioDefinitions {
out, ok := def.(*gherkin.ScenarioOutline)
if !ok {
continue
}
for _, ex := range out.Examples {
for _, rw := range ex.TableBody {
if rw.Location.Line == row.Location.Line {
return fmt.Sprintf("%s:%d", f.feature.Path, out.Location.Line)
}
}
}
}
}
return f.line() // was not expecting different owner
}
type basefmt struct {
out io.Writer
owner interface{}
indent int
started time.Time
features []*feature
failed []*stepResult
passed []*stepResult
skipped []*stepResult
undefined []*stepResult
pending []*stepResult
}
func (f *basefmt) Node(n interface{}) {
switch t := n.(type) {
case *gherkin.TableRow:
f.owner = t
case *gherkin.Scenario:
f.owner = t
}
}
func (f *basefmt) Defined(*gherkin.Step, *StepDef) {
}
func (f *basefmt) Feature(ft *gherkin.Feature, p string, c []byte) {
f.features = append(f.features, &feature{Path: p, Feature: ft})
}
func (f *basefmt) Passed(step *gherkin.Step, match *StepDef) {
s := &stepResult{
owner: f.owner,
feature: f.features[len(f.features)-1],
step: step,
def: match,
typ: passed,
}
f.passed = append(f.passed, s)
}
func (f *basefmt) Skipped(step *gherkin.Step, match *StepDef) {
s := &stepResult{
owner: f.owner,
feature: f.features[len(f.features)-1],
step: step,
def: match,
typ: skipped,
}
f.skipped = append(f.skipped, s)
}
func (f *basefmt) Undefined(step *gherkin.Step, match *StepDef) {
s := &stepResult{
owner: f.owner,
feature: f.features[len(f.features)-1],
step: step,
def: match,
typ: undefined,
}
f.undefined = append(f.undefined, s)
}
func (f *basefmt) Failed(step *gherkin.Step, match *StepDef, err error) {
s := &stepResult{
owner: f.owner,
feature: f.features[len(f.features)-1],
step: step,
def: match,
err: err,
typ: failed,
}
f.failed = append(f.failed, s)
}
func (f *basefmt) Pending(step *gherkin.Step, match *StepDef) {
s := &stepResult{
owner: f.owner,
feature: f.features[len(f.features)-1],
step: step,
def: match,
typ: pending,
}
f.pending = append(f.pending, s)
}
func (f *basefmt) Summary() {
var total, passed, undefined int
for _, ft := range f.features {
for _, def := range ft.ScenarioDefinitions {
switch t := def.(type) {
case *gherkin.Scenario:
total++
case *gherkin.ScenarioOutline:
for _, ex := range t.Examples {
if examples, hasExamples := examples(ex); hasExamples {
total += len(examples.TableBody)
}
}
}
}
}
passed = total
var owner interface{}
for _, undef := range f.undefined {
if owner != undef.owner {
undefined++
owner = undef.owner
}
}
for _, def := range steps {
n := utf8.RuneCountInString(def.Expr.String())
location := internal_fmt.DefinitionID(def)
spaces := strings.Repeat(" ", longest-n)
fmt.Fprintln(w,
colors.Yellow(def.Expr.String())+spaces,
colors.Bold(colors.Black)("# "+location))
var steps, parts, scenarios []string
nsteps := len(f.passed) + len(f.failed) + len(f.skipped) + len(f.undefined) + len(f.pending)
if len(f.passed) > 0 {
steps = append(steps, green(fmt.Sprintf("%d passed", len(f.passed))))
}
if len(f.failed) > 0 {
passed -= len(f.failed)
parts = append(parts, red(fmt.Sprintf("%d failed", len(f.failed))))
steps = append(steps, parts[len(parts)-1])
}
if len(f.pending) > 0 {
passed -= len(f.pending)
parts = append(parts, yellow(fmt.Sprintf("%d pending", len(f.pending))))
steps = append(steps, yellow(fmt.Sprintf("%d pending", len(f.pending))))
}
if len(f.undefined) > 0 {
passed -= undefined
parts = append(parts, yellow(fmt.Sprintf("%d undefined", undefined)))
steps = append(steps, yellow(fmt.Sprintf("%d undefined", len(f.undefined))))
}
if len(f.skipped) > 0 {
steps = append(steps, cyan(fmt.Sprintf("%d skipped", len(f.skipped))))
}
if passed > 0 {
scenarios = append(scenarios, green(fmt.Sprintf("%d passed", passed)))
}
scenarios = append(scenarios, parts...)
elapsed := timeNowFunc().Sub(f.started)
fmt.Fprintln(f.out, "")
if total == 0 {
fmt.Fprintln(f.out, "No scenarios")
} else {
fmt.Fprintln(f.out, fmt.Sprintf("%d scenarios (%s)", total, strings.Join(scenarios, ", ")))
}
if len(steps) == 0 {
fmt.Fprintln(w, "there were no contexts registered, could not find any step definition..")
if nsteps == 0 {
fmt.Fprintln(f.out, "No steps")
} else {
fmt.Fprintln(f.out, fmt.Sprintf("%d steps (%s)", nsteps, strings.Join(steps, ", ")))
}
fmt.Fprintln(f.out, elapsed)
// prints used randomization seed
seed, err := strconv.ParseInt(os.Getenv("GODOG_SEED"), 10, 64)
if err == nil && seed != 0 {
fmt.Fprintln(f.out, "")
fmt.Fprintln(f.out, "Randomized with seed:", colors.Yellow(seed))
}
if text := f.snippets(); text != "" {
fmt.Fprintln(f.out, yellow("\nYou can implement step definitions for undefined steps with these snippets:"))
fmt.Fprintln(f.out, yellow(text))
}
}
// NewBaseFmt creates a new base formatter.
func NewBaseFmt(suite string, out io.Writer) *BaseFmt {
return internal_fmt.NewBase(suite, out)
func (s *undefinedSnippet) Args() (ret string) {
var args []string
var pos, idx int
var breakLoop bool
for !breakLoop {
part := s.Expr[pos:]
ipos := strings.Index(part, "(\\d+)")
spos := strings.Index(part, "\"([^\"]*)\"")
switch {
case spos == -1 && ipos == -1:
breakLoop = true
case spos == -1:
idx++
pos += ipos + len("(\\d+)")
args = append(args, reflect.Int.String())
case ipos == -1:
idx++
pos += spos + len("\"([^\"]*)\"")
args = append(args, reflect.String.String())
case ipos < spos:
idx++
pos += ipos + len("(\\d+)")
args = append(args, reflect.Int.String())
case spos < ipos:
idx++
pos += spos + len("\"([^\"]*)\"")
args = append(args, reflect.String.String())
}
}
if s.argument != nil {
idx++
switch s.argument.(type) {
case *gherkin.DocString:
args = append(args, "*gherkin.DocString")
case *gherkin.DataTable:
args = append(args, "*gherkin.DataTable")
}
}
var last string
for i, arg := range args {
if last == "" || last == arg {
ret += fmt.Sprintf("arg%d, ", i+1)
} else {
ret = strings.TrimRight(ret, ", ") + fmt.Sprintf(" %s, arg%d, ", last, i+1)
}
last = arg
}
return strings.TrimSpace(strings.TrimRight(ret, ", ") + " " + last)
}
// NewProgressFmt creates a new progress formatter.
func NewProgressFmt(suite string, out io.Writer) *ProgressFmt {
return internal_fmt.NewProgress(suite, out)
func (f *basefmt) snippets() string {
if len(f.undefined) == 0 {
return ""
}
var index int
var snips []*undefinedSnippet
// build snippets
for _, u := range f.undefined {
steps := []string{u.step.Text}
arg := u.step.Argument
if u.def != nil {
steps = u.def.undefined
arg = nil
}
for _, step := range steps {
expr := snippetExprCleanup.ReplaceAllString(step, "\\$1")
expr = snippetNumbers.ReplaceAllString(expr, "(\\d+)")
expr = snippetExprQuoted.ReplaceAllString(expr, "$1\"([^\"]*)\"$2")
expr = "^" + strings.TrimSpace(expr) + "$"
name := snippetNumbers.ReplaceAllString(step, " ")
name = snippetExprQuoted.ReplaceAllString(name, " ")
name = strings.TrimSpace(snippetMethodName.ReplaceAllString(name, ""))
var words []string
for i, w := range strings.Split(name, " ") {
switch {
case i != 0:
w = strings.Title(w)
case len(w) > 0:
w = string(unicode.ToLower(rune(w[0]))) + w[1:]
}
words = append(words, w)
}
name = strings.Join(words, "")
if len(name) == 0 {
index++
name = fmt.Sprintf("stepDefinition%d", index)
}
var found bool
for _, snip := range snips {
if snip.Expr == expr {
found = true
break
}
}
if !found {
snips = append(snips, &undefinedSnippet{Method: name, Expr: expr, argument: arg})
}
}
}
var buf bytes.Buffer
if err := undefinedSnippetsTpl.Execute(&buf, snips); err != nil {
panic(err)
}
// there may be trailing spaces
return strings.Replace(buf.String(), " \n", "\n", -1)
}
// NewPrettyFmt creates a new pretty formatter.
func NewPrettyFmt(suite string, out io.Writer) *PrettyFmt {
return &PrettyFmt{Base: NewBaseFmt(suite, out)}
func (f *basefmt) isLastStep(s *gherkin.Step) bool {
ft := f.features[len(f.features)-1]
for _, def := range ft.ScenarioDefinitions {
if outline, ok := def.(*gherkin.ScenarioOutline); ok {
for n, step := range outline.Steps {
if step.Location.Line == s.Location.Line {
return n == len(outline.Steps)-1
}
}
}
if scenario, ok := def.(*gherkin.Scenario); ok {
for n, step := range scenario.Steps {
if step.Location.Line == s.Location.Line {
return n == len(scenario.Steps)-1
}
}
}
}
return false
}
// NewEventsFmt creates a new event streaming formatter.
func NewEventsFmt(suite string, out io.Writer) *EventsFmt {
return &EventsFmt{Base: NewBaseFmt(suite, out)}
}
// NewCukeFmt creates a new Cucumber JSON formatter.
func NewCukeFmt(suite string, out io.Writer) *CukeFmt {
return &CukeFmt{Base: NewBaseFmt(suite, out)}
}
// NewJUnitFmt creates a new JUnit formatter.
func NewJUnitFmt(suite string, out io.Writer) *JUnitFmt {
return &JUnitFmt{Base: NewBaseFmt(suite, out)}
}
// BaseFmt exports Base formatter.
type BaseFmt = internal_fmt.Base
// ProgressFmt exports Progress formatter.
type ProgressFmt = internal_fmt.Progress
// PrettyFmt exports Pretty formatter.
type PrettyFmt = internal_fmt.Pretty
// EventsFmt exports Events formatter.
type EventsFmt = internal_fmt.Events
// CukeFmt exports Cucumber JSON formatter.
type CukeFmt = internal_fmt.Cuke
// JUnitFmt exports JUnit formatter.
type JUnitFmt = internal_fmt.JUnit

332
fmt_cucumber.go Обычный файл
Просмотреть файл

@ -0,0 +1,332 @@
package godog
/*
The specification for the formatting originated from https://www.relishapp.com/cucumber/cucumber/docs/formatters/json-output-formatter.
I found that documentation was misleading or out dated. To validate formatting I create a ruby cucumber test harness and ran the
same feature files through godog and the ruby cucumber.
The docstrings in the cucumber.feature represent the cucumber output for those same feature definitions.
I did note that comments in ruby could be at just about any level in particular Feature, Scenario and Step. In godog I
could only find comments under the Feature data structure.
*/
import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/DATA-DOG/godog/gherkin"
)
func init() {
Format("cucumber", "Produces cucumber JSON format output.", cucumberFunc)
}
func cucumberFunc(suite string, out io.Writer) Formatter {
formatter := &cukefmt{
basefmt: basefmt{
started: timeNowFunc(),
indent: 2,
out: out,
},
}
return formatter
}
// Replace spaces with - This function is used to create the "id" fields of the cucumber output.
func makeID(name string) string {
return strings.Replace(strings.ToLower(name), " ", "-", -1)
}
// The sequence of type structs are used to marshall the json object.
type cukeComment struct {
Value string `json:"value"`
Line int `json:"line"`
}
type cukeDocstring struct {
Value string `json:"value"`
ContentType string `json:"content_type"`
Line int `json:"line"`
}
type cukeTag struct {
Name string `json:"name"`
Line int `json:"line"`
}
type cukeResult struct {
Status string `json:"status"`
Error string `json:"error_message,omitempty"`
Duration *int `json:"duration,omitempty"`
}
type cukeMatch struct {
Location string `json:"location"`
}
type cukeStep struct {
Keyword string `json:"keyword"`
Name string `json:"name"`
Line int `json:"line"`
Docstring *cukeDocstring `json:"doc_string,omitempty"`
Match cukeMatch `json:"match"`
Result cukeResult `json:"result"`
}
type cukeElement struct {
ID string `json:"id"`
Keyword string `json:"keyword"`
Name string `json:"name"`
Description string `json:"description"`
Line int `json:"line"`
Type string `json:"type"`
Tags []cukeTag `json:"tags,omitempty"`
Steps []cukeStep `json:"steps,omitempty"`
}
type cukeFeatureJSON struct {
URI string `json:"uri"`
ID string `json:"id"`
Keyword string `json:"keyword"`
Name string `json:"name"`
Description string `json:"description"`
Line int `json:"line"`
Comments []cukeComment `json:"comments,omitempty"`
Tags []cukeTag `json:"tags,omitempty"`
Elements []cukeElement `json:"elements,omitempty"`
}
type cukefmt struct {
basefmt
// currently running feature path, to be part of id.
// this is sadly not passed by gherkin nodes.
// it restricts this formatter to run only in synchronous single
// threaded execution. Unless running a copy of formatter for each feature
path string
stat stepType // last step status, before skipped
outlineSteps int // number of current outline scenario steps
ID string // current test id.
results []cukeFeatureJSON // structure that represent cuke results
curStep *cukeStep // track the current step
curElement *cukeElement // track the current element
curFeature *cukeFeatureJSON // track the current feature
curOutline cukeElement // Each example show up as an outline element but the outline is parsed only once
// so I need to keep track of the current outline
curRow int // current row of the example table as it is being processed.
curExampleTags []cukeTag // temporary storage for tags associate with the current example table.
startTime time.Time // used to time duration of the step execution
curExampleName string // Due to the fact that examples are parsed once and then iterated over for each result then we need to keep track
// of the example name inorder to build id fields.
}
func (f *cukefmt) Node(n interface{}) {
f.basefmt.Node(n)
switch t := n.(type) {
// When the example definition is seen we just need track the id and
// append the name associated with the example as part of the id.
case *gherkin.Examples:
f.curExampleName = makeID(t.Name)
f.curRow = 2 // there can be more than one example set per outline so reset row count.
// cucumber counts the header row as an example when creating the id.
// store any example level tags in a temp location.
f.curExampleTags = make([]cukeTag, len(t.Tags))
for idx, element := range t.Tags {
f.curExampleTags[idx].Line = element.Location.Line
f.curExampleTags[idx].Name = element.Name
}
// The outline node creates a placeholder and the actual element is added as each TableRow is processed.
case *gherkin.ScenarioOutline:
f.curOutline = cukeElement{}
f.curOutline.Name = t.Name
f.curOutline.Line = t.Location.Line
f.curOutline.Description = t.Description
f.curOutline.Keyword = t.Keyword
f.curOutline.ID = f.curFeature.ID + ";" + makeID(t.Name)
f.curOutline.Type = "scenario"
f.curOutline.Tags = make([]cukeTag, len(t.Tags)+len(f.curFeature.Tags))
// apply feature level tags
if len(f.curOutline.Tags) > 0 {
copy(f.curOutline.Tags, f.curFeature.Tags)
// apply outline level tags.
for idx, element := range t.Tags {
f.curOutline.Tags[idx+len(f.curFeature.Tags)].Line = element.Location.Line
f.curOutline.Tags[idx+len(f.curFeature.Tags)].Name = element.Name
}
}
// This scenario adds the element to the output immediately.
case *gherkin.Scenario:
f.curFeature.Elements = append(f.curFeature.Elements, cukeElement{})
f.curElement = &f.curFeature.Elements[len(f.curFeature.Elements)-1]
f.curElement.Name = t.Name
f.curElement.Line = t.Location.Line
f.curElement.Description = t.Description
f.curElement.Keyword = t.Keyword
f.curElement.ID = f.curFeature.ID + ";" + makeID(t.Name)
f.curElement.Type = "scenario"
f.curElement.Tags = make([]cukeTag, len(t.Tags)+len(f.curFeature.Tags))
if len(f.curElement.Tags) > 0 {
// apply feature level tags
copy(f.curElement.Tags, f.curFeature.Tags)
// apply scenario level tags.
for idx, element := range t.Tags {
f.curElement.Tags[idx+len(f.curFeature.Tags)].Line = element.Location.Line
f.curElement.Tags[idx+len(f.curFeature.Tags)].Name = element.Name
}
}
// This is an outline scenario and the element is added to the output as
// the TableRows are encountered.
case *gherkin.TableRow:
tmpElem := f.curOutline
tmpElem.Line = t.Location.Line
tmpElem.ID = tmpElem.ID + ";" + f.curExampleName + ";" + strconv.Itoa(f.curRow)
f.curRow++
f.curFeature.Elements = append(f.curFeature.Elements, tmpElem)
f.curElement = &f.curFeature.Elements[len(f.curFeature.Elements)-1]
// copy in example level tags.
f.curElement.Tags = append(f.curElement.Tags, f.curExampleTags...)
}
}
func (f *cukefmt) Feature(ft *gherkin.Feature, p string, c []byte) {
f.basefmt.Feature(ft, p, c)
f.path = p
f.ID = makeID(ft.Name)
f.results = append(f.results, cukeFeatureJSON{})
f.curFeature = &f.results[len(f.results)-1]
f.curFeature.URI = p
f.curFeature.Name = ft.Name
f.curFeature.Keyword = ft.Keyword
f.curFeature.Line = ft.Location.Line
f.curFeature.Description = ft.Description
f.curFeature.ID = f.ID
f.curFeature.Tags = make([]cukeTag, len(ft.Tags))
for idx, element := range ft.Tags {
f.curFeature.Tags[idx].Line = element.Location.Line
f.curFeature.Tags[idx].Name = element.Name
}
f.curFeature.Comments = make([]cukeComment, len(ft.Comments))
for idx, comment := range ft.Comments {
f.curFeature.Comments[idx].Value = strings.TrimSpace(comment.Text)
f.curFeature.Comments[idx].Line = comment.Location.Line
}
}
func (f *cukefmt) Summary() {
dat, err := json.MarshalIndent(f.results, "", " ")
if err != nil {
panic(err)
}
fmt.Fprintf(f.out, "%s\n", string(dat))
}
func (f *cukefmt) step(res *stepResult) {
// determine if test case has finished
switch t := f.owner.(type) {
case *gherkin.TableRow:
d := int(timeNowFunc().Sub(f.startTime).Nanoseconds())
f.curStep.Result.Duration = &d
f.curStep.Line = t.Location.Line
f.curStep.Result.Status = res.typ.String()
if res.err != nil {
f.curStep.Result.Error = res.err.Error()
}
case *gherkin.Scenario:
d := int(timeNowFunc().Sub(f.startTime).Nanoseconds())
f.curStep.Result.Duration = &d
f.curStep.Result.Status = res.typ.String()
if res.err != nil {
f.curStep.Result.Error = res.err.Error()
}
}
}
func (f *cukefmt) Defined(step *gherkin.Step, def *StepDef) {
f.startTime = timeNowFunc() // start timing the step
f.curElement.Steps = append(f.curElement.Steps, cukeStep{})
f.curStep = &f.curElement.Steps[len(f.curElement.Steps)-1]
f.curStep.Name = step.Text
f.curStep.Line = step.Location.Line
f.curStep.Keyword = step.Keyword
if _, ok := step.Argument.(*gherkin.DocString); ok {
f.curStep.Docstring = &cukeDocstring{}
f.curStep.Docstring.ContentType = strings.TrimSpace(step.Argument.(*gherkin.DocString).ContentType)
f.curStep.Docstring.Line = step.Argument.(*gherkin.DocString).Location.Line
f.curStep.Docstring.Value = step.Argument.(*gherkin.DocString).Content
}
if def != nil {
f.curStep.Match.Location = strings.Split(def.definitionID(), " ")[0]
}
}
func (f *cukefmt) Passed(step *gherkin.Step, match *StepDef) {
f.basefmt.Passed(step, match)
f.stat = passed
f.step(f.passed[len(f.passed)-1])
}
func (f *cukefmt) Skipped(step *gherkin.Step, match *StepDef) {
f.basefmt.Skipped(step, match)
f.step(f.skipped[len(f.skipped)-1])
// no duration reported for skipped.
f.curStep.Result.Duration = nil
}
func (f *cukefmt) Undefined(step *gherkin.Step, match *StepDef) {
f.basefmt.Undefined(step, match)
f.stat = undefined
f.step(f.undefined[len(f.undefined)-1])
// the location for undefined is the feature file location not the step file.
f.curStep.Match.Location = fmt.Sprintf("%s:%d", f.path, step.Location.Line)
f.curStep.Result.Duration = nil
}
func (f *cukefmt) Failed(step *gherkin.Step, match *StepDef, err error) {
f.basefmt.Failed(step, match, err)
f.stat = failed
f.step(f.failed[len(f.failed)-1])
}
func (f *cukefmt) Pending(step *gherkin.Step, match *StepDef) {
f.stat = pending
f.basefmt.Pending(step, match)
f.step(f.pending[len(f.pending)-1])
// the location for pending is the feature file location not the step file.
f.curStep.Match.Location = fmt.Sprintf("%s:%d", f.path, step.Location.Line)
f.curStep.Result.Duration = nil
}

269
fmt_events.go Обычный файл
Просмотреть файл

@ -0,0 +1,269 @@
package godog
import (
"encoding/json"
"fmt"
"io"
"github.com/DATA-DOG/godog/gherkin"
)
const nanoSec = 1000000
const spec = "0.1.0"
func init() {
Format("events", fmt.Sprintf("Produces JSON event stream, based on spec: %s.", spec), eventsFunc)
}
func eventsFunc(suite string, out io.Writer) Formatter {
formatter := &events{
basefmt: basefmt{
started: timeNowFunc(),
indent: 2,
out: out,
},
}
formatter.event(&struct {
Event string `json:"event"`
Version string `json:"version"`
Timestamp int64 `json:"timestamp"`
Suite string `json:"suite"`
}{
"TestRunStarted",
spec,
timeNowFunc().UnixNano() / nanoSec,
suite,
})
return formatter
}
type events struct {
basefmt
// currently running feature path, to be part of id.
// this is sadly not passed by gherkin nodes.
// it restricts this formatter to run only in synchronous single
// threaded execution. Unless running a copy of formatter for each feature
path string
stat stepType // last step status, before skipped
outlineSteps int // number of current outline scenario steps
}
func (f *events) event(ev interface{}) {
data, err := json.Marshal(ev)
if err != nil {
panic(fmt.Sprintf("failed to marshal stream event: %+v - %v", ev, err))
}
fmt.Fprintln(f.out, string(data))
}
func (f *events) Node(n interface{}) {
f.basefmt.Node(n)
var id string
var undefined bool
switch t := n.(type) {
case *gherkin.Scenario:
id = fmt.Sprintf("%s:%d", f.path, t.Location.Line)
undefined = len(t.Steps) == 0
case *gherkin.TableRow:
id = fmt.Sprintf("%s:%d", f.path, t.Location.Line)
undefined = f.outlineSteps == 0
case *gherkin.ScenarioOutline:
f.outlineSteps = len(t.Steps)
}
if len(id) == 0 {
return
}
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
Timestamp int64 `json:"timestamp"`
}{
"TestCaseStarted",
id,
timeNowFunc().UnixNano() / nanoSec,
})
if undefined {
// @TODO: is status undefined or passed? when there are no steps
// for this scenario
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
}{
"TestCaseFinished",
id,
timeNowFunc().UnixNano() / nanoSec,
"undefined",
})
}
}
func (f *events) Feature(ft *gherkin.Feature, p string, c []byte) {
f.basefmt.Feature(ft, p, c)
f.path = p
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
Source string `json:"source"`
}{
"TestSource",
fmt.Sprintf("%s:%d", p, ft.Location.Line),
string(c),
})
}
func (f *events) Summary() {
// @TODO: determine status
status := passed
if len(f.failed) > 0 {
status = failed
} else if len(f.passed) == 0 {
if len(f.undefined) > len(f.pending) {
status = undefined
} else {
status = pending
}
}
snips := f.snippets()
if len(snips) > 0 {
snips = "You can implement step definitions for undefined steps with these snippets:\n" + snips
}
f.event(&struct {
Event string `json:"event"`
Status string `json:"status"`
Timestamp int64 `json:"timestamp"`
Snippets string `json:"snippets"`
Memory string `json:"memory"`
}{
"TestRunFinished",
status.String(),
timeNowFunc().UnixNano() / nanoSec,
snips,
"", // @TODO not sure that could be correctly implemented
})
}
func (f *events) step(res *stepResult) {
var errMsg string
if res.err != nil {
errMsg = res.err.Error()
}
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
Summary string `json:"summary,omitempty"`
}{
"TestStepFinished",
fmt.Sprintf("%s:%d", f.path, res.step.Location.Line),
timeNowFunc().UnixNano() / nanoSec,
res.typ.String(),
errMsg,
})
// determine if test case has finished
var finished bool
var line int
switch t := f.owner.(type) {
case *gherkin.TableRow:
line = t.Location.Line
finished = f.isLastStep(res.step)
case *gherkin.Scenario:
line = t.Location.Line
finished = f.isLastStep(res.step)
}
if finished {
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
}{
"TestCaseFinished",
fmt.Sprintf("%s:%d", f.path, line),
timeNowFunc().UnixNano() / nanoSec,
f.stat.String(),
})
}
}
func (f *events) Defined(step *gherkin.Step, def *StepDef) {
if def != nil {
m := def.Expr.FindStringSubmatchIndex(step.Text)[2:]
var args [][2]int
for i := 0; i < len(m)/2; i++ {
pair := m[i : i*2+2]
var idxs [2]int
idxs[0] = pair[0]
idxs[1] = pair[1]
args = append(args, idxs)
}
if len(args) == 0 {
args = make([][2]int, 0)
}
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
DefID string `json:"definition_id"`
Args [][2]int `json:"arguments"`
}{
"StepDefinitionFound",
fmt.Sprintf("%s:%d", f.path, step.Location.Line),
def.definitionID(),
args,
})
}
f.event(&struct {
Event string `json:"event"`
Location string `json:"location"`
Timestamp int64 `json:"timestamp"`
}{
"TestStepStarted",
fmt.Sprintf("%s:%d", f.path, step.Location.Line),
timeNowFunc().UnixNano() / nanoSec,
})
}
func (f *events) Passed(step *gherkin.Step, match *StepDef) {
f.basefmt.Passed(step, match)
f.stat = passed
f.step(f.passed[len(f.passed)-1])
}
func (f *events) Skipped(step *gherkin.Step, match *StepDef) {
f.basefmt.Skipped(step, match)
f.step(f.skipped[len(f.skipped)-1])
}
func (f *events) Undefined(step *gherkin.Step, match *StepDef) {
f.basefmt.Undefined(step, match)
f.stat = undefined
f.step(f.undefined[len(f.undefined)-1])
}
func (f *events) Failed(step *gherkin.Step, match *StepDef, err error) {
f.basefmt.Failed(step, match, err)
f.stat = failed
f.step(f.failed[len(f.failed)-1])
}
func (f *events) Pending(step *gherkin.Step, match *StepDef) {
f.stat = pending
f.basefmt.Pending(step, match)
f.step(f.pending[len(f.pending)-1])
}

208
fmt_junit.go Обычный файл
Просмотреть файл

@ -0,0 +1,208 @@
package godog
import (
"encoding/xml"
"fmt"
"io"
"os"
"time"
"github.com/DATA-DOG/godog/gherkin"
)
func init() {
Format("junit", "Prints junit compatible xml to stdout", junitFunc)
}
func junitFunc(suite string, out io.Writer) Formatter {
return &junitFormatter{
suite: &junitPackageSuite{
Name: suite,
TestSuites: make([]*junitTestSuite, 0),
},
out: out,
started: timeNowFunc(),
}
}
type junitFormatter struct {
suite *junitPackageSuite
out io.Writer
// timing
started time.Time
caseStarted time.Time
featStarted time.Time
outline *gherkin.ScenarioOutline
outlineExample int
}
func (j *junitFormatter) Feature(feature *gherkin.Feature, path string, c []byte) {
testSuite := &junitTestSuite{
TestCases: make([]*junitTestCase, 0),
Name: feature.Name,
}
if len(j.suite.TestSuites) > 0 {
j.current().Time = timeNowFunc().Sub(j.featStarted).String()
}
j.featStarted = timeNowFunc()
j.suite.TestSuites = append(j.suite.TestSuites, testSuite)
}
func (j *junitFormatter) Defined(*gherkin.Step, *StepDef) {
}
func (j *junitFormatter) Node(node interface{}) {
suite := j.current()
tcase := &junitTestCase{}
switch t := node.(type) {
case *gherkin.ScenarioOutline:
j.outline = t
j.outlineExample = 0
return
case *gherkin.Scenario:
tcase.Name = t.Name
suite.Tests++
j.suite.Tests++
case *gherkin.TableRow:
j.outlineExample++
tcase.Name = fmt.Sprintf("%s #%d", j.outline.Name, j.outlineExample)
suite.Tests++
j.suite.Tests++
default:
return
}
j.caseStarted = timeNowFunc()
suite.TestCases = append(suite.TestCases, tcase)
}
func (j *junitFormatter) Failed(step *gherkin.Step, match *StepDef, err error) {
suite := j.current()
suite.Failures++
j.suite.Failures++
tcase := suite.current()
tcase.Time = timeNowFunc().Sub(j.caseStarted).String()
tcase.Status = "failed"
tcase.Failure = &junitFailure{
Message: fmt.Sprintf("%s %s: %s", step.Type, step.Text, err.Error()),
}
}
func (j *junitFormatter) Passed(step *gherkin.Step, match *StepDef) {
suite := j.current()
tcase := suite.current()
tcase.Time = timeNowFunc().Sub(j.caseStarted).String()
tcase.Status = "passed"
}
func (j *junitFormatter) Skipped(step *gherkin.Step, match *StepDef) {
suite := j.current()
tcase := suite.current()
tcase.Time = timeNowFunc().Sub(j.caseStarted).String()
tcase.Error = append(tcase.Error, &junitError{
Type: "skipped",
Message: fmt.Sprintf("%s %s", step.Type, step.Text),
})
}
func (j *junitFormatter) Undefined(step *gherkin.Step, match *StepDef) {
suite := j.current()
tcase := suite.current()
if tcase.Status != "undefined" {
// do not count two undefined steps as another error
suite.Errors++
j.suite.Errors++
}
tcase.Time = timeNowFunc().Sub(j.caseStarted).String()
tcase.Status = "undefined"
tcase.Error = append(tcase.Error, &junitError{
Type: "undefined",
Message: fmt.Sprintf("%s %s", step.Type, step.Text),
})
}
func (j *junitFormatter) Pending(step *gherkin.Step, match *StepDef) {
suite := j.current()
suite.Errors++
j.suite.Errors++
tcase := suite.current()
tcase.Time = timeNowFunc().Sub(j.caseStarted).String()
tcase.Status = "pending"
tcase.Error = append(tcase.Error, &junitError{
Type: "pending",
Message: fmt.Sprintf("%s %s: TODO: write pending definition", step.Type, step.Text),
})
}
func (j *junitFormatter) Summary() {
if j.current() != nil {
j.current().Time = timeNowFunc().Sub(j.featStarted).String()
}
j.suite.Time = timeNowFunc().Sub(j.started).String()
io.WriteString(j.out, xml.Header)
enc := xml.NewEncoder(j.out)
enc.Indent("", s(2))
if err := enc.Encode(j.suite); err != nil {
fmt.Fprintln(os.Stderr, "failed to write junit xml:", err)
}
}
type junitFailure struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr,omitempty"`
}
type junitError struct {
XMLName xml.Name `xml:"error,omitempty"`
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
}
type junitTestCase struct {
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Status string `xml:"status,attr"`
Time string `xml:"time,attr"`
Failure *junitFailure `xml:"failure,omitempty"`
Error []*junitError
}
type junitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Skipped int `xml:"skipped,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Time string `xml:"time,attr"`
TestCases []*junitTestCase
}
func (ts *junitTestSuite) current() *junitTestCase {
return ts.TestCases[len(ts.TestCases)-1]
}
type junitPackageSuite struct {
XMLName xml.Name `xml:"testsuites"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Skipped int `xml:"skipped,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Time string `xml:"time,attr"`
TestSuites []*junitTestSuite
}
func (j *junitFormatter) current() *junitTestSuite {
return j.suite.TestSuites[len(j.suite.TestSuites)-1]
}

171
fmt_junit_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,171 @@
package godog
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/DATA-DOG/godog/colors"
"github.com/DATA-DOG/godog/gherkin"
)
var sampleGherkinFeature = `
Feature: junit formatter
Background:
Given passing
Scenario: passing scenario
Then passing
Scenario: failing scenario
When failing
Then passing
Scenario: pending scenario
When pending
Then passing
Scenario: undefined scenario
When undefined
Then next undefined
Scenario Outline: outline
Given <one>
When <two>
Examples:
| one | two |
| passing | passing |
| passing | failing |
| passing | pending |
Examples:
| one | two |
| passing | undefined |
`
func TestJUnitFormatterOutput(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(sampleGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
s := &Suite{
fmt: junitFunc("junit", w),
features: []*feature{&feature{
Path: "any.feature",
Feature: feat,
Content: []byte(sampleGherkinFeature),
}},
}
s.Step(`^passing$`, func() error { return nil })
s.Step(`^failing$`, func() error { return fmt.Errorf("errored") })
s.Step(`^pending$`, func() error { return ErrPending })
var zeroDuration time.Duration
expected := junitPackageSuite{
Name: "junit",
Tests: 8,
Skipped: 0,
Failures: 2,
Errors: 4,
Time: zeroDuration.String(),
TestSuites: []*junitTestSuite{{
Name: "junit formatter",
Tests: 8,
Skipped: 0,
Failures: 2,
Errors: 4,
Time: zeroDuration.String(),
TestCases: []*junitTestCase{
{
Name: "passing scenario",
Status: "passed",
Time: zeroDuration.String(),
},
{
Name: "failing scenario",
Status: "failed",
Time: zeroDuration.String(),
Failure: &junitFailure{
Message: "Step failing: errored",
},
Error: []*junitError{
{Message: "Step passing", Type: "skipped"},
},
},
{
Name: "pending scenario",
Status: "pending",
Time: zeroDuration.String(),
Error: []*junitError{
{Message: "Step pending: TODO: write pending definition", Type: "pending"},
{Message: "Step passing", Type: "skipped"},
},
},
{
Name: "undefined scenario",
Status: "undefined",
Time: zeroDuration.String(),
Error: []*junitError{
{Message: "Step undefined", Type: "undefined"},
{Message: "Step next undefined", Type: "undefined"},
},
},
{
Name: "outline #1",
Status: "passed",
Time: zeroDuration.String(),
},
{
Name: "outline #2",
Status: "failed",
Time: zeroDuration.String(),
Failure: &junitFailure{
Message: "Step failing: errored",
},
},
{
Name: "outline #3",
Status: "pending",
Time: zeroDuration.String(),
Error: []*junitError{
{Message: "Step pending: TODO: write pending definition", Type: "pending"},
},
},
{
Name: "outline #4",
Status: "undefined",
Time: zeroDuration.String(),
Error: []*junitError{
{Message: "Step undefined", Type: "undefined"},
},
},
},
}},
}
s.run()
s.fmt.Summary()
var exp bytes.Buffer
io.WriteString(&exp, xml.Header)
enc := xml.NewEncoder(&exp)
enc.Indent("", " ")
if err := enc.Encode(expected); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if buf.String() != exp.String() {
t.Fatalf("expected output does not match: %s", buf.String())
}
}

456
fmt_pretty.go Обычный файл
Просмотреть файл

@ -0,0 +1,456 @@
package godog
import (
"fmt"
"io"
"math"
"regexp"
"strings"
"unicode/utf8"
"github.com/DATA-DOG/godog/colors"
"github.com/DATA-DOG/godog/gherkin"
)
func init() {
Format("pretty", "Prints every feature with runtime statuses.", prettyFunc)
}
func prettyFunc(suite string, out io.Writer) Formatter {
return &pretty{
basefmt: basefmt{
started: timeNowFunc(),
indent: 2,
out: out,
},
}
}
var outlinePlaceholderRegexp = regexp.MustCompile("<[^>]+>")
// a built in default pretty formatter
type pretty struct {
basefmt
// currently processed
feature *gherkin.Feature
scenario *gherkin.Scenario
outline *gherkin.ScenarioOutline
// state
bgSteps int
totalBgSteps int
steps int
commentPos int
// whether scenario or scenario outline keyword was printed
scenarioKeyword bool
// outline
outlineSteps []*stepResult
outlineNumExample int
outlineNumExamples int
}
func (f *pretty) Feature(ft *gherkin.Feature, p string, c []byte) {
if len(f.features) != 0 {
// not a first feature, add a newline
fmt.Fprintln(f.out, "")
}
f.features = append(f.features, &feature{Path: p, Feature: ft})
fmt.Fprintln(f.out, whiteb(ft.Keyword+": ")+ft.Name)
if strings.TrimSpace(ft.Description) != "" {
for _, line := range strings.Split(ft.Description, "\n") {
fmt.Fprintln(f.out, s(f.indent)+strings.TrimSpace(line))
}
}
f.feature = ft
f.scenario = nil
f.outline = nil
f.bgSteps = 0
f.totalBgSteps = 0
if ft.Background != nil {
f.bgSteps = len(ft.Background.Steps)
f.totalBgSteps = len(ft.Background.Steps)
}
}
// Node takes a gherkin node for formatting
func (f *pretty) Node(node interface{}) {
f.basefmt.Node(node)
switch t := node.(type) {
case *gherkin.Examples:
f.outlineNumExamples = len(t.TableBody)
f.outlineNumExample++
case *gherkin.Scenario:
f.scenario = t
f.outline = nil
f.steps = len(t.Steps) + f.totalBgSteps
f.scenarioKeyword = false
if isEmptyScenario(t) {
f.printUndefinedScenario(t)
}
case *gherkin.ScenarioOutline:
f.outline = t
f.scenario = nil
f.outlineNumExample = -1
f.scenarioKeyword = false
case *gherkin.TableRow:
f.steps = len(f.outline.Steps) + f.totalBgSteps
f.outlineSteps = []*stepResult{}
}
}
func (f *pretty) printUndefinedScenario(sc *gherkin.Scenario) {
if f.bgSteps > 0 {
f.commentPos = f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background))
fmt.Fprintln(f.out, "\n"+s(f.indent)+whiteb(f.feature.Background.Keyword+": "+f.feature.Background.Name))
for _, step := range f.feature.Background.Steps {
f.bgSteps--
f.printStep(step, nil, colors.Cyan)
}
}
text := s(f.indent) + whiteb(f.scenario.Keyword+": ") + sc.Name
text += s(f.commentPos-f.length(f.scenario)+1) + f.line(sc.Location)
fmt.Fprintln(f.out, "\n"+text)
}
// Summary sumarize the feature formatter output
func (f *pretty) Summary() {
if len(f.failed) > 0 {
fmt.Fprintln(f.out, "\n--- "+red("Failed steps:")+"\n")
for _, fail := range f.failed {
fmt.Fprintln(f.out, s(2)+red(fail.scenarioDesc())+black(" # "+fail.scenarioLine()))
fmt.Fprintln(f.out, s(4)+red(strings.TrimSpace(fail.step.Keyword)+" "+fail.step.Text)+black(" # "+fail.line()))
fmt.Fprintln(f.out, s(6)+red("Error: ")+redb(fmt.Sprintf("%+v", fail.err))+"\n")
}
}
f.basefmt.Summary()
}
func (f *pretty) printOutlineExample(outline *gherkin.ScenarioOutline) {
var msg string
var clr colors.ColorFunc
ex := outline.Examples[f.outlineNumExample]
example, hasExamples := examples(ex)
if !hasExamples {
// do not print empty examples
return
}
firstExample := f.outlineNumExamples == len(example.TableBody)
printSteps := firstExample && f.outlineNumExample == 0
for i, res := range f.outlineSteps {
// determine example row status
switch {
case res.typ == failed:
msg = res.err.Error()
clr = res.typ.clr()
case res.typ == undefined || res.typ == pending:
clr = res.typ.clr()
case res.typ == skipped && clr == nil:
clr = cyan
}
if printSteps && i >= f.totalBgSteps {
// in first example, we need to print steps
var text string
ostep := outline.Steps[i-f.totalBgSteps]
if res.def != nil {
if m := outlinePlaceholderRegexp.FindAllStringIndex(ostep.Text, -1); len(m) > 0 {
var pos int
for i := 0; i < len(m); i++ {
pair := m[i]
text += cyan(ostep.Text[pos:pair[0]])
text += cyanb(ostep.Text[pair[0]:pair[1]])
pos = pair[1]
}
text += cyan(ostep.Text[pos:len(ostep.Text)])
} else {
text = cyan(ostep.Text)
}
text += s(f.commentPos-f.length(ostep)+1) + black(fmt.Sprintf("# %s", res.def.definitionID()))
} else {
text = cyan(ostep.Text)
}
// print the step outline
fmt.Fprintln(f.out, s(f.indent*2)+cyan(strings.TrimSpace(ostep.Keyword))+" "+text)
// print step argument
// @TODO: need to make example header cells bold
switch t := ostep.Argument.(type) {
case *gherkin.DataTable:
f.printTable(t, cyan)
case *gherkin.DocString:
var ct string
if len(t.ContentType) > 0 {
ct = " " + cyan(t.ContentType)
}
fmt.Fprintln(f.out, s(f.indent*3)+cyan(t.Delimitter)+ct)
for _, ln := range strings.Split(t.Content, "\n") {
fmt.Fprintln(f.out, s(f.indent*3)+cyan(ln))
}
fmt.Fprintln(f.out, s(f.indent*3)+cyan(t.Delimitter))
}
}
}
if clr == nil {
clr = green
}
cells := make([]string, len(example.TableHeader.Cells))
max := longest(example, clr, cyan)
// an example table header
if firstExample {
fmt.Fprintln(f.out, "")
fmt.Fprintln(f.out, s(f.indent*2)+whiteb(example.Keyword+": ")+example.Name)
for i, cell := range example.TableHeader.Cells {
val := cyan(cell.Value)
ln := utf8.RuneCountInString(val)
cells[i] = val + s(max[i]-ln)
}
fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cells, " | ")+" |")
}
// an example table row
row := example.TableBody[len(example.TableBody)-f.outlineNumExamples]
for i, cell := range row.Cells {
val := clr(cell.Value)
ln := utf8.RuneCountInString(val)
cells[i] = val + s(max[i]-ln)
}
fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cells, " | ")+" |")
// if there is an error
if msg != "" {
fmt.Fprintln(f.out, s(f.indent*4)+redb(msg))
}
}
func (f *pretty) printStep(step *gherkin.Step, def *StepDef, c colors.ColorFunc) {
text := s(f.indent*2) + c(strings.TrimSpace(step.Keyword)) + " "
switch {
case def != nil:
if m := def.Expr.FindStringSubmatchIndex(step.Text)[2:]; len(m) > 0 {
var pos, i int
for pos, i = 0, 0; i < len(m); i++ {
if m[i] == -1 {
continue // no index for this match
}
if math.Mod(float64(i), 2) == 0 {
text += c(step.Text[pos:m[i]])
} else {
text += colors.Bold(c)(step.Text[pos:m[i]])
}
pos = m[i]
}
text += c(step.Text[pos:len(step.Text)])
} else {
text += c(step.Text)
}
text += s(f.commentPos-f.length(step)+1) + black(fmt.Sprintf("# %s", def.definitionID()))
default:
text += c(step.Text)
}
fmt.Fprintln(f.out, text)
switch t := step.Argument.(type) {
case *gherkin.DataTable:
f.printTable(t, c)
case *gherkin.DocString:
var ct string
if len(t.ContentType) > 0 {
ct = " " + c(t.ContentType)
}
fmt.Fprintln(f.out, s(f.indent*3)+c(t.Delimitter)+ct)
for _, ln := range strings.Split(t.Content, "\n") {
fmt.Fprintln(f.out, s(f.indent*3)+c(ln))
}
fmt.Fprintln(f.out, s(f.indent*3)+c(t.Delimitter))
}
}
func (f *pretty) printStepKind(res *stepResult) {
f.steps--
if f.outline != nil {
f.outlineSteps = append(f.outlineSteps, res)
}
var bgStep bool
// if has not printed background yet
switch {
// first background step
case f.bgSteps > 0 && f.bgSteps == len(f.feature.Background.Steps):
f.commentPos = f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background))
fmt.Fprintln(f.out, "\n"+s(f.indent)+whiteb(f.feature.Background.Keyword+": "+f.feature.Background.Name))
f.bgSteps--
bgStep = true
// subsequent background steps
case f.bgSteps > 0:
f.bgSteps--
bgStep = true
// first step of scenario, print header and calculate comment position
case f.scenario != nil:
// print scenario keyword and value if first example
if !f.scenarioKeyword {
f.commentPos = f.longestStep(f.scenario.Steps, f.length(f.scenario))
if f.feature.Background != nil {
if bgLen := f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background)); bgLen > f.commentPos {
f.commentPos = bgLen
}
}
text := s(f.indent) + whiteb(f.scenario.Keyword+": ") + f.scenario.Name
text += s(f.commentPos-f.length(f.scenario)+1) + f.line(f.scenario.Location)
fmt.Fprintln(f.out, "\n"+text)
f.scenarioKeyword = true
}
// first step of outline scenario, print header and calculate comment position
case f.outline != nil:
// print scenario keyword and value if first example
if !f.scenarioKeyword {
f.commentPos = f.longestStep(f.outline.Steps, f.length(f.outline))
if f.feature.Background != nil {
if bgLen := f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background)); bgLen > f.commentPos {
f.commentPos = bgLen
}
}
text := s(f.indent) + whiteb(f.outline.Keyword+": ") + f.outline.Name
text += s(f.commentPos-f.length(f.outline)+1) + f.line(f.outline.Location)
fmt.Fprintln(f.out, "\n"+text)
f.scenarioKeyword = true
}
if len(f.outlineSteps) == len(f.outline.Steps)+f.totalBgSteps {
// an outline example steps has went through
f.printOutlineExample(f.outline)
f.outlineNumExamples--
}
return
}
if !f.isBackgroundStep(res.step) || bgStep {
f.printStep(res.step, res.def, res.typ.clr())
}
if res.err != nil {
fmt.Fprintln(f.out, s(f.indent*2)+redb(fmt.Sprintf("%+v", res.err)))
}
if res.typ == pending {
fmt.Fprintln(f.out, s(f.indent*3)+yellow("TODO: write pending definition"))
}
}
func (f *pretty) isBackgroundStep(step *gherkin.Step) bool {
if f.feature.Background == nil {
return false
}
for _, bstep := range f.feature.Background.Steps {
if bstep.Location.Line == step.Location.Line {
return true
}
}
return false
}
// print table with aligned table cells
func (f *pretty) printTable(t *gherkin.DataTable, c colors.ColorFunc) {
var l = longest(t, c)
var cols = make([]string, len(t.Rows[0].Cells))
for _, row := range t.Rows {
for i, cell := range row.Cells {
val := c(cell.Value)
ln := utf8.RuneCountInString(val)
cols[i] = val + s(l[i]-ln)
}
fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cols, " | ")+" |")
}
}
func (f *pretty) Passed(step *gherkin.Step, match *StepDef) {
f.basefmt.Passed(step, match)
f.printStepKind(f.passed[len(f.passed)-1])
}
func (f *pretty) Skipped(step *gherkin.Step, match *StepDef) {
f.basefmt.Skipped(step, match)
f.printStepKind(f.skipped[len(f.skipped)-1])
}
func (f *pretty) Undefined(step *gherkin.Step, match *StepDef) {
f.basefmt.Undefined(step, match)
f.printStepKind(f.undefined[len(f.undefined)-1])
}
func (f *pretty) Failed(step *gherkin.Step, match *StepDef, err error) {
f.basefmt.Failed(step, match, err)
f.printStepKind(f.failed[len(f.failed)-1])
}
func (f *pretty) Pending(step *gherkin.Step, match *StepDef) {
f.basefmt.Pending(step, match)
f.printStepKind(f.pending[len(f.pending)-1])
}
// longest gives a list of longest columns of all rows in Table
func longest(tbl interface{}, clrs ...colors.ColorFunc) []int {
var rows []*gherkin.TableRow
switch t := tbl.(type) {
case *gherkin.Examples:
rows = append(rows, t.TableHeader)
rows = append(rows, t.TableBody...)
case *gherkin.DataTable:
rows = append(rows, t.Rows...)
}
longest := make([]int, len(rows[0].Cells))
for _, row := range rows {
for i, cell := range row.Cells {
for _, c := range clrs {
ln := utf8.RuneCountInString(c(cell.Value))
if longest[i] < ln {
longest[i] = ln
}
}
ln := utf8.RuneCountInString(cell.Value)
if longest[i] < ln {
longest[i] = ln
}
}
}
return longest
}
func (f *pretty) longestStep(steps []*gherkin.Step, base int) int {
ret := base
for _, step := range steps {
length := f.length(step)
if length > ret {
ret = length
}
}
return ret
}
// a line number representation in feature file
func (f *pretty) line(loc *gherkin.Location) string {
return black(fmt.Sprintf("# %s:%d", f.features[len(f.features)-1].Path, loc.Line))
}
func (f *pretty) length(node interface{}) int {
switch t := node.(type) {
case *gherkin.Background:
return f.indent + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+": "+t.Name)
case *gherkin.Step:
return f.indent*2 + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+" "+t.Text)
case *gherkin.Scenario:
return f.indent + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+": "+t.Name)
case *gherkin.ScenarioOutline:
return f.indent + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+": "+t.Name)
}
panic(fmt.Sprintf("unexpected node %T to determine length", node))
}

121
fmt_progress.go Обычный файл
Просмотреть файл

@ -0,0 +1,121 @@
package godog
import (
"fmt"
"io"
"math"
"strings"
"sync"
"github.com/DATA-DOG/godog/gherkin"
)
func init() {
Format("progress", "Prints a character per step.", progressFunc)
}
func progressFunc(suite string, out io.Writer) Formatter {
return &progress{
basefmt: basefmt{
started: timeNowFunc(),
indent: 2,
out: out,
},
stepsPerRow: 70,
}
}
type progress struct {
basefmt
sync.Mutex
stepsPerRow int
steps int
}
func (f *progress) Node(n interface{}) {
f.Lock()
defer f.Unlock()
f.basefmt.Node(n)
}
func (f *progress) Feature(ft *gherkin.Feature, p string, c []byte) {
f.Lock()
defer f.Unlock()
f.basefmt.Feature(ft, p, c)
}
func (f *progress) Summary() {
left := math.Mod(float64(f.steps), float64(f.stepsPerRow))
if left != 0 {
if int(f.steps) > f.stepsPerRow {
fmt.Fprintf(f.out, s(f.stepsPerRow-int(left))+fmt.Sprintf(" %d\n", f.steps))
} else {
fmt.Fprintf(f.out, " %d\n", f.steps)
}
}
fmt.Fprintln(f.out, "")
if len(f.failed) > 0 {
fmt.Fprintln(f.out, "\n--- "+red("Failed steps:")+"\n")
for _, fail := range f.failed {
fmt.Fprintln(f.out, s(2)+red(fail.scenarioDesc())+black(" # "+fail.scenarioLine()))
fmt.Fprintln(f.out, s(4)+red(strings.TrimSpace(fail.step.Keyword)+" "+fail.step.Text)+black(" # "+fail.line()))
fmt.Fprintln(f.out, s(6)+red("Error: ")+redb(fmt.Sprintf("%+v", fail.err))+"\n")
}
}
f.basefmt.Summary()
}
func (f *progress) step(res *stepResult) {
switch res.typ {
case passed:
fmt.Fprint(f.out, green("."))
case skipped:
fmt.Fprint(f.out, cyan("-"))
case failed:
fmt.Fprint(f.out, red("F"))
case undefined:
fmt.Fprint(f.out, yellow("U"))
case pending:
fmt.Fprint(f.out, yellow("P"))
}
f.steps++
if math.Mod(float64(f.steps), float64(f.stepsPerRow)) == 0 {
fmt.Fprintf(f.out, " %d\n", f.steps)
}
}
func (f *progress) Passed(step *gherkin.Step, match *StepDef) {
f.Lock()
defer f.Unlock()
f.basefmt.Passed(step, match)
f.step(f.passed[len(f.passed)-1])
}
func (f *progress) Skipped(step *gherkin.Step, match *StepDef) {
f.Lock()
defer f.Unlock()
f.basefmt.Skipped(step, match)
f.step(f.skipped[len(f.skipped)-1])
}
func (f *progress) Undefined(step *gherkin.Step, match *StepDef) {
f.Lock()
defer f.Unlock()
f.basefmt.Undefined(step, match)
f.step(f.undefined[len(f.undefined)-1])
}
func (f *progress) Failed(step *gherkin.Step, match *StepDef, err error) {
f.Lock()
defer f.Unlock()
f.basefmt.Failed(step, match, err)
f.step(f.failed[len(f.failed)-1])
}
func (f *progress) Pending(step *gherkin.Step, match *StepDef) {
f.Lock()
defer f.Unlock()
f.basefmt.Pending(step, match)
f.step(f.pending[len(f.pending)-1])
}

411
fmt_progress_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,411 @@
package godog
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
"github.com/DATA-DOG/godog/colors"
"github.com/DATA-DOG/godog/gherkin"
)
func TestProgressFormatterOutput(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(sampleGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{
Path: "any.feature",
Feature: feat,
Content: []byte(sampleGherkinFeature),
}},
initializer: func(s *Suite) {
s.Step(`^passing$`, func() error { return nil })
s.Step(`^failing$`, func() error { return fmt.Errorf("errored") })
s.Step(`^pending$`, func() error { return ErrPending })
},
}
expected := `
...F-.P-.UU.....F..P..U 23
--- Failed steps:
Scenario: failing scenario # any.feature:10
When failing # any.feature:11
Error: errored
Scenario Outline: outline # any.feature:22
When failing # any.feature:24
Error: errored
8 scenarios (2 passed, 2 failed, 2 pending, 2 undefined)
23 steps (14 passed, 2 failed, 2 pending, 3 undefined, 2 skipped)
%s
Randomized with seed: %s
You can implement step definitions for undefined steps with these snippets:
func undefined() error {
return godog.ErrPending
}
func nextUndefined() error {
return godog.ErrPending
}
func FeatureContext(s *godog.Suite) {
s.Step(` + "`^undefined$`" + `, undefined)
s.Step(` + "`^next undefined$`" + `, nextUndefined)
}`
var zeroDuration time.Duration
expected = fmt.Sprintf(expected, zeroDuration.String(), os.Getenv("GODOG_SEED"))
expected = trimAllLines(expected)
r.run()
actual := trimAllLines(buf.String())
shouldMatchOutput(expected, actual, t)
}
func trimAllLines(s string) string {
var lines []string
for _, ln := range strings.Split(strings.TrimSpace(s), "\n") {
lines = append(lines, strings.TrimSpace(ln))
}
return strings.Join(lines, "\n")
}
var basicGherkinFeature = `
Feature: basic
Scenario: passing scenario
When one
Then two
`
func TestProgressFormatterWhenStepPanics(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() error { panic("omg") })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
out := buf.String()
if idx := strings.Index(out, "github.com/DATA-DOG/godog/fmt_progress_test.go:114"); idx == -1 {
t.Fatalf("expected to find panic stacktrace, actual:\n%s", out)
}
}
func TestProgressFormatterWithPassingMultisteps(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^sub1$`, func() error { return nil })
s.Step(`^sub-sub$`, func() error { return nil })
s.Step(`^sub2$`, func() Steps { return Steps{"sub-sub", "sub1", "one"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{"sub1", "sub2"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
}
func TestProgressFormatterWithFailingMultisteps(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat, Path: "some.feature"}},
initializer: func(s *Suite) {
s.Step(`^sub1$`, func() error { return nil })
s.Step(`^sub-sub$`, func() error { return fmt.Errorf("errored") })
s.Step(`^sub2$`, func() Steps { return Steps{"sub-sub", "sub1", "one"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{"sub1", "sub2"} })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
expected := `
.F 2
--- Failed steps:
Scenario: passing scenario # some.feature:4
Then two # some.feature:6
Error: sub2: sub-sub: errored
1 scenarios (1 failed)
2 steps (1 passed, 1 failed)
%s
Randomized with seed: %s
`
expected = trimAllLines(expected)
var zeroDuration time.Duration
expected = fmt.Sprintf(expected, zeroDuration.String(), os.Getenv("GODOG_SEED"))
actual := trimAllLines(buf.String())
shouldMatchOutput(expected, actual, t)
}
func shouldMatchOutput(expected, actual string, t *testing.T) {
act := []byte(actual)
exp := []byte(expected)
if len(act) != len(exp) {
t.Fatalf("content lengths do not match, expected: %d, actual %d, actual output:\n%s", len(exp), len(act), actual)
}
for i := 0; i < len(exp); i++ {
if act[i] == exp[i] {
continue
}
cpe := make([]byte, len(exp))
copy(cpe, exp)
e := append(exp[:i], '^')
e = append(e, cpe[i:]...)
cpa := make([]byte, len(act))
copy(cpa, act)
a := append(act[:i], '^')
a = append(a, cpa[i:]...)
t.Fatalf("expected output does not match:\n%s\n\n%s", string(a), string(e))
}
}
func TestProgressFormatterWithPanicInMultistep(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^sub1$`, func() error { return nil })
s.Step(`^sub-sub$`, func() error { return nil })
s.Step(`^sub2$`, func() []string { return []string{"sub-sub", "sub1", "one"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() []string { return []string{"sub1", "sub2"} })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
}
func TestProgressFormatterMultistepTemplates(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^sub-sub$`, func() error { return nil })
s.Step(`^substep$`, func() Steps { return Steps{"sub-sub", `unavailable "John" cost 5`, "one", "three"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^(t)wo$`, func(s string) Steps { return Steps{"undef", "substep"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
expected := `
.U 2
1 scenarios (1 undefined)
2 steps (1 passed, 1 undefined)
%s
Randomized with seed: %s
You can implement step definitions for undefined steps with these snippets:
func undef() error {
return godog.ErrPending
}
func unavailableCost(arg1 string, arg2 int) error {
return godog.ErrPending
}
func three() error {
return godog.ErrPending
}
func FeatureContext(s *godog.Suite) {
s.Step(` + "`^undef$`" + `, undef)
s.Step(` + "`^unavailable \"([^\"]*)\" cost (\\d+)$`" + `, unavailableCost)
s.Step(` + "`^three$`" + `, three)
}
`
var zeroDuration time.Duration
expected = fmt.Sprintf(expected, zeroDuration.String(), os.Getenv("GODOG_SEED"))
expected = trimAllLines(expected)
actual := trimAllLines(buf.String())
if actual != expected {
t.Fatalf("expected output does not match: %s", actual)
}
}
func TestProgressFormatterWhenMultiStepHasArgument(t *testing.T) {
var featureSource = `
Feature: basic
Scenario: passing scenario
When one
Then two:
"""
text
"""
`
feat, err := gherkin.ParseFeature(strings.NewReader(featureSource))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
r := runner{
fmt: progressFunc("progress", ioutil.Discard),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two:$`, func(doc *gherkin.DocString) Steps { return Steps{"one"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
}
func TestProgressFormatterWhenMultiStepHasStepWithArgument(t *testing.T) {
var featureSource = `
Feature: basic
Scenario: passing scenario
When one
Then two`
feat, err := gherkin.ParseFeature(strings.NewReader(featureSource))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var subStep = `three:
"""
content
"""`
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{subStep} })
s.Step(`^three:$`, func(doc *gherkin.DocString) error { return nil })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
expected := `
.F 2
--- Failed steps:
Scenario: passing scenario # :4
Then two # :6
Error: nested steps cannot be multiline and have table or content body argument
1 scenarios (1 failed)
2 steps (1 passed, 1 failed)
%s
Randomized with seed: %s
`
var zeroDuration time.Duration
expected = fmt.Sprintf(expected, zeroDuration.String(), os.Getenv("GODOG_SEED"))
expected = trimAllLines(expected)
actual := trimAllLines(buf.String())
if actual != expected {
t.Fatalf("expected output does not match: %s", actual)
}
}

Просмотреть файл

@ -1,67 +1,25 @@
package godog_test
package godog
import (
"io"
"testing"
import "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.golang1.ru/softonik/godog"
)
func Test_FindFmt(t *testing.T) {
func TestShouldFindFormatter(t *testing.T) {
cases := map[string]bool{
"cucumber": true,
"custom": true, // is available for test purposes only
"events": true,
"junit": true,
"pretty": true,
"progress": true,
"progress": true, // true means should be available
"unknown": false,
"junit": true,
"cucumber": true,
"pretty": true,
"custom": true, // is available for test purposes only
"undef": false,
}
for name, expected := range cases {
t.Run(
name,
func(t *testing.T) {
actual := godog.FindFmt(name)
if expected {
assert.NotNilf(t, actual, "expected %s formatter should be available", name)
} else {
assert.Nilf(t, actual, "expected %s formatter should be available", name)
}
},
)
for name, shouldFind := range cases {
actual := FindFmt(name)
if actual == nil && shouldFind {
t.Fatalf("expected %s formatter should be available", name)
}
if actual != nil && !shouldFind {
t.Fatalf("expected %s formatter should not be available", name)
}
}
}
func Test_AvailableFormatters(t *testing.T) {
expected := map[string]string{
"cucumber": "Produces cucumber JSON format output.",
"custom": "custom format description", // is available for test purposes only
"events": "Produces JSON event stream, based on spec: 0.1.0.",
"junit": "Prints junit compatible xml to stdout",
"pretty": "Prints every feature with runtime statuses.",
"progress": "Prints a character per step.",
}
actual := godog.AvailableFormatters()
assert.Equal(t, expected, actual)
}
func Test_Format(t *testing.T) {
actual := godog.FindFmt("Test_Format")
require.Nil(t, actual)
godog.Format("Test_Format", "...", testFormatterFunc)
actual = godog.FindFmt("Test_Format")
assert.NotNil(t, actual)
}
func testFormatterFunc(suiteName string, out io.Writer) godog.Formatter {
return nil
}

Просмотреть файл

@ -1,108 +0,0 @@
package formatters
import (
"io"
"regexp"
messages "github.com/cucumber/messages/go/v21"
)
type registeredFormatter struct {
name string
description string
fmt FormatterFunc
}
var registeredFormatters []*registeredFormatter
// FindFmt searches available formatters registered
// and returns FormaterFunc matched by given
// format name or nil otherwise
func FindFmt(name string) FormatterFunc {
for _, el := range registeredFormatters {
if el.name == name {
return el.fmt
}
}
return nil
}
// Format registers a feature suite output
// formatter by given name, description and
// FormatterFunc constructor function, to initialize
// formatter with the output recorder.
func Format(name, description string, f FormatterFunc) {
registeredFormatters = append(registeredFormatters, &registeredFormatter{
name: name,
fmt: f,
description: description,
})
}
// AvailableFormatters gives a map of all
// formatters registered with their name as key
// and description as value
func AvailableFormatters() map[string]string {
fmts := make(map[string]string, len(registeredFormatters))
for _, f := range registeredFormatters {
fmts[f.name] = f.description
}
return fmts
}
// Formatter is an interface for feature runner
// output summary presentation.
//
// New formatters may be created to represent
// suite results in different ways. These new
// formatters needs to be registered with a
// godog.Format function call
type Formatter interface {
TestRunStarted()
Feature(*messages.GherkinDocument, string, []byte)
Pickle(*messages.Pickle)
Defined(*messages.Pickle, *messages.PickleStep, *StepDefinition)
Failed(*messages.Pickle, *messages.PickleStep, *StepDefinition, error)
Passed(*messages.Pickle, *messages.PickleStep, *StepDefinition)
Skipped(*messages.Pickle, *messages.PickleStep, *StepDefinition)
Undefined(*messages.Pickle, *messages.PickleStep, *StepDefinition)
Pending(*messages.Pickle, *messages.PickleStep, *StepDefinition)
Ambiguous(*messages.Pickle, *messages.PickleStep, *StepDefinition, error)
Summary()
}
// FlushFormatter is a `Formatter` but can be flushed.
type FlushFormatter interface {
Formatter
Flush()
}
// FormatterFunc builds a formatter with given
// suite name and io.Writer to record output
type FormatterFunc func(string, io.Writer) Formatter
// StepDefinition is a registered step definition
// contains a StepHandler and regexp which
// is used to match a step. Args which
// were matched by last executed step
//
// This structure is passed to the formatter
// when step is matched and is either failed
// or successful
type StepDefinition struct {
Expr *regexp.Regexp
Handler interface{}
Keyword Keyword
}
type Keyword int64
const (
Given Keyword = iota
When
Then
None
)

Просмотреть файл

@ -1,65 +0,0 @@
package formatters_test
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.golang1.ru/softonik/godog"
)
func Test_FindFmt(t *testing.T) {
cases := map[string]bool{
"cucumber": true,
"events": true,
"junit": true,
"pretty": true,
"progress": true,
"unknown": false,
"undef": false,
}
for name, expected := range cases {
t.Run(
name,
func(t *testing.T) {
actual := godog.FindFmt(name)
if expected {
assert.NotNilf(t, actual, "expected %s formatter should be available", name)
} else {
assert.Nilf(t, actual, "expected %s formatter should be available", name)
}
},
)
}
}
func Test_AvailableFormatters(t *testing.T) {
expected := map[string]string{
"cucumber": "Produces cucumber JSON format output.",
"events": "Produces JSON event stream, based on spec: 0.1.0.",
"junit": "Prints junit compatible xml to stdout",
"pretty": "Prints every feature with runtime statuses.",
"progress": "Prints a character per step.",
}
actual := godog.AvailableFormatters()
assert.Equal(t, expected, actual)
}
func Test_Format(t *testing.T) {
actual := godog.FindFmt("Test_Format")
require.Nil(t, actual)
godog.Format("Test_Format", "...", testFormatterFunc)
actual = godog.FindFmt("Test_Format")
assert.NotNil(t, actual)
}
func testFormatterFunc(suiteName string, out io.Writer) godog.Formatter {
return nil
}

36
gherkin.go Обычный файл
Просмотреть файл

@ -0,0 +1,36 @@
package godog
import "github.com/DATA-DOG/godog/gherkin"
// examples is a helper func to cast gherkin.Examples
// or gherkin.BaseExamples if its empty
// @TODO: this should go away with gherkin update
func examples(ex interface{}) (*gherkin.Examples, bool) {
t, ok := ex.(*gherkin.Examples)
return t, ok
}
// means there are no scenarios or they do not have steps
func isEmptyFeature(ft *gherkin.Feature) bool {
for _, def := range ft.ScenarioDefinitions {
if !isEmptyScenario(def) {
return false
}
}
return true
}
// means scenario dooes not have steps
func isEmptyScenario(def interface{}) bool {
switch t := def.(type) {
case *gherkin.Scenario:
if len(t.Steps) > 0 {
return false
}
case *gherkin.ScenarioOutline:
if len(t.Steps) > 0 {
return false
}
}
return true
}

21
gherkin/LICENSE Обычный файл
Просмотреть файл

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014-2016 Cucumber Ltd, Gaspar Nagy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Показаны не все изменённые файлы, т.к. их слишком много Показать больше