Repository: go-bdd/gobdd (go)

Issue:
Add current test, scenario and step to Context
**Is your feature request related to a problem? Please describe.**
1. I am using VCR library to record requests from client library to server and I would like to use correct cassette name depending from which scenario/step the request has been made.
2. I would like to create tracing span for each step and have them grouped by scenario.

**Describe the solution you'd like**
Add information about current test, scenario and step to context.

https://github.com/go-bdd/gobdd/blob/master/gobdd.go#L307

**Describe alternatives you've considered**
`-`

**Additional context**
See https://pytest-bdd.readthedocs.io/en/latest/#hooks

Pull request:
Store Feature and Scenario in test context
closes #121 

Patch:
diff --git a/docs/context.md b/docs/context.md
index 51ec36a..08da748 100644
--- a/docs/context.md
+++ b/docs/context.md
@@ -34,6 +34,18 @@ When the data is not provided, the whole test will fail.
 
 The context holds current test state `testing.T`. It is accessible by calling `Context.Get(TestingTKey{})`. This is useful if you need access to the test state from scenario or step hooks.
 
+It is also possible to access references to current feature and scenario by calling `Context.Get(FeatureNameKey{})` and `Context.Get(ScenarioNameKey{})` respectively.
+
+```go
+value, err := ctx.Get(FeatureKey{})
+feature, ok := value.(*msgs.GherkinDocument_Feature)
+```
+
+```go
+value, err := ctx.Get(ScenarioKey{})
+scenario, ok := value.(*msgs.GherkinDocument_Feature_Scenario)
+```
+
 ## Good practices
 
 It's a good practice to use custom structs as keys instead of strings or any built-in types to avoid collisions between steps using context.
diff --git a/gobdd.go b/gobdd.go
index 898d57b..be553ef 100644
--- a/gobdd.go
+++ b/gobdd.go
@@ -135,6 +135,13 @@ type TestingT interface {
 // TestingTKey is used to store reference to current *testing.T instance
 type TestingTKey struct{}
 
+// FeatureKey is used to store reference to current *msgs.GherkinDocument_Feature instance
+type FeatureKey struct{}
+
+// ScenarioKey is used to store reference to current *msgs.GherkinDocument_Feature_Scenario instance
+type ScenarioKey struct{}
+
+
 // Creates a new suites with given configuration and empty steps defined
 func NewSuite(t TestingT, optionClosures ...func(*SuiteOptions)) *Suite {
 	options := NewSuiteOptions()
@@ -326,6 +333,9 @@ func (s *Suite) runFeature(feature *msgs.GherkinDocument_Feature) error {
 				continue
 			}
 			ctx := NewContext()
+			ctx.Set(FeatureKey{}, feature)
+			ctx.Set(ScenarioKey{}, scenario)
+
 			s.runScenario(ctx, scenario, bkgSteps, t)
 		}
 	})


Test patch:
diff --git a/gobdd_test.go b/gobdd_test.go
index b269cfb..79eb05c 100644
--- a/gobdd_test.go
+++ b/gobdd_test.go
@@ -8,6 +8,7 @@ import (
 
 	msgs "github.com/cucumber/messages-go/v12"
 	"github.com/go-bdd/assert"
+	"github.com/stretchr/testify/require"
 )
 
 func TestScenarios(t *testing.T) {
@@ -162,6 +163,20 @@ func TestWithAfterStep(t *testing.T) {
 	c := 0
 	suite := NewSuite(t, WithFeaturesPath("features/background.feature"), WithAfterStep(func(ctx Context) {
 		c++
+
+		// feature should be *msgs.GherkinDocument_Feature
+		feature, err := ctx.Get(FeatureKey{})
+		require.NoError(t, err)
+		if _, ok := feature.(*msgs.GherkinDocument_Feature); !ok {
+			t.Errorf("expected feature but got %T", feature)
+		}
+
+		// scenario should be *msgs.GherkinDocument_Feature_Scenario
+		scenario, err := ctx.Get(ScenarioKey{})
+		require.NoError(t, err)
+		if _, ok := scenario.(*msgs.GherkinDocument_Feature_Scenario); !ok {
+			t.Errorf("expected scenario but got %T", scenario)
+		}
 	}))
 	suite.AddStep(`I add (\d+) and (\d+)`, add)
 	suite.AddStep(`the result should equal (\d+)`, check)